@gtkx/mcp 0.20.0 → 1.0.0-rc.1
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 +122 -55
- package/bin/gtkx-mcp.js +8 -1
- package/dist/app-router.d.ts +35 -0
- package/dist/app-router.d.ts.map +1 -0
- package/dist/app-router.js +130 -0
- package/dist/app-router.js.map +1 -0
- package/dist/connection-registry.d.ts +12 -0
- package/dist/connection-registry.d.ts.map +1 -0
- package/dist/connection-registry.js +38 -0
- package/dist/connection-registry.js.map +1 -0
- package/dist/internal.d.ts +4 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +4 -0
- package/dist/internal.js.map +1 -0
- package/dist/protocol/errors.d.ts +24 -87
- package/dist/protocol/errors.d.ts.map +1 -1
- package/dist/protocol/errors.js +27 -91
- package/dist/protocol/errors.js.map +1 -1
- package/dist/protocol/schemas.d.ts +89 -0
- package/dist/protocol/schemas.d.ts.map +1 -0
- package/dist/protocol/schemas.js +56 -0
- package/dist/protocol/schemas.js.map +1 -0
- package/dist/reference.d.ts +13 -0
- package/dist/reference.d.ts.map +1 -0
- package/dist/reference.js +250 -0
- package/dist/reference.js.map +1 -0
- package/dist/server.d.ts +14 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +220 -0
- package/dist/server.js.map +1 -0
- package/dist/socket-server.d.ts +4 -38
- package/dist/socket-server.d.ts.map +1 -1
- package/dist/socket-server.js +26 -109
- package/dist/socket-server.js.map +1 -1
- package/dist/tool.d.ts +22 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +36 -0
- package/dist/tool.js.map +1 -0
- package/dist/transport.d.ts +41 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +121 -0
- package/dist/transport.js.map +1 -0
- package/package.json +17 -11
- package/src/app-router.ts +172 -0
- package/src/connection-registry.ts +42 -0
- package/src/internal.ts +17 -0
- package/src/protocol/errors.ts +44 -100
- package/src/protocol/schemas.ts +148 -0
- package/src/reference.ts +340 -0
- package/src/server.ts +281 -0
- package/src/socket-server.ts +30 -154
- package/src/tool.ts +66 -0
- package/src/transport.ts +165 -0
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -399
- package/dist/cli.js.map +0 -1
- package/dist/connection-manager.d.ts +0 -48
- package/dist/connection-manager.d.ts.map +0 -1
- package/dist/connection-manager.js +0 -185
- package/dist/connection-manager.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -5
- package/dist/index.js.map +0 -1
- package/dist/protocol/types.d.ts +0 -132
- package/dist/protocol/types.d.ts.map +0 -1
- package/dist/protocol/types.js +0 -46
- package/dist/protocol/types.js.map +0 -1
- package/src/cli.ts +0 -449
- package/src/connection-manager.ts +0 -247
- package/src/index.ts +0 -27
- package/src/protocol/types.ts +0 -153
package/README.md
CHANGED
|
@@ -1,106 +1,173 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
|
|
2
|
+
<img src="https://raw.githubusercontent.com/gtkx-org/gtkx/main/logo.svg" alt="GTKX" width="100" />
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<h1 align="center">GTKX</h1>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
|
-
|
|
8
|
+
The React framework for Linux.<br />
|
|
9
|
+
Build GTK4 and Adwaita apps in TypeScript, with React components and hooks driving GNOME's own widgets. What you ship is a GNOME app.
|
|
9
10
|
</p>
|
|
10
11
|
|
|
11
12
|
<p align="center">
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
<a href="https://www.npmjs.com/package/create-gtkx"><img src="https://img.shields.io/npm/v/create-gtkx?color=cb3837&logo=npm&label=create-gtkx" alt="npm version" /></a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/create-gtkx"><img src="https://img.shields.io/npm/dm/create-gtkx?color=cb3837&logo=npm&label=downloads" alt="npm downloads" /></a>
|
|
15
|
+
<img src="https://img.shields.io/badge/node-%E2%89%A524-339933?logo=node.js&logoColor=white" alt="Node >= 24" />
|
|
16
|
+
<a href="https://github.com/gtkx-org/gtkx/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MPL--2.0-blue.svg" alt="License: MPL-2.0" /></a>
|
|
17
|
+
<a href="https://github.com/gtkx-org/gtkx/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/gtkx-org/gtkx/ci.yml?branch=main&logo=github&label=CI" alt="CI status" /></a>
|
|
18
|
+
<img src="https://img.shields.io/badge/TypeScript-strict-3178c6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<a href="https://gtkx.dev">Homepage</a> ·
|
|
23
|
+
<a href="https://gtkx.dev/guide/why-gtkx">Documentation</a> ·
|
|
24
|
+
<a href="https://github.com/gtkx-org/gtkx/tree/main/examples">Examples</a> ·
|
|
25
|
+
<a href="https://github.com/gtkx-org/gtkx/blob/main/CONTRIBUTING.md">Contributing</a>
|
|
16
26
|
</p>
|
|
17
27
|
|
|
18
28
|
---
|
|
19
29
|
|
|
20
|
-
GTKX
|
|
30
|
+
GTKX generates fully typed bindings for the entire GTK4 and Adwaita surface directly from GObject-Introspection. On top of those bindings you get the React programming model: components and hooks driving GObject instances, with Fast Refresh while you develop.
|
|
21
31
|
|
|
22
|
-
|
|
32
|
+
<p align="center">
|
|
33
|
+
<img src="https://raw.githubusercontent.com/gtkx-org/gtkx/main/examples/tutorial/assets/screenshot.png" alt="The Tasks app: an Adwaita window with a sidebar of smart views and colored lists on the left, and a boxed task list on the right." />
|
|
34
|
+
</p>
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
<p align="center">
|
|
37
|
+
<em>The Tasks app you build in the <a href="https://gtkx.dev/tutorial/">tutorial</a>.</em>
|
|
38
|
+
</p>
|
|
39
|
+
|
|
40
|
+
## Demo
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
The intrinsic elements in this snippet render GTK4 widgets, and ordinary React hooks and events drive them:
|
|
31
43
|
|
|
32
44
|
```tsx
|
|
33
|
-
import
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
GtkButton,
|
|
37
|
-
GtkLabel,
|
|
38
|
-
quit,
|
|
39
|
-
render,
|
|
40
|
-
} from "@gtkx/react";
|
|
41
|
-
import * as Gtk from "@gtkx/ffi/gtk";
|
|
45
|
+
import * as Gtk from "@gtkx/gi/gtk";
|
|
46
|
+
import { GtkApplication, GtkApplicationWindow, GtkBox, GtkButton, GtkLabel } from "@gtkx/jsx/gtk";
|
|
47
|
+
import { createRoot, quit } from "@gtkx/react";
|
|
42
48
|
import { useState } from "react";
|
|
43
49
|
|
|
44
|
-
const
|
|
50
|
+
const Counter = () => {
|
|
45
51
|
const [count, setCount] = useState(0);
|
|
46
52
|
|
|
47
53
|
return (
|
|
48
54
|
<GtkApplicationWindow
|
|
49
|
-
title="
|
|
50
|
-
defaultWidth={
|
|
51
|
-
defaultHeight={
|
|
52
|
-
|
|
55
|
+
title="Hello GTKX"
|
|
56
|
+
defaultWidth={400}
|
|
57
|
+
defaultHeight={300}
|
|
58
|
+
onCloseRequest={quit}
|
|
53
59
|
>
|
|
54
60
|
<GtkBox
|
|
55
61
|
orientation={Gtk.Orientation.VERTICAL}
|
|
56
62
|
spacing={20}
|
|
63
|
+
marginTop={40}
|
|
64
|
+
marginBottom={40}
|
|
65
|
+
marginStart={40}
|
|
66
|
+
marginEnd={40}
|
|
57
67
|
valign={Gtk.Align.CENTER}
|
|
68
|
+
halign={Gtk.Align.CENTER}
|
|
58
69
|
>
|
|
59
|
-
<GtkLabel
|
|
60
|
-
<
|
|
70
|
+
<GtkLabel cssClasses={["title-1"]}>Welcome to GTKX!</GtkLabel>
|
|
71
|
+
<GtkLabel cssClasses={["title-2"]}>{`Count: ${count}`}</GtkLabel>
|
|
72
|
+
<GtkButton
|
|
73
|
+
label="Increment"
|
|
74
|
+
onClicked={() => setCount((c) => c + 1)}
|
|
75
|
+
cssClasses={["suggested-action", "pill"]}
|
|
76
|
+
/>
|
|
61
77
|
</GtkBox>
|
|
62
78
|
</GtkApplicationWindow>
|
|
63
79
|
);
|
|
64
80
|
};
|
|
65
81
|
|
|
66
|
-
|
|
82
|
+
const App = () => (
|
|
83
|
+
<GtkApplication>
|
|
84
|
+
<Counter />
|
|
85
|
+
</GtkApplication>
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
createRoot().render(<App />);
|
|
67
89
|
```
|
|
68
90
|
|
|
69
|
-
|
|
91
|
+
This is the [`hello-world`](https://github.com/gtkx-org/gtkx/tree/main/examples/hello-world) example, with `app.tsx` and `index.tsx` combined into a single snippet. `@gtkx/gi` and `@gtkx/jsx` are per-project bindings generated by the CLI, not packages you install from npm.
|
|
70
92
|
|
|
71
|
-
|
|
72
|
-
- **Native GTK4 widgets** — Real native controls, not web components in a webview
|
|
73
|
-
- **Adwaita support** — Modern GNOME styling with Libadwaita components
|
|
74
|
-
- **Declarative animations** — Framer Motion-like API using native Adwaita animations
|
|
75
|
-
- **Hot Module Replacement** — Fast refresh during development
|
|
76
|
-
- **TypeScript first** — Full type safety with auto-generated bindings
|
|
77
|
-
- **CSS-in-JS styling** — Familiar styling patterns adapted for GTK
|
|
78
|
-
- **Testing utilities** — Component testing similar to Testing Library
|
|
93
|
+
## Why GTKX
|
|
79
94
|
|
|
80
|
-
|
|
95
|
+
### A declarative layer for the GNOME stack
|
|
96
|
+
|
|
97
|
+
GTK4 is mature, and GtkBuilder XML can lay out a static interface, but nothing re-renders that interface when your application state changes, and nothing hot-reloads it as you work. GTKX adds that missing layer, and the tooling around it, on top of the stack you already know:
|
|
98
|
+
|
|
99
|
+
- a React reconciler that exposes every GObject as a JSX element,
|
|
100
|
+
- a CLI for scaffolding, development, and production builds,
|
|
101
|
+
- a dev server with Fast Refresh that patches your running UI in place,
|
|
102
|
+
- CSS-in-JS styling and high-level list, grid, and dialog components,
|
|
103
|
+
- a Testing Library-style API for querying and driving your widgets in tests,
|
|
104
|
+
- and a Model Context Protocol (MCP) server that exposes your live app to AI agents.
|
|
105
|
+
|
|
106
|
+
### The full GNOME API surface
|
|
107
|
+
|
|
108
|
+
React Native and similar frameworks hide the native toolkit so one API can run everywhere. GTKX exposes it: GTK4, Adwaita, and any other GObject-Introspection library on your system. Linux-only by design.
|
|
109
|
+
|
|
110
|
+
### Why Node.js, and why generated bindings
|
|
111
|
+
|
|
112
|
+
GTKX runs on Node.js, which puts native modules, the npm ecosystem, and the tooling built for Node.js APIs within reach. GJS is GNOME's own runtime, built on SpiderMonkey rather than V8; node-gtk runs on Node.js but is lightly maintained, on the older nan/V8 ABI rather than N-API, and still centered on GTK3. The [why-gtkx guide](https://gtkx.dev/guide/why-gtkx) covers the comparison in full.
|
|
113
|
+
|
|
114
|
+
GTKX generates the TypeScript types and the native FFI calls from the same GObject-Introspection data, so the types cannot drift from the calls they back. Codegen covers the whole GTK4 and Adwaita surface.
|
|
115
|
+
|
|
116
|
+
At runtime, the native Rust core calls straight into the system GTK4, Adwaita, and GLib libraries through libffi, without loading libgirepository at all.
|
|
81
117
|
|
|
82
|
-
|
|
118
|
+
## Quick start
|
|
83
119
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
120
|
+
GTKX is Linux-only and needs Node.js 24 or later. See [Requirements](#requirements).
|
|
121
|
+
|
|
122
|
+
Scaffold a new app with the `create-gtkx` initializer:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
npm create gtkx@rc
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The same command works with other package managers: `pnpm create gtkx@rc` or `yarn create gtkx@rc`.
|
|
129
|
+
|
|
130
|
+
Then run your new app:
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
cd my-app
|
|
134
|
+
npm run dev
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
To go further, follow the [tutorial](https://gtkx.dev/tutorial/).
|
|
90
138
|
|
|
91
139
|
## Documentation
|
|
92
140
|
|
|
93
|
-
|
|
141
|
+
The documentation at **[gtkx.dev](https://gtkx.dev)** includes a step-by-step tutorial that builds a complete GNOME app, from scaffolding to packaging and shipping, plus guides and a full API reference.
|
|
94
142
|
|
|
95
|
-
|
|
143
|
+
**[Read the docs →](https://gtkx.dev/guide/why-gtkx)**
|
|
96
144
|
|
|
97
|
-
|
|
145
|
+
## Requirements
|
|
98
146
|
|
|
99
|
-
|
|
147
|
+
GTKX is Linux-only. You need:
|
|
148
|
+
|
|
149
|
+
- Linux with the GTK4 (4.20 or later), Adwaita (1.8 or later), and GLib development libraries
|
|
150
|
+
- Node.js 24 or later
|
|
151
|
+
|
|
152
|
+
The `@gtkx/native` addon ships prebuilt for x64 and arm64 glibc Linux; other targets need to build it from the GTKX repository, which requires a Rust toolchain.
|
|
153
|
+
|
|
154
|
+
## Examples
|
|
155
|
+
|
|
156
|
+
Explore the [example apps](https://github.com/gtkx-org/gtkx/tree/main/examples):
|
|
157
|
+
|
|
158
|
+
- [`hello-world`](https://github.com/gtkx-org/gtkx/tree/main/examples/hello-world): the counter above.
|
|
159
|
+
- [`gtk-demo`](https://github.com/gtkx-org/gtkx/tree/main/examples/gtk-demo): a React port of the official GTK4 widget showcase, covering lists, dialogs, gestures, CSS, and OpenGL.
|
|
160
|
+
- [`browser`](https://github.com/gtkx-org/gtkx/tree/main/examples/browser): a WebKitWebView-based web browser.
|
|
161
|
+
- [`tutorial`](https://github.com/gtkx-org/gtkx/tree/main/examples/tutorial): the Tasks app the documentation builds.
|
|
162
|
+
|
|
163
|
+
## Status
|
|
164
|
+
|
|
165
|
+
GTKX 1.0 is at the release candidate stage.
|
|
166
|
+
|
|
167
|
+
## Contributing
|
|
100
168
|
|
|
101
|
-
- [
|
|
102
|
-
- [Issue Tracker](https://github.com/gtkx-org/gtkx/issues) — Bug reports and feature requests
|
|
169
|
+
Contributions are welcome. See [CONTRIBUTING.md](https://github.com/gtkx-org/gtkx/blob/main/CONTRIBUTING.md), the [Code of Conduct](https://github.com/gtkx-org/gtkx/blob/main/CODE_OF_CONDUCT.md), and the [security policy](https://github.com/gtkx-org/gtkx/blob/main/SECURITY.md). Building the repo needs Node.js 24 or later, pnpm, and a Rust toolchain.
|
|
103
170
|
|
|
104
171
|
## License
|
|
105
172
|
|
|
106
|
-
[MPL-2.0](
|
|
173
|
+
GTKX is licensed under [MPL-2.0](https://github.com/gtkx-org/gtkx/blob/main/LICENSE).
|
package/bin/gtkx-mcp.js
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { type AppInfo } from "./protocol/schemas.js";
|
|
3
|
+
import { type AppConnections, type ProtocolConnection } from "./transport.js";
|
|
4
|
+
type AppRouterEventMap = {
|
|
5
|
+
appRegistered: [AppInfo];
|
|
6
|
+
appUnregistered: [string];
|
|
7
|
+
};
|
|
8
|
+
type RegisteredApp = {
|
|
9
|
+
info: AppInfo;
|
|
10
|
+
connection: ProtocolConnection;
|
|
11
|
+
};
|
|
12
|
+
export declare class AppRouter extends EventEmitter<AppRouterEventMap> {
|
|
13
|
+
private static DEFAULT_WAIT_TIMEOUT;
|
|
14
|
+
private apps;
|
|
15
|
+
private connectionToApp;
|
|
16
|
+
private requestTimeout;
|
|
17
|
+
private connections;
|
|
18
|
+
constructor(connections: AppConnections, options?: {
|
|
19
|
+
requestTimeout?: number;
|
|
20
|
+
});
|
|
21
|
+
getApps(): AppInfo[];
|
|
22
|
+
hasConnectedApps(): boolean;
|
|
23
|
+
getDefaultApp(): RegisteredApp | undefined;
|
|
24
|
+
getProjectRoot(): string | undefined;
|
|
25
|
+
waitForApp(timeout?: number): Promise<AppInfo>;
|
|
26
|
+
sendToApp<T>(applicationId: string | undefined, method: string, params?: unknown): Promise<T>;
|
|
27
|
+
private handleRequest;
|
|
28
|
+
private handleRegister;
|
|
29
|
+
private handleUnregister;
|
|
30
|
+
private acknowledge;
|
|
31
|
+
private sendError;
|
|
32
|
+
private removeApp;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=app-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-router.d.ts","sourceRoot":"","sources":["../src/app-router.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AASvC,OAAO,EAAE,KAAK,OAAO,EAAqD,MAAM,uBAAuB,CAAC;AACxG,OAAO,EAAE,KAAK,cAAc,EAAyB,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAErG,KAAK,iBAAiB,GAAG;IACrB,aAAa,EAAE,CAAC,OAAO,CAAC,CAAC;IACzB,eAAe,EAAE,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC;AAEF,KAAK,aAAa,GAAG;IACjB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,kBAAkB,CAAC;CAClC,CAAC;AAIF,qBAAa,SAAU,SAAQ,YAAY,CAAC,iBAAiB,CAAC;IAC1D,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAS;IAE5C,OAAO,CAAC,IAAI,CAAyC;IACrD,OAAO,CAAC,eAAe,CAAkC;IACzD,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,WAAW,CAAiB;IAEpC,YAAY,WAAW,EAAE,cAAc,EAAE,OAAO,GAAE;QAAE,cAAc,CAAC,EAAE,MAAM,CAAA;KAAO,EAYjF;IAED,OAAO,IAAI,OAAO,EAAE,CAEnB;IAED,gBAAgB,IAAI,OAAO,CAE1B;IAED,aAAa,IAAI,aAAa,GAAG,SAAS,CAGzC;IAED,cAAc,IAAI,MAAM,GAAG,SAAS,CAEnC;IAED,UAAU,CAAC,OAAO,GAAE,MAAuC,GAAG,OAAO,CAAC,OAAO,CAAC,CAyB7E;IAEK,SAAS,CAAC,CAAC,EAAE,aAAa,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAmBlG;IAED,OAAO,CAAC,aAAa;IAUrB,OAAO,CAAC,cAAc;IAsBtB,OAAO,CAAC,gBAAgB;IAKxB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,SAAS;CAQpB"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { appNotFoundError, connectionWriteFailedError, invalidRequestError, methodNotFoundError, noAppConnectedError, } from "./protocol/errors.js";
|
|
3
|
+
import { RegisterParamsSchema } from "./protocol/schemas.js";
|
|
4
|
+
import { ConnectionClosedError } from "./transport.js";
|
|
5
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 30000;
|
|
6
|
+
export class AppRouter extends EventEmitter {
|
|
7
|
+
static DEFAULT_WAIT_TIMEOUT = 10000;
|
|
8
|
+
apps = new Map();
|
|
9
|
+
connectionToApp = new Map();
|
|
10
|
+
requestTimeout;
|
|
11
|
+
connections;
|
|
12
|
+
constructor(connections, options = {}) {
|
|
13
|
+
super();
|
|
14
|
+
this.connections = connections;
|
|
15
|
+
this.requestTimeout = options.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
16
|
+
this.connections.on("request", (connection, request) => {
|
|
17
|
+
this.handleRequest(connection, request);
|
|
18
|
+
});
|
|
19
|
+
this.connections.on("disconnection", (connection) => {
|
|
20
|
+
this.removeApp(connection);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
getApps() {
|
|
24
|
+
return Array.from(this.apps.values()).map((app) => app.info);
|
|
25
|
+
}
|
|
26
|
+
hasConnectedApps() {
|
|
27
|
+
return this.apps.size > 0;
|
|
28
|
+
}
|
|
29
|
+
getDefaultApp() {
|
|
30
|
+
const first = this.apps.values().next();
|
|
31
|
+
return first.done ? undefined : first.value;
|
|
32
|
+
}
|
|
33
|
+
getProjectRoot() {
|
|
34
|
+
return this.getDefaultApp()?.info.projectRoot;
|
|
35
|
+
}
|
|
36
|
+
waitForApp(timeout = AppRouter.DEFAULT_WAIT_TIMEOUT) {
|
|
37
|
+
const defaultApp = this.getDefaultApp();
|
|
38
|
+
if (defaultApp) {
|
|
39
|
+
return Promise.resolve(defaultApp.info);
|
|
40
|
+
}
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const timeoutId = setTimeout(() => {
|
|
43
|
+
this.off("appRegistered", onRegister);
|
|
44
|
+
reject(new Error(`Timeout waiting for app registration after ${timeout}ms. ` +
|
|
45
|
+
"Make sure your GTKX app is running with 'gtkx dev'."));
|
|
46
|
+
}, timeout);
|
|
47
|
+
const onRegister = (appInfo) => {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
this.off("appRegistered", onRegister);
|
|
50
|
+
resolve(appInfo);
|
|
51
|
+
};
|
|
52
|
+
this.on("appRegistered", onRegister);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async sendToApp(applicationId, method, params) {
|
|
56
|
+
const app = applicationId ? this.apps.get(applicationId) : this.getDefaultApp();
|
|
57
|
+
if (!app) {
|
|
58
|
+
if (applicationId) {
|
|
59
|
+
throw appNotFoundError(applicationId);
|
|
60
|
+
}
|
|
61
|
+
throw noAppConnectedError();
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
return await app.connection.send(method, params, this.requestTimeout);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error instanceof ConnectionClosedError) {
|
|
68
|
+
this.removeApp(app.connection);
|
|
69
|
+
throw connectionWriteFailedError(app.info.applicationId);
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
handleRequest(connection, request) {
|
|
75
|
+
if (request.method === "app.register") {
|
|
76
|
+
this.handleRegister(connection, request);
|
|
77
|
+
}
|
|
78
|
+
else if (request.method === "app.unregister") {
|
|
79
|
+
this.handleUnregister(connection, request);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.sendError(connection, request, methodNotFoundError(request.method));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
handleRegister(connection, request) {
|
|
86
|
+
const parseResult = RegisterParamsSchema.safeParse(request.params);
|
|
87
|
+
if (!parseResult.success) {
|
|
88
|
+
this.sendError(connection, request, invalidRequestError(parseResult.error.message));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const params = parseResult.data;
|
|
92
|
+
const appInfo = {
|
|
93
|
+
applicationId: params.applicationId,
|
|
94
|
+
pid: params.pid,
|
|
95
|
+
...(params.projectRoot === undefined ? {} : { projectRoot: params.projectRoot }),
|
|
96
|
+
};
|
|
97
|
+
this.apps.set(params.applicationId, { info: appInfo, connection });
|
|
98
|
+
this.connectionToApp.set(connection.id, params.applicationId);
|
|
99
|
+
this.acknowledge(connection, request);
|
|
100
|
+
this.emit("appRegistered", appInfo);
|
|
101
|
+
}
|
|
102
|
+
handleUnregister(connection, request) {
|
|
103
|
+
this.removeApp(connection);
|
|
104
|
+
this.acknowledge(connection, request);
|
|
105
|
+
}
|
|
106
|
+
acknowledge(connection, request) {
|
|
107
|
+
const response = {
|
|
108
|
+
id: request.id,
|
|
109
|
+
result: { success: true },
|
|
110
|
+
};
|
|
111
|
+
this.connections.send(connection.id, response);
|
|
112
|
+
}
|
|
113
|
+
sendError(connection, request, error) {
|
|
114
|
+
this.connections.send(connection.id, {
|
|
115
|
+
id: request.id,
|
|
116
|
+
error: error.toErrorObject(),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
removeApp(connection) {
|
|
120
|
+
const applicationId = this.connectionToApp.get(connection.id);
|
|
121
|
+
this.connectionToApp.delete(connection.id);
|
|
122
|
+
if (applicationId === undefined)
|
|
123
|
+
return;
|
|
124
|
+
if (this.apps.get(applicationId)?.connection !== connection)
|
|
125
|
+
return;
|
|
126
|
+
this.apps.delete(applicationId);
|
|
127
|
+
this.emit("appUnregistered", applicationId);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=app-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-router.js","sourceRoot":"","sources":["../src/app-router.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,EACH,gBAAgB,EAChB,0BAA0B,EAC1B,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GAEtB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAgB,oBAAoB,EAA+B,MAAM,uBAAuB,CAAC;AACxG,OAAO,EAAuB,qBAAqB,EAA2B,MAAM,gBAAgB,CAAC;AAYrG,MAAM,0BAA0B,GAAG,KAAK,CAAC;AAEzC,MAAM,OAAO,SAAU,SAAQ,YAA+B;IAClD,MAAM,CAAC,oBAAoB,GAAG,KAAK,CAAC;IAEpC,IAAI,GAA+B,IAAI,GAAG,EAAE,CAAC;IAC7C,eAAe,GAAwB,IAAI,GAAG,EAAE,CAAC;IACjD,cAAc,CAAS;IACvB,WAAW,CAAiB;IAEpC,YAAY,WAA2B,EAAE,OAAO,GAAgC,EAAE;QAC9E,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,0BAA0B,CAAC;QAE3E,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE;YACnD,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,eAAe,EAAE,CAAC,UAAU,EAAE,EAAE;YAChD,IAAI,CAAC,SAAS,CAAC,UAA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W,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC;IAEO,WAAW,CAAC,UAA8B,EAAE,OAAgB;QAChE,MAAM,QAAQ,GAAa;YACvB,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;SAC5B,CAAC;QACF,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IACnD,CAAC;IAEO,SAAS,CAAC,UAA8B,EAAE,OAAgB,EAAE,KAAoB;QACpF,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE;YACjC,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,KAAK,EAAE,KAAK,CAAC,aAAa,EAAE;SAC/B,CAAC,CAAC;IACP,CAAC;IAEO,SAAS,CAAC,UAA8B;QAC5C,MAAM,aAAa,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QAC9D,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QAC3C,IAAI,aAAa,KAAK,SAAS;YAAE,OAAO;QACxC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,UAAU,KAAK,UAAU;YAAE,OAAO;QACpE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAChC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,aAAa,CAAC,CAAC;IAChD,CAAC;CACJ","sourcesContent":["import EventEmitter from \"node:events\";\nimport {\n appNotFoundError,\n connectionWriteFailedError,\n invalidRequestError,\n methodNotFoundError,\n noAppConnectedError,\n type ProtocolError,\n} from \"./protocol/errors.js\";\nimport { type AppInfo, RegisterParamsSchema, type Request, type Response } from \"./protocol/schemas.js\";\nimport { type AppConnections, ConnectionClosedError, type ProtocolConnection } from \"./transport.js\";\n\ntype AppRouterEventMap = {\n appRegistered: [AppInfo];\n appUnregistered: [string];\n};\n\ntype RegisteredApp = {\n info: AppInfo;\n connection: ProtocolConnection;\n};\n\nconst DEFAULT_REQUEST_TIMEOUT_MS = 30000;\n\nexport class AppRouter extends EventEmitter<AppRouterEventMap> {\n private static DEFAULT_WAIT_TIMEOUT = 10000;\n\n private apps: Map<string, RegisteredApp> = new Map();\n private connectionToApp: Map<string, string> = new Map();\n private requestTimeout: number;\n private connections: AppConnections;\n\n constructor(connections: AppConnections, options: { requestTimeout?: number } = {}) {\n super();\n this.connections = connections;\n this.requestTimeout = options.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT_MS;\n\n this.connections.on(\"request\", (connection, request) => {\n this.handleRequest(connection, request);\n });\n\n this.connections.on(\"disconnection\", (connection) => {\n this.removeApp(connection);\n });\n }\n\n getApps(): AppInfo[] {\n return Array.from(this.apps.values()).map((app) => app.info);\n }\n\n hasConnectedApps(): boolean {\n return this.apps.size > 0;\n }\n\n getDefaultApp(): RegisteredApp | undefined {\n const first = this.apps.values().next();\n return first.done ? undefined : first.value;\n }\n\n getProjectRoot(): string | undefined {\n return this.getDefaultApp()?.info.projectRoot;\n }\n\n waitForApp(timeout: number = AppRouter.DEFAULT_WAIT_TIMEOUT): Promise<AppInfo> {\n const defaultApp = this.getDefaultApp();\n if (defaultApp) {\n return Promise.resolve(defaultApp.info);\n }\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n this.off(\"appRegistered\", onRegister);\n reject(\n new Error(\n `Timeout waiting for app registration after ${timeout}ms. ` +\n \"Make sure your GTKX app is running with 'gtkx dev'.\",\n ),\n );\n }, timeout);\n\n const onRegister = (appInfo: AppInfo) => {\n clearTimeout(timeoutId);\n this.off(\"appRegistered\", onRegister);\n resolve(appInfo);\n };\n\n this.on(\"appRegistered\", onRegister);\n });\n }\n\n async sendToApp<T>(applicationId: string | undefined, method: string, params?: unknown): Promise<T> {\n const app = applicationId ? this.apps.get(applicationId) : this.getDefaultApp();\n\n if (!app) {\n if (applicationId) {\n throw appNotFoundError(applicationId);\n }\n throw noAppConnectedError();\n }\n\n try {\n return await app.connection.send<T>(method, params, this.requestTimeout);\n } catch (error) {\n if (error instanceof ConnectionClosedError) {\n this.removeApp(app.connection);\n throw connectionWriteFailedError(app.info.applicationId);\n }\n throw error;\n }\n }\n\n private handleRequest(connection: ProtocolConnection, request: Request): void {\n if (request.method === \"app.register\") {\n this.handleRegister(connection, request);\n } else if (request.method === \"app.unregister\") {\n this.handleUnregister(connection, request);\n } else {\n this.sendError(connection, request, methodNotFoundError(request.method));\n }\n }\n\n private handleRegister(connection: ProtocolConnection, request: Request): void {\n const parseResult = RegisterParamsSchema.safeParse(request.params);\n if (!parseResult.success) {\n this.sendError(connection, request, invalidRequestError(parseResult.error.message));\n return;\n }\n\n const params = parseResult.data;\n const appInfo: AppInfo = {\n applicationId: params.applicationId,\n pid: params.pid,\n ...(params.projectRoot === undefined ? {} : { projectRoot: params.projectRoot }),\n };\n\n this.apps.set(params.applicationId, { info: appInfo, connection });\n this.connectionToApp.set(connection.id, params.applicationId);\n\n this.acknowledge(connection, request);\n\n this.emit(\"appRegistered\", appInfo);\n }\n\n private handleUnregister(connection: ProtocolConnection, request: Request): void {\n this.removeApp(connection);\n this.acknowledge(connection, request);\n }\n\n private acknowledge(connection: ProtocolConnection, request: Request): void {\n const response: Response = {\n id: request.id,\n result: { success: true },\n };\n this.connections.send(connection.id, response);\n }\n\n private sendError(connection: ProtocolConnection, request: Request, error: ProtocolError): void {\n this.connections.send(connection.id, {\n id: request.id,\n error: error.toErrorObject(),\n });\n }\n\n private removeApp(connection: ProtocolConnection): void {\n const applicationId = this.connectionToApp.get(connection.id);\n this.connectionToApp.delete(connection.id);\n if (applicationId === undefined) return;\n if (this.apps.get(applicationId)?.connection !== connection) return;\n this.apps.delete(applicationId);\n this.emit(\"appUnregistered\", applicationId);\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import type { Socket } from "node:net";
|
|
3
|
+
import type { Message } from "./protocol/schemas.js";
|
|
4
|
+
import { type AppConnectionEvents, type AppConnections, ProtocolConnection } from "./transport.js";
|
|
5
|
+
export declare class ConnectionRegistry extends EventEmitter<AppConnectionEvents> implements AppConnections {
|
|
6
|
+
private connections;
|
|
7
|
+
private sockets;
|
|
8
|
+
register(socket: Socket): ProtocolConnection;
|
|
9
|
+
send(connectionId: string, message: Message): void;
|
|
10
|
+
dispose(reason: string): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=connection-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-registry.d.ts","sourceRoot":"","sources":["../src/connection-registry.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,cAAc,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEnG,qBAAa,kBAAmB,SAAQ,YAAY,CAAC,mBAAmB,CAAE,YAAW,cAAc;IAC/F,OAAO,CAAC,WAAW,CAA8C;IACjE,OAAO,CAAC,OAAO,CAAkC;IAEjD,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAgB3C;IAED,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAIjD;IAED,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAO5B;CACJ"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { ProtocolConnection } from "./transport.js";
|
|
3
|
+
export class ConnectionRegistry extends EventEmitter {
|
|
4
|
+
connections = new Map();
|
|
5
|
+
sockets = new Map();
|
|
6
|
+
register(socket) {
|
|
7
|
+
const connection = ProtocolConnection.fromSocket(socket, {
|
|
8
|
+
onClose: () => {
|
|
9
|
+
this.connections.delete(connection.id);
|
|
10
|
+
this.sockets.delete(connection.id);
|
|
11
|
+
this.emit("disconnection", connection);
|
|
12
|
+
},
|
|
13
|
+
onError: (error) => this.emit("error", error),
|
|
14
|
+
});
|
|
15
|
+
this.connections.set(connection.id, connection);
|
|
16
|
+
this.sockets.set(connection.id, socket);
|
|
17
|
+
connection.on("request", (request) => this.emit("request", connection, request));
|
|
18
|
+
connection.on("invalid", ({ id: badId, error }) => {
|
|
19
|
+
connection.write({ id: badId, error: error.toErrorObject() });
|
|
20
|
+
});
|
|
21
|
+
return connection;
|
|
22
|
+
}
|
|
23
|
+
send(connectionId, message) {
|
|
24
|
+
const connection = this.connections.get(connectionId);
|
|
25
|
+
if (!connection)
|
|
26
|
+
return;
|
|
27
|
+
connection.write(message);
|
|
28
|
+
}
|
|
29
|
+
dispose(reason) {
|
|
30
|
+
for (const connection of this.connections.values()) {
|
|
31
|
+
connection.rejectPending(new Error(reason));
|
|
32
|
+
}
|
|
33
|
+
for (const socket of this.sockets.values()) {
|
|
34
|
+
socket.destroy();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=connection-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-registry.js","sourceRoot":"","sources":["../src/connection-registry.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AAGvC,OAAO,EAAiD,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEnG,MAAM,OAAO,kBAAmB,SAAQ,YAAiC;IAC7D,WAAW,GAAoC,IAAI,GAAG,EAAE,CAAC;IACzD,OAAO,GAAwB,IAAI,GAAG,EAAE,CAAC;IAEjD,QAAQ,CAAC,MAAc;QACnB,MAAM,UAAU,GAAG,kBAAkB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AAM,UAAU,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,EAAE,CAAC;YACjD,UAAU,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;QAChD,CAAC;QACD,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,OAAO,EAAE,CAAC;QACrB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import EventEmitter from \"node:events\";\nimport type { Socket } from \"node:net\";\nimport type { Message } from \"./protocol/schemas.js\";\nimport { type AppConnectionEvents, type AppConnections, ProtocolConnection } from \"./transport.js\";\n\nexport class ConnectionRegistry extends EventEmitter<AppConnectionEvents> implements AppConnections {\n private connections: Map<string, ProtocolConnection> = new Map();\n private sockets: Map<string, Socket> = new Map();\n\n register(socket: Socket): ProtocolConnection {\n const connection = ProtocolConnection.fromSocket(socket, {\n onClose: () => {\n this.connections.delete(connection.id);\n this.sockets.delete(connection.id);\n this.emit(\"disconnection\", connection);\n },\n onError: (error) => this.emit(\"error\", error),\n });\n this.connections.set(connection.id, connection);\n this.sockets.set(connection.id, socket);\n connection.on(\"request\", (request) => this.emit(\"request\", connection, request));\n connection.on(\"invalid\", ({ id: badId, error }) => {\n connection.write({ id: badId, error: error.toErrorObject() });\n });\n return connection;\n }\n\n send(connectionId: string, message: Message): void {\n const connection = this.connections.get(connectionId);\n if (!connection) return;\n connection.write(message);\n }\n\n dispose(reason: string): void {\n for (const connection of this.connections.values()) {\n connection.rejectPending(new Error(reason));\n }\n for (const socket of this.sockets.values()) {\n socket.destroy();\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ErrorCode, invalidRequestError, methodNotFoundError, ProtocolError, widgetNotFoundError, } from "./protocol/errors.js";
|
|
2
|
+
export { DEFAULT_SOCKET_PATH, type ParamsSchema, type Request, type SerializedWidget, type ServerInitiatedMethod, type ServerRequestParams, ServerRequestParamsSchemas, } from "./protocol/schemas.js";
|
|
3
|
+
export { ProtocolConnection } from "./transport.js";
|
|
4
|
+
//# sourceMappingURL=internal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,SAAS,EACT,mBAAmB,EACnB,mBAAmB,EACnB,aAAa,EACb,mBAAmB,GACtB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACH,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,OAAO,EACZ,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACxB,0BAA0B,GAC7B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ErrorCode, invalidRequestError, methodNotFoundError, ProtocolError, widgetNotFoundError, } from "./protocol/errors.js";
|
|
2
|
+
export { DEFAULT_SOCKET_PATH, ServerRequestParamsSchemas, } from "./protocol/schemas.js";
|
|
3
|
+
export { ProtocolConnection } from "./transport.js";
|
|
4
|
+
//# sourceMappingURL=internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,SAAS,EACT,mBAAmB,EACnB,mBAAmB,EACnB,aAAa,EACb,mBAAmB,GACtB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACH,mBAAmB,EAMnB,0BAA0B,GAC7B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export {\n ErrorCode,\n invalidRequestError,\n methodNotFoundError,\n ProtocolError,\n widgetNotFoundError,\n} from \"./protocol/errors.js\";\nexport {\n DEFAULT_SOCKET_PATH,\n type ParamsSchema,\n type Request,\n type SerializedWidget,\n type ServerInitiatedMethod,\n type ServerRequestParams,\n ServerRequestParamsSchemas,\n} from \"./protocol/schemas.js\";\nexport { ProtocolConnection } from \"./transport.js\";\n"]}
|