@cmdless/ui-sdk 0.0.4 → 0.0.6
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 +17 -21
- package/dist/browser/index.d.ts +4 -74
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +29 -80
- package/dist/browser/index.js.map +1 -1
- package/dist/index.d.ts +44 -40
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -13
- package/dist/index.js.map +1 -1
- package/dist/node/host.d.ts +270 -0
- package/dist/node/host.d.ts.map +1 -0
- package/dist/node/host.js +398 -0
- package/dist/node/host.js.map +1 -0
- package/dist/node/index.d.ts +1 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -2
- package/dist/node/index.js.map +1 -1
- package/dist/node/utils.d.ts +9 -92
- package/dist/node/utils.d.ts.map +1 -1
- package/dist/node/utils.js +50 -325
- package/dist/node/utils.js.map +1 -1
- package/dist/node/utils.test.js +13 -87
- package/dist/node/utils.test.js.map +1 -1
- package/dist/prompts.d.ts +33 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +66 -0
- package/dist/prompts.js.map +1 -0
- package/dist/types.d.ts +25 -78
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -27
- package/dist/types.js.map +1 -1
- package/package.json +4 -10
- package/dist/app.d.ts +0 -30
- package/dist/app.d.ts.map +0 -1
- package/dist/app.js +0 -8
- package/dist/app.js.map +0 -1
- package/dist/emitter.d.ts +0 -11
- package/dist/emitter.d.ts.map +0 -1
- package/dist/emitter.js +0 -37
- package/dist/emitter.js.map +0 -1
- package/dist/node/app.d.ts +0 -25
- package/dist/node/app.d.ts.map +0 -1
- package/dist/node/app.fixture.d.ts +0 -9
- package/dist/node/app.fixture.d.ts.map +0 -1
- package/dist/node/app.fixture.js +0 -21
- package/dist/node/app.fixture.js.map +0 -1
- package/dist/node/app.js +0 -192
- package/dist/node/app.js.map +0 -1
- package/dist/node/app.test.d.ts +0 -2
- package/dist/node/app.test.d.ts.map +0 -1
- package/dist/node/app.test.js +0 -185
- package/dist/node/app.test.js.map +0 -1
- package/dist/node/bin.d.ts +0 -3
- package/dist/node/bin.d.ts.map +0 -1
- package/dist/node/bin.js +0 -4
- package/dist/node/bin.js.map +0 -1
- package/dist/node/mcp.test.d.ts +0 -2
- package/dist/node/mcp.test.d.ts.map +0 -1
- package/dist/node/mcp.test.js +0 -92
- package/dist/node/mcp.test.js.map +0 -1
- package/dist/node/runtime.d.ts +0 -32
- package/dist/node/runtime.d.ts.map +0 -1
- package/dist/node/runtime.js +0 -20
- package/dist/node/runtime.js.map +0 -1
- package/dist/node/streams.d.ts +0 -24
- package/dist/node/streams.d.ts.map +0 -1
- package/dist/node/streams.js +0 -54
- package/dist/node/streams.js.map +0 -1
- package/dist/runtime.d.ts +0 -31
- package/dist/runtime.d.ts.map +0 -1
- package/dist/runtime.js +0 -22
- package/dist/runtime.js.map +0 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import type { RpcProtocol } from '@cmdless/rpc-sdk';
|
|
2
|
+
import type { ElicitationRequest, PermissionRequest } from '../prompts.js';
|
|
3
|
+
import type { CardEvent, CardView, FormView, MessageBoxType, ShowType } from '../types.js';
|
|
4
|
+
export type HostService = {
|
|
5
|
+
address: string;
|
|
6
|
+
};
|
|
7
|
+
export type InitializeParams = {
|
|
8
|
+
sdkVersion: string;
|
|
9
|
+
};
|
|
10
|
+
export type InitializeResult = {
|
|
11
|
+
appName: string;
|
|
12
|
+
userData: string;
|
|
13
|
+
ephemeral: boolean;
|
|
14
|
+
sdkVersion: string;
|
|
15
|
+
hasSingleInstanceLock: boolean;
|
|
16
|
+
};
|
|
17
|
+
export type WindowOptions = {
|
|
18
|
+
width?: number;
|
|
19
|
+
height?: number;
|
|
20
|
+
title?: string;
|
|
21
|
+
};
|
|
22
|
+
export type WindowParams = {
|
|
23
|
+
type: ShowType;
|
|
24
|
+
source: string;
|
|
25
|
+
redirect?: string;
|
|
26
|
+
external?: boolean;
|
|
27
|
+
window?: WindowOptions;
|
|
28
|
+
parent?: string;
|
|
29
|
+
backend?: WindowBackend;
|
|
30
|
+
};
|
|
31
|
+
export type WindowOpenParams = Omit<WindowParams, 'backend'> & {
|
|
32
|
+
backend?: string;
|
|
33
|
+
};
|
|
34
|
+
export type DialogParams = {
|
|
35
|
+
type: MessageBoxType;
|
|
36
|
+
message: string;
|
|
37
|
+
detail?: string;
|
|
38
|
+
buttons?: string[];
|
|
39
|
+
defaultId?: number;
|
|
40
|
+
cancelId?: number;
|
|
41
|
+
checkboxLabel?: string;
|
|
42
|
+
checkboxChecked?: boolean;
|
|
43
|
+
parent?: string;
|
|
44
|
+
};
|
|
45
|
+
export type DialogResult = {
|
|
46
|
+
response: number;
|
|
47
|
+
checkboxChecked: boolean;
|
|
48
|
+
};
|
|
49
|
+
export type TrayMenuItem = {
|
|
50
|
+
type?: 'normal' | 'checkbox' | 'radio';
|
|
51
|
+
id: string;
|
|
52
|
+
label: string;
|
|
53
|
+
checked?: boolean;
|
|
54
|
+
enabled?: boolean;
|
|
55
|
+
} | {
|
|
56
|
+
type: 'submenu';
|
|
57
|
+
label: string;
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
submenu: TrayMenuItem[];
|
|
60
|
+
} | {
|
|
61
|
+
type: 'separator';
|
|
62
|
+
};
|
|
63
|
+
export type TrayParams = {
|
|
64
|
+
icon: string;
|
|
65
|
+
tooltip?: string;
|
|
66
|
+
title?: string;
|
|
67
|
+
menu?: TrayMenuItem[];
|
|
68
|
+
};
|
|
69
|
+
export type ViewWindowParams = {
|
|
70
|
+
window?: WindowOptions;
|
|
71
|
+
parent?: string;
|
|
72
|
+
};
|
|
73
|
+
export type CardParams = ViewWindowParams & CardView;
|
|
74
|
+
export type CardUpdateParams = HandleParams & Pick<CardView, 'card'>;
|
|
75
|
+
export type FormParams = ViewWindowParams & FormView;
|
|
76
|
+
export type NotificationParams = {
|
|
77
|
+
title: string;
|
|
78
|
+
body?: string;
|
|
79
|
+
};
|
|
80
|
+
export type TrayUpdateParams = HandleParams & Partial<TrayParams>;
|
|
81
|
+
export type TrayEvent = {
|
|
82
|
+
event: 'click';
|
|
83
|
+
} | {
|
|
84
|
+
event: 'menu-item';
|
|
85
|
+
item: string;
|
|
86
|
+
};
|
|
87
|
+
export type OpenExternalParams = {
|
|
88
|
+
url: string;
|
|
89
|
+
};
|
|
90
|
+
export type ClipboardWriteParams = {
|
|
91
|
+
text: string;
|
|
92
|
+
};
|
|
93
|
+
export type HandleParams = {
|
|
94
|
+
id: string;
|
|
95
|
+
};
|
|
96
|
+
export type WindowConnectParams = {
|
|
97
|
+
backend: string;
|
|
98
|
+
};
|
|
99
|
+
export type WindowConnectResult = {
|
|
100
|
+
url: string;
|
|
101
|
+
token: string;
|
|
102
|
+
};
|
|
103
|
+
export type HandleCloseParams = HandleParams & {
|
|
104
|
+
value?: unknown;
|
|
105
|
+
};
|
|
106
|
+
export type HandleClosedParams = HandleParams & {
|
|
107
|
+
value?: unknown;
|
|
108
|
+
error?: string;
|
|
109
|
+
};
|
|
110
|
+
declare const uiHostProtocol: import("@cmdless/rpc-sdk").Protocol<{
|
|
111
|
+
readonly clientToServer: import("@cmdless/rpc-sdk").RpcSide<NoInfer<{
|
|
112
|
+
init: import("@cmdless/rpc-sdk").RpcRequest<InitializeParams, InitializeResult>;
|
|
113
|
+
windowOpen: import("@cmdless/rpc-sdk").RpcRequest<WindowOpenParams, HandleParams>;
|
|
114
|
+
dialogOpen: import("@cmdless/rpc-sdk").RpcRequest<DialogParams, HandleParams>;
|
|
115
|
+
trayCreate: import("@cmdless/rpc-sdk").RpcRequest<TrayParams, HandleParams>;
|
|
116
|
+
trayUpdate: import("@cmdless/rpc-sdk").RpcRequest<TrayUpdateParams, void>;
|
|
117
|
+
handleClose: import("@cmdless/rpc-sdk").RpcRequest<HandleCloseParams, void>;
|
|
118
|
+
handleFocus: import("@cmdless/rpc-sdk").RpcRequest<HandleParams, void>;
|
|
119
|
+
shellOpenExternal: import("@cmdless/rpc-sdk").RpcRequest<OpenExternalParams, void>;
|
|
120
|
+
clipboardWriteText: import("@cmdless/rpc-sdk").RpcRequest<ClipboardWriteParams, void>;
|
|
121
|
+
notificationShow: import("@cmdless/rpc-sdk").RpcRequest<NotificationParams, HandleParams>;
|
|
122
|
+
cardOpen: import("@cmdless/rpc-sdk").RpcRequest<CardParams, HandleParams>;
|
|
123
|
+
cardUpdate: import("@cmdless/rpc-sdk").RpcRequest<CardUpdateParams, void>;
|
|
124
|
+
formOpen: import("@cmdless/rpc-sdk").RpcRequest<FormParams, HandleParams>;
|
|
125
|
+
}>, {}>;
|
|
126
|
+
readonly serverToClient: import("@cmdless/rpc-sdk").RpcSide<NoInfer<{
|
|
127
|
+
windowConnect: import("@cmdless/rpc-sdk").RpcRequest<WindowConnectParams, WindowConnectResult>;
|
|
128
|
+
}>, NoInfer<{
|
|
129
|
+
handleClosed: import("@cmdless/rpc-sdk").RpcNotification<HandleClosedParams>;
|
|
130
|
+
trayEvent: import("@cmdless/rpc-sdk").RpcNotification<HandleParams & TrayEvent>;
|
|
131
|
+
cardEvent: import("@cmdless/rpc-sdk").RpcNotification<HandleParams & CardEvent>;
|
|
132
|
+
}>>;
|
|
133
|
+
}>;
|
|
134
|
+
export type UIServer = ReturnType<typeof UIHost.protocol.createServer>;
|
|
135
|
+
export type UIClient = ReturnType<typeof uiHostProtocol.createClient>;
|
|
136
|
+
export type CreateHostOptions = {
|
|
137
|
+
app?: string;
|
|
138
|
+
cmdlessRoot?: string;
|
|
139
|
+
timeoutMs?: number;
|
|
140
|
+
};
|
|
141
|
+
export type WindowBackend = (connection: Parameters<RpcProtocol['createServer']>[0]) => {
|
|
142
|
+
listen(): void;
|
|
143
|
+
};
|
|
144
|
+
declare const end: unique symbol;
|
|
145
|
+
export interface UIOpener {
|
|
146
|
+
readonly host: UIHost;
|
|
147
|
+
openWindow<W = unknown>(params: Omit<WindowParams, 'parent'>): Promise<RemoteWindow<W>>;
|
|
148
|
+
openDialog(params: Omit<DialogParams, 'parent'>): Promise<RemoteDialog>;
|
|
149
|
+
openCard(params: Omit<CardParams, 'parent'>): Promise<RemoteCard>;
|
|
150
|
+
openForm<F = unknown>(params: Omit<FormParams, 'parent'>): Promise<RemoteForm<F>>;
|
|
151
|
+
open<P, H extends RemoteHandle<any>>(factory: UIFactory<P, H>, params: P): Promise<H>;
|
|
152
|
+
}
|
|
153
|
+
export type UIFactory<P, H extends RemoteHandle<any>> = (opener: UIOpener, params: P) => Promise<H>;
|
|
154
|
+
export declare abstract class RemoteHandle<T = unknown> implements UIOpener {
|
|
155
|
+
readonly host: UIHost;
|
|
156
|
+
readonly id: string;
|
|
157
|
+
readonly closed: Promise<T>;
|
|
158
|
+
private settle;
|
|
159
|
+
constructor(host: UIHost, id: string);
|
|
160
|
+
close(value?: T): Promise<void>;
|
|
161
|
+
focus(): Promise<void>;
|
|
162
|
+
openWindow<W = unknown>(params: Omit<WindowParams, 'parent'>): Promise<RemoteWindow<W>>;
|
|
163
|
+
openDialog(params: Omit<DialogParams, 'parent'>): Promise<RemoteDialog>;
|
|
164
|
+
openCard(params: Omit<CardParams, 'parent'>): Promise<RemoteCard>;
|
|
165
|
+
openForm<F = unknown>(params: Omit<FormParams, 'parent'>): Promise<RemoteForm<F>>;
|
|
166
|
+
open<P, H extends RemoteHandle<any>>(factory: UIFactory<P, H>, params: P): Promise<H>;
|
|
167
|
+
[end]({ value, error }: Omit<HandleClosedParams, 'id'>): void;
|
|
168
|
+
}
|
|
169
|
+
export declare class RemoteWindow<T = unknown> extends RemoteHandle<T> {
|
|
170
|
+
}
|
|
171
|
+
export declare class RemoteDialog extends RemoteHandle<DialogResult> {
|
|
172
|
+
}
|
|
173
|
+
export declare class RemoteNotification extends RemoteHandle<'click' | undefined> {
|
|
174
|
+
}
|
|
175
|
+
declare const emit: unique symbol;
|
|
176
|
+
export declare class RemoteTray extends RemoteHandle<void> {
|
|
177
|
+
private readonly listeners;
|
|
178
|
+
update(params: Partial<TrayParams>): Promise<void>;
|
|
179
|
+
onEvent(listener: (event: TrayEvent) => void): {
|
|
180
|
+
dispose: () => void;
|
|
181
|
+
};
|
|
182
|
+
[emit](event: TrayEvent): void;
|
|
183
|
+
}
|
|
184
|
+
export declare class RemoteCard extends RemoteHandle<CardEvent | undefined> {
|
|
185
|
+
private readonly listeners;
|
|
186
|
+
update(card: CardUpdateParams['card']): Promise<void>;
|
|
187
|
+
onEvent(listener: (event: CardEvent) => void): {
|
|
188
|
+
dispose: () => void;
|
|
189
|
+
};
|
|
190
|
+
[emit](event: CardEvent): void;
|
|
191
|
+
}
|
|
192
|
+
export declare class RemoteForm<F = unknown> extends RemoteHandle<F | undefined> {
|
|
193
|
+
}
|
|
194
|
+
export declare class UIShellHandler {
|
|
195
|
+
private readonly host;
|
|
196
|
+
constructor(host: UIHost);
|
|
197
|
+
openExternal(url: string): Promise<void>;
|
|
198
|
+
}
|
|
199
|
+
export declare class UIClipboardHandler {
|
|
200
|
+
private readonly host;
|
|
201
|
+
constructor(host: UIHost);
|
|
202
|
+
writeText(text: string): Promise<void>;
|
|
203
|
+
}
|
|
204
|
+
export declare class UIHost implements UIOpener {
|
|
205
|
+
readonly client: UIClient;
|
|
206
|
+
readonly params: InitializeResult;
|
|
207
|
+
static readonly protocol: import("@cmdless/rpc-sdk").Protocol<{
|
|
208
|
+
readonly clientToServer: import("@cmdless/rpc-sdk").RpcSide<NoInfer<{
|
|
209
|
+
init: import("@cmdless/rpc-sdk").RpcRequest<InitializeParams, InitializeResult>;
|
|
210
|
+
windowOpen: import("@cmdless/rpc-sdk").RpcRequest<WindowOpenParams, HandleParams>;
|
|
211
|
+
dialogOpen: import("@cmdless/rpc-sdk").RpcRequest<DialogParams, HandleParams>;
|
|
212
|
+
trayCreate: import("@cmdless/rpc-sdk").RpcRequest<TrayParams, HandleParams>;
|
|
213
|
+
trayUpdate: import("@cmdless/rpc-sdk").RpcRequest<TrayUpdateParams, void>;
|
|
214
|
+
handleClose: import("@cmdless/rpc-sdk").RpcRequest<HandleCloseParams, void>;
|
|
215
|
+
handleFocus: import("@cmdless/rpc-sdk").RpcRequest<HandleParams, void>;
|
|
216
|
+
shellOpenExternal: import("@cmdless/rpc-sdk").RpcRequest<OpenExternalParams, void>;
|
|
217
|
+
clipboardWriteText: import("@cmdless/rpc-sdk").RpcRequest<ClipboardWriteParams, void>;
|
|
218
|
+
notificationShow: import("@cmdless/rpc-sdk").RpcRequest<NotificationParams, HandleParams>;
|
|
219
|
+
cardOpen: import("@cmdless/rpc-sdk").RpcRequest<CardParams, HandleParams>;
|
|
220
|
+
cardUpdate: import("@cmdless/rpc-sdk").RpcRequest<CardUpdateParams, void>;
|
|
221
|
+
formOpen: import("@cmdless/rpc-sdk").RpcRequest<FormParams, HandleParams>;
|
|
222
|
+
}>, {}>;
|
|
223
|
+
readonly serverToClient: import("@cmdless/rpc-sdk").RpcSide<NoInfer<{
|
|
224
|
+
windowConnect: import("@cmdless/rpc-sdk").RpcRequest<WindowConnectParams, WindowConnectResult>;
|
|
225
|
+
}>, NoInfer<{
|
|
226
|
+
handleClosed: import("@cmdless/rpc-sdk").RpcNotification<HandleClosedParams>;
|
|
227
|
+
trayEvent: import("@cmdless/rpc-sdk").RpcNotification<HandleParams & TrayEvent>;
|
|
228
|
+
cardEvent: import("@cmdless/rpc-sdk").RpcNotification<HandleParams & CardEvent>;
|
|
229
|
+
}>>;
|
|
230
|
+
}>;
|
|
231
|
+
static factory<P, H extends RemoteHandle<any>>(factory: UIFactory<P, H>): UIFactory<P, H>;
|
|
232
|
+
get host(): this;
|
|
233
|
+
readonly shell: UIShellHandler;
|
|
234
|
+
readonly clipboard: UIClipboardHandler;
|
|
235
|
+
private readonly remotes;
|
|
236
|
+
private readonly unclaimed;
|
|
237
|
+
private readonly backends;
|
|
238
|
+
private readonly tokens;
|
|
239
|
+
private backendServer?;
|
|
240
|
+
private constructor();
|
|
241
|
+
private connectWindow;
|
|
242
|
+
private startBackendServer;
|
|
243
|
+
private closeBackendServer;
|
|
244
|
+
private forgetBackend;
|
|
245
|
+
static create({ app, cmdlessRoot, timeoutMs }?: CreateHostOptions): Promise<UIHost>;
|
|
246
|
+
static showWindow<T = unknown>(params: WindowParams, options?: CreateHostOptions): Promise<T>;
|
|
247
|
+
static showDialog(params: DialogParams, options?: CreateHostOptions): Promise<DialogResult>;
|
|
248
|
+
static showCard(params: CardParams, options?: CreateHostOptions): Promise<CardEvent | undefined>;
|
|
249
|
+
static showElicitation(request: ElicitationRequest & {
|
|
250
|
+
window?: WindowOptions;
|
|
251
|
+
}, options?: CreateHostOptions): Promise<import("../prompts.js").ElicitationResult>;
|
|
252
|
+
static showPermission(request: PermissionRequest & {
|
|
253
|
+
window?: WindowOptions;
|
|
254
|
+
}, options?: CreateHostOptions): Promise<import("../prompts.js").PermissionOutcome>;
|
|
255
|
+
static showForm<F = unknown>(params: FormParams, options?: CreateHostOptions): Promise<F | undefined>;
|
|
256
|
+
static show<P, T>(factory: UIFactory<P, RemoteHandle<T>>, params: P, options?: CreateHostOptions): Promise<T>;
|
|
257
|
+
private static once;
|
|
258
|
+
open<P, H extends RemoteHandle<any>>(factory: UIFactory<P, H>, params: P): Promise<H>;
|
|
259
|
+
openWindow<T = unknown>({ backend, ...params }: WindowParams): Promise<RemoteWindow<T>>;
|
|
260
|
+
openDialog(params: DialogParams): Promise<RemoteDialog>;
|
|
261
|
+
showNotification(params: NotificationParams): Promise<RemoteNotification>;
|
|
262
|
+
createTray(params: TrayParams): Promise<RemoteTray>;
|
|
263
|
+
openCard(params: CardParams): Promise<RemoteCard>;
|
|
264
|
+
openForm<F = unknown>(params: FormParams): Promise<RemoteForm<F>>;
|
|
265
|
+
private track;
|
|
266
|
+
onClose(listener: () => void): import("@cmdless/rpc-sdk").Disposable;
|
|
267
|
+
dispose(): void;
|
|
268
|
+
}
|
|
269
|
+
export {};
|
|
270
|
+
//# sourceMappingURL=host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|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|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { defineProtocol } from '@cmdless/rpc-sdk';
|
|
4
|
+
import { connectSocket } from '@cmdless/rpc-sdk/node';
|
|
5
|
+
import { createWebSocket } from '@cmdless/rpc-sdk/ws';
|
|
6
|
+
import { closeWebSocketServer, getWebSocketUrl, waitForWebSocketListening } from '@cmdless/rpc-sdk/ws/node';
|
|
7
|
+
import { resolvePackageBin, waitForService } from '@cmdless/core/node';
|
|
8
|
+
import { getCmdlessUIVersion, setCmdlessApp } from './utils.js';
|
|
9
|
+
import { elicitationCard, elicitationResult, permissionCard, permissionResult } from '../prompts.js';
|
|
10
|
+
const uiHostProtocol = defineProtocol(({ side, request, notification }) => ({
|
|
11
|
+
clientToServer: side({
|
|
12
|
+
requests: {
|
|
13
|
+
init: request('init'),
|
|
14
|
+
windowOpen: request('window/open'),
|
|
15
|
+
dialogOpen: request('dialog/open'),
|
|
16
|
+
trayCreate: request('tray/create'),
|
|
17
|
+
trayUpdate: request('tray/update'),
|
|
18
|
+
handleClose: request('handle/close'),
|
|
19
|
+
handleFocus: request('handle/focus'),
|
|
20
|
+
shellOpenExternal: request('shell/openExternal'),
|
|
21
|
+
clipboardWriteText: request('clipboard/writeText'),
|
|
22
|
+
notificationShow: request('notification/show'),
|
|
23
|
+
cardOpen: request('card/open'),
|
|
24
|
+
cardUpdate: request('card/update'),
|
|
25
|
+
formOpen: request('form/open'),
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
serverToClient: side({
|
|
29
|
+
notifications: {
|
|
30
|
+
handleClosed: notification('handle/closed'),
|
|
31
|
+
trayEvent: notification('tray/event'),
|
|
32
|
+
cardEvent: notification('card/event'),
|
|
33
|
+
},
|
|
34
|
+
requests: {
|
|
35
|
+
windowConnect: request('window/connect'),
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
}));
|
|
39
|
+
// how long an end that arrived ahead of its handle's open is kept
|
|
40
|
+
const UNCLAIMED_MS = 60_000;
|
|
41
|
+
// how a remote handle is ended - by its UIHost, which hears it
|
|
42
|
+
const end = Symbol('end');
|
|
43
|
+
// a window, dialog or tray a UIHost has opened on its host
|
|
44
|
+
export class RemoteHandle {
|
|
45
|
+
host;
|
|
46
|
+
id;
|
|
47
|
+
// the value it ends with: a window's resolve, the redirect it caught, or
|
|
48
|
+
// nothing if it was closed by hand. Rejects if it failed, or the host went.
|
|
49
|
+
closed;
|
|
50
|
+
settle;
|
|
51
|
+
constructor(host, id) {
|
|
52
|
+
this.host = host;
|
|
53
|
+
this.id = id;
|
|
54
|
+
this.closed = new Promise((resolve, reject) => this.settle = { resolve, reject });
|
|
55
|
+
// nobody has to wait on how it ends
|
|
56
|
+
this.closed.catch(() => { });
|
|
57
|
+
}
|
|
58
|
+
// ends it, with the value it ends with
|
|
59
|
+
close(value) {
|
|
60
|
+
return this.host.client.request.handleClose({ id: this.id, value });
|
|
61
|
+
}
|
|
62
|
+
// brings its window to the front, restored if it was minimized - nothing,
|
|
63
|
+
// for what has no window of its own (a dialog, a tray, a notification)
|
|
64
|
+
focus() {
|
|
65
|
+
return this.host.client.request.handleFocus({ id: this.id });
|
|
66
|
+
}
|
|
67
|
+
// opens a window in front of this one
|
|
68
|
+
openWindow(params) {
|
|
69
|
+
return this.host.openWindow({ ...params, parent: this.id });
|
|
70
|
+
}
|
|
71
|
+
// shows a dialog in front of this one
|
|
72
|
+
openDialog(params) {
|
|
73
|
+
return this.host.openDialog({ ...params, parent: this.id });
|
|
74
|
+
}
|
|
75
|
+
// opens a card window in front of this one
|
|
76
|
+
openCard(params) {
|
|
77
|
+
return this.host.openCard({ ...params, parent: this.id });
|
|
78
|
+
}
|
|
79
|
+
// opens a form window in front of this one
|
|
80
|
+
openForm(params) {
|
|
81
|
+
return this.host.openForm({ ...params, parent: this.id });
|
|
82
|
+
}
|
|
83
|
+
// opens what a factory opens, in front of this one
|
|
84
|
+
open(factory, params) {
|
|
85
|
+
return factory(this, params);
|
|
86
|
+
}
|
|
87
|
+
[end]({ value, error }) {
|
|
88
|
+
if (error !== undefined)
|
|
89
|
+
this.settle.reject(new Error(error));
|
|
90
|
+
else
|
|
91
|
+
this.settle.resolve(value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export class RemoteWindow extends RemoteHandle {
|
|
95
|
+
}
|
|
96
|
+
export class RemoteDialog extends RemoteHandle {
|
|
97
|
+
}
|
|
98
|
+
// a notification ends with 'click' if it was clicked, or nothing if it was
|
|
99
|
+
// dismissed (or closed from here)
|
|
100
|
+
export class RemoteNotification extends RemoteHandle {
|
|
101
|
+
}
|
|
102
|
+
// how a remote tray hears what happened to it - from its UIHost
|
|
103
|
+
const emit = Symbol('emit');
|
|
104
|
+
// a tray stays until it's closed, telling whoever listens when it's clicked
|
|
105
|
+
export class RemoteTray extends RemoteHandle {
|
|
106
|
+
listeners = new Set();
|
|
107
|
+
// changes its icon, tooltip, title or menu - anything left out stays
|
|
108
|
+
update(params) {
|
|
109
|
+
return this.host.client.request.trayUpdate({ ...params, id: this.id });
|
|
110
|
+
}
|
|
111
|
+
onEvent(listener) {
|
|
112
|
+
this.listeners.add(listener);
|
|
113
|
+
return { dispose: () => { this.listeners.delete(listener); } };
|
|
114
|
+
}
|
|
115
|
+
[emit](event) {
|
|
116
|
+
for (const listener of [...this.listeners])
|
|
117
|
+
listener(event);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// a card ends with the Action.Submit chosen on it (and its inputs), or
|
|
121
|
+
// nothing if it was closed instead. Until then, whoever listens hears each
|
|
122
|
+
// Action.Execute chosen on it - and can show another card in its place.
|
|
123
|
+
export class RemoteCard extends RemoteHandle {
|
|
124
|
+
listeners = new Set();
|
|
125
|
+
// shows another card in its place
|
|
126
|
+
update(card) {
|
|
127
|
+
return this.host.client.request.cardUpdate({ id: this.id, card });
|
|
128
|
+
}
|
|
129
|
+
onEvent(listener) {
|
|
130
|
+
this.listeners.add(listener);
|
|
131
|
+
return { dispose: () => { this.listeners.delete(listener); } };
|
|
132
|
+
}
|
|
133
|
+
[emit](event) {
|
|
134
|
+
for (const listener of [...this.listeners])
|
|
135
|
+
listener(event);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// a form ends with the data it was submitted with, or nothing if it was
|
|
139
|
+
// cancelled or closed
|
|
140
|
+
export class RemoteForm extends RemoteHandle {
|
|
141
|
+
}
|
|
142
|
+
// what the host can do on the desktop beyond its own windows - Electron's
|
|
143
|
+
// shell, through the host
|
|
144
|
+
export class UIShellHandler {
|
|
145
|
+
host;
|
|
146
|
+
constructor(host) {
|
|
147
|
+
this.host = host;
|
|
148
|
+
}
|
|
149
|
+
// opens a link in the default browser (http and https), or mail app
|
|
150
|
+
// (mailto) - anything else is refused
|
|
151
|
+
openExternal(url) {
|
|
152
|
+
return this.host.client.request.shellOpenExternal({ url });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// the system clipboard, through the host
|
|
156
|
+
export class UIClipboardHandler {
|
|
157
|
+
host;
|
|
158
|
+
constructor(host) {
|
|
159
|
+
this.host = host;
|
|
160
|
+
}
|
|
161
|
+
writeText(text) {
|
|
162
|
+
return this.host.client.request.clipboardWriteText({ text });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
export class UIHost {
|
|
166
|
+
client;
|
|
167
|
+
params;
|
|
168
|
+
static protocol = uiHostProtocol;
|
|
169
|
+
// defines a factory: only there to infer and check its params and handle,
|
|
170
|
+
// the way defineProtocol is for protocols
|
|
171
|
+
static factory(factory) {
|
|
172
|
+
return factory;
|
|
173
|
+
}
|
|
174
|
+
// a host is its own: what a factory reaches through its opener
|
|
175
|
+
get host() {
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
shell = new UIShellHandler(this);
|
|
179
|
+
clipboard = new UIClipboardHandler(this);
|
|
180
|
+
remotes = new Map();
|
|
181
|
+
// A handle can end before the reply saying it opened arrives: on macOS a
|
|
182
|
+
// dialog with no parent holds the host up until it's answered, so its end
|
|
183
|
+
// goes out ahead of the reply to dialog/open. An end for a handle not known
|
|
184
|
+
// yet is kept here until its open is answered - for a while, not forever.
|
|
185
|
+
unclaimed = new Map();
|
|
186
|
+
// each open window's backend, by the key it was opened with - known before
|
|
187
|
+
// the window is, so a page can never ask for one that isn't here yet
|
|
188
|
+
backends = new Map();
|
|
189
|
+
// tokens handed out and not yet used, each good for one connection to one
|
|
190
|
+
// window's backend (by its key)
|
|
191
|
+
tokens = new Map();
|
|
192
|
+
// where every window's backend is reached, started when a page first asks
|
|
193
|
+
backendServer;
|
|
194
|
+
constructor(client, params) {
|
|
195
|
+
this.client = client;
|
|
196
|
+
this.params = params;
|
|
197
|
+
client.onRequest.windowConnect(params => this.connectWindow(params));
|
|
198
|
+
client.onNotification.handleClosed(params => {
|
|
199
|
+
const remote = this.remotes.get(params.id);
|
|
200
|
+
this.remotes.delete(params.id);
|
|
201
|
+
if (remote)
|
|
202
|
+
return remote[end](params);
|
|
203
|
+
this.unclaimed.set(params.id, params);
|
|
204
|
+
setTimeout(() => this.unclaimed.delete(params.id), UNCLAIMED_MS).unref();
|
|
205
|
+
});
|
|
206
|
+
client.onNotification.trayEvent(({ id, ...event }) => {
|
|
207
|
+
const remote = this.remotes.get(id);
|
|
208
|
+
if (remote instanceof RemoteTray)
|
|
209
|
+
remote[emit](event);
|
|
210
|
+
});
|
|
211
|
+
client.onNotification.cardEvent(({ id, ...event }) => {
|
|
212
|
+
const remote = this.remotes.get(id);
|
|
213
|
+
if (remote instanceof RemoteCard)
|
|
214
|
+
remote[emit](event);
|
|
215
|
+
});
|
|
216
|
+
client.onClose(() => {
|
|
217
|
+
for (const remote of this.remotes.values())
|
|
218
|
+
remote[end]({ error: 'the host went away' });
|
|
219
|
+
this.remotes.clear();
|
|
220
|
+
void this.closeBackendServer();
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
// a window's page asking for its backend: a token for one connection
|
|
224
|
+
async connectWindow({ backend }) {
|
|
225
|
+
if (!this.backends.has(backend))
|
|
226
|
+
throw new Error('that window\'s backend is gone');
|
|
227
|
+
const server = await (this.backendServer ??= this.startBackendServer());
|
|
228
|
+
const token = randomBytes(24).toString('base64url');
|
|
229
|
+
this.tokens.set(token, backend);
|
|
230
|
+
return { url: getWebSocketUrl(server), token };
|
|
231
|
+
}
|
|
232
|
+
// one loopback WebSocket server for every window's backend. Anything on
|
|
233
|
+
// this machine can reach it, so a connection gets in only with a token
|
|
234
|
+
// from connectWindow - used once, and only for the window it was made for.
|
|
235
|
+
async startBackendServer() {
|
|
236
|
+
const server = new WebSocketServer({ host: '127.0.0.1', port: 0 });
|
|
237
|
+
server.on('connection', (socket, request) => {
|
|
238
|
+
const token = new URL(request.url ?? '/', 'ws://backend').searchParams.get('token') ?? '';
|
|
239
|
+
const key = this.tokens.get(token);
|
|
240
|
+
this.tokens.delete(token);
|
|
241
|
+
const backend = key === undefined ? undefined : this.backends.get(key);
|
|
242
|
+
if (!backend)
|
|
243
|
+
return socket.close(1008, 'not a token for an open window');
|
|
244
|
+
backend(createWebSocket(socket)).listen();
|
|
245
|
+
});
|
|
246
|
+
return waitForWebSocketListening(server);
|
|
247
|
+
}
|
|
248
|
+
async closeBackendServer() {
|
|
249
|
+
const server = await this.backendServer;
|
|
250
|
+
this.backendServer = undefined;
|
|
251
|
+
if (!server)
|
|
252
|
+
return;
|
|
253
|
+
for (const socket of server.clients)
|
|
254
|
+
socket.terminate();
|
|
255
|
+
await closeWebSocketServer(server).catch(() => { });
|
|
256
|
+
}
|
|
257
|
+
// a window that's gone takes its backend, and any tokens for it, with it
|
|
258
|
+
forgetBackend(key) {
|
|
259
|
+
this.backends.delete(key);
|
|
260
|
+
for (const [token, owner] of this.tokens)
|
|
261
|
+
if (owner === key)
|
|
262
|
+
this.tokens.delete(token);
|
|
263
|
+
}
|
|
264
|
+
// starts ui-runtime's host, detached, for it to report where it can be
|
|
265
|
+
// reached: its own address when it gets the app's single-instance lock, or
|
|
266
|
+
// the lock holder's when it doesn't. Everything it needs goes through its
|
|
267
|
+
// env - the app (CMDLESS_APP), the root (CMDLESS_ROOT) and where to report
|
|
268
|
+
// (CMDLESS_SERVICE_SOCKET).
|
|
269
|
+
static async create({ app, cmdlessRoot, timeoutMs } = {}) {
|
|
270
|
+
const sdkVersion = await getCmdlessUIVersion();
|
|
271
|
+
const { binPath, env } = await resolvePackageBin(import.meta, '@cmdless/ui-runtime', sdkVersion, cmdlessRoot);
|
|
272
|
+
if (app)
|
|
273
|
+
setCmdlessApp(app, env);
|
|
274
|
+
const { value } = await waitForService(process.execPath, [binPath, 'host'], { env, timeoutMs });
|
|
275
|
+
const client = await connectSocket(uiHostProtocol, { path: value.address });
|
|
276
|
+
client.listen();
|
|
277
|
+
try {
|
|
278
|
+
return new UIHost(client, await client.request.init({ sdkVersion }));
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
client.dispose();
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// starts or joins a host, shows one window on it, and hands back the
|
|
286
|
+
// value it ends with - leaving the host once it has
|
|
287
|
+
static showWindow(params, options) {
|
|
288
|
+
return UIHost.once(options, host => host.openWindow(params));
|
|
289
|
+
}
|
|
290
|
+
// the same for a dialog, handing back what was chosen
|
|
291
|
+
static showDialog(params, options) {
|
|
292
|
+
return UIHost.once(options, host => host.openDialog(params));
|
|
293
|
+
}
|
|
294
|
+
// the same for a card, handing back the Action.Submit chosen on it - or
|
|
295
|
+
// nothing, if it was closed
|
|
296
|
+
static showCard(params, options) {
|
|
297
|
+
return UIHost.once(options, host => host.openCard(params));
|
|
298
|
+
}
|
|
299
|
+
// asks an elicitation's question as a card, handing back its answer -
|
|
300
|
+
// cancel, if the card was closed
|
|
301
|
+
static async showElicitation(request, options) {
|
|
302
|
+
return elicitationResult(await UIHost.showCard({ card: elicitationCard(request), window: request.window }, options));
|
|
303
|
+
}
|
|
304
|
+
// asks a permission request as a card, handing back its outcome -
|
|
305
|
+
// cancelled, if the card was closed
|
|
306
|
+
static async showPermission(request, options) {
|
|
307
|
+
return permissionResult(await UIHost.showCard({ card: permissionCard(request), window: request.window }, options));
|
|
308
|
+
}
|
|
309
|
+
// the same for a form, handing back what it was submitted with - or
|
|
310
|
+
// nothing, if it was cancelled
|
|
311
|
+
static showForm(params, options) {
|
|
312
|
+
return UIHost.once(options, host => host.openForm(params));
|
|
313
|
+
}
|
|
314
|
+
// the same for what a factory opens, handing back what it ends with
|
|
315
|
+
static show(factory, params, options) {
|
|
316
|
+
return UIHost.once(options, host => factory(host, params));
|
|
317
|
+
}
|
|
318
|
+
static async once(options, open) {
|
|
319
|
+
const host = await UIHost.create(options);
|
|
320
|
+
try {
|
|
321
|
+
return await (await open(host)).closed;
|
|
322
|
+
}
|
|
323
|
+
finally {
|
|
324
|
+
host.dispose();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// opens what a factory opens, on this host
|
|
328
|
+
open(factory, params) {
|
|
329
|
+
return factory(this, params);
|
|
330
|
+
}
|
|
331
|
+
// opens a window, once the host has - with a backend, its page can
|
|
332
|
+
// connect() to this process directly, the host only introducing them
|
|
333
|
+
async openWindow({ backend, ...params }) {
|
|
334
|
+
if (!backend) {
|
|
335
|
+
const { id } = await this.client.request.windowOpen(params);
|
|
336
|
+
return this.track(new RemoteWindow(this, id));
|
|
337
|
+
}
|
|
338
|
+
// the backend is here, under a key of its own, before the host hears of
|
|
339
|
+
// the window - and goes when the window does, however it ends
|
|
340
|
+
const key = randomBytes(12).toString('base64url');
|
|
341
|
+
this.backends.set(key, backend);
|
|
342
|
+
try {
|
|
343
|
+
const { id } = await this.client.request.windowOpen({ ...params, backend: key });
|
|
344
|
+
const window = this.track(new RemoteWindow(this, id));
|
|
345
|
+
void window.closed.catch(() => { }).finally(() => this.forgetBackend(key));
|
|
346
|
+
return window;
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
this.forgetBackend(key);
|
|
350
|
+
throw error;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// shows a dialog, once the host has. On macOS, as in Electron, one with no
|
|
354
|
+
// parent holds up the whole host until it's answered, and can't be closed
|
|
355
|
+
// from here - open it from a window (RemoteHandle.openDialog) to avoid that.
|
|
356
|
+
async openDialog(params) {
|
|
357
|
+
const { id } = await this.client.request.dialogOpen(params);
|
|
358
|
+
return this.track(new RemoteDialog(this, id));
|
|
359
|
+
}
|
|
360
|
+
// shows an OS notification, once the host has
|
|
361
|
+
async showNotification(params) {
|
|
362
|
+
const { id } = await this.client.request.notificationShow(params);
|
|
363
|
+
return this.track(new RemoteNotification(this, id));
|
|
364
|
+
}
|
|
365
|
+
// puts an icon in the tray, once the host has - it stays until it's closed
|
|
366
|
+
async createTray(params) {
|
|
367
|
+
const { id } = await this.client.request.trayCreate(params);
|
|
368
|
+
return this.track(new RemoteTray(this, id));
|
|
369
|
+
}
|
|
370
|
+
// opens a window showing an Adaptive Card, once the host has
|
|
371
|
+
async openCard(params) {
|
|
372
|
+
const { id } = await this.client.request.cardOpen(params);
|
|
373
|
+
return this.track(new RemoteCard(this, id));
|
|
374
|
+
}
|
|
375
|
+
// opens a window showing a form, once the host has
|
|
376
|
+
async openForm(params) {
|
|
377
|
+
const { id } = await this.client.request.formOpen(params);
|
|
378
|
+
return this.track(new RemoteForm(this, id));
|
|
379
|
+
}
|
|
380
|
+
track(remote) {
|
|
381
|
+
const ended = this.unclaimed.get(remote.id);
|
|
382
|
+
this.unclaimed.delete(remote.id);
|
|
383
|
+
if (ended)
|
|
384
|
+
remote[end](ended);
|
|
385
|
+
else
|
|
386
|
+
this.remotes.set(remote.id, remote);
|
|
387
|
+
return remote;
|
|
388
|
+
}
|
|
389
|
+
onClose(listener) {
|
|
390
|
+
return this.client.onClose(listener);
|
|
391
|
+
}
|
|
392
|
+
// leaves the host - which, for its owner, ends it
|
|
393
|
+
dispose() {
|
|
394
|
+
this.client.dispose();
|
|
395
|
+
void this.closeBackendServer();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
//# sourceMappingURL=host.js.map
|