@one2x/medeo-plugin-sdk 0.0.0-stage → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -2
- package/dist/generated.d.ts +591 -0
- package/dist/generated.js +1 -0
- package/dist/index.d.ts +1320 -0
- package/dist/index.js +541 -0
- package/dist/react.d.ts +46 -0
- package/dist/react.js +44 -0
- package/dist/registry.d.ts +1349 -0
- package/dist/registry.js +203 -0
- package/dist/rpc.d.ts +28 -0
- package/dist/rpc.js +172 -0
- package/dist/testing.d.ts +43 -0
- package/dist/testing.js +190 -0
- package/dist/ui/styles.d.ts +4 -0
- package/dist/ui/styles.js +31 -0
- package/dist/ui.d.ts +50 -0
- package/dist/ui.js +115 -0
- package/package.json +66 -4
- package/src/generated.ts +231 -0
- package/src/index.ts +658 -0
- package/src/react.ts +50 -0
- package/src/registry.ts +204 -0
- package/src/rpc.ts +136 -0
- package/src/testing.ts +169 -0
- package/src/ui/styles.ts +31 -0
- package/src/ui.ts +102 -0
package/src/react.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** React is only imported by the optional @one2x/medeo-plugin-sdk/ui/react entry. */
|
|
2
|
+
import { createElement as h, useEffect, type ReactNode, type KeyboardEvent } from 'react';
|
|
3
|
+
import { segmentTarget, segmentTabIndex, type CardOptions, type PillOptions, type ToggleOptions, type SegmentedOptions, type ListOptions } from './ui.js';
|
|
4
|
+
import { installStyles } from './ui/styles.js';
|
|
5
|
+
export { uiStyles, installStyles } from './ui/styles.js';
|
|
6
|
+
export type { CardOptions, PillOptions, ToggleOptions, SegmentedOptions, ListOptions, Segment } from './ui.js';
|
|
7
|
+
|
|
8
|
+
/** Call once near your root. SSR-safe; style installation is idempotent. */
|
|
9
|
+
export function useUIStyles(): void { useEffect(() => { installStyles(); }, []); }
|
|
10
|
+
export function Card({ title, description, testid, children }: CardOptions & { children?: ReactNode }) {
|
|
11
|
+
return h('section', { className: 'medeo-ui medeo-card', 'data-testid': testid, 'aria-label': title },
|
|
12
|
+
h('h2', { 'data-testid': `${testid}.title` }, title),
|
|
13
|
+
description !== undefined && h('p', { className: 'medeo-description' }, description), children);
|
|
14
|
+
}
|
|
15
|
+
export function Pill({ label, tone = 'info', testid }: PillOptions) {
|
|
16
|
+
return h('span', { className: 'medeo-ui medeo-pill', 'data-testid': testid, 'data-tone': tone }, label);
|
|
17
|
+
}
|
|
18
|
+
/** Controlled: update checked in onChange. Native input supplies name and Space handling. */
|
|
19
|
+
export function Toggle({ label, checked, disabled, onChange, testid }: ToggleOptions) {
|
|
20
|
+
return h('label', { className: 'medeo-ui medeo-toggle', 'data-testid': `${testid}.root` },
|
|
21
|
+
h('input', { type: 'checkbox', role: 'switch', checked, disabled, 'data-testid': testid, onChange: (event: React.ChangeEvent<HTMLInputElement>) => onChange(event.currentTarget.checked) }),
|
|
22
|
+
h('span', null, label));
|
|
23
|
+
}
|
|
24
|
+
/** Controlled radio group with a single tab stop and arrow/Home/End navigation. */
|
|
25
|
+
export function Segmented<T extends string>({ label, value, options, disabled, onChange, testid }: SegmentedOptions<T>) {
|
|
26
|
+
return h('div', { className: 'medeo-ui medeo-segmented', role: 'radiogroup', 'aria-label': label, 'data-testid': testid },
|
|
27
|
+
options.map((option, index) => h('button', {
|
|
28
|
+
key: option.value, type: 'button', role: 'radio', 'aria-checked': option.value === value,
|
|
29
|
+
'data-testid': `${testid}.${index}`, disabled: disabled || option.disabled,
|
|
30
|
+
tabIndex: disabled ? -1 : segmentTabIndex(options, value, index),
|
|
31
|
+
onClick: () => { if (!disabled && !option.disabled) onChange(option.value); },
|
|
32
|
+
onKeyDown: (event: KeyboardEvent<HTMLButtonElement>) => {
|
|
33
|
+
if (disabled) return;
|
|
34
|
+
const group = event.currentTarget.parentElement!;
|
|
35
|
+
const target = segmentTarget(options, index, event.key, getComputedStyle(group).direction === 'rtl');
|
|
36
|
+
if (target === undefined) return;
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
(group.children[target] as HTMLButtonElement).focus();
|
|
39
|
+
onChange(options[target].value);
|
|
40
|
+
},
|
|
41
|
+
}, option.label)));
|
|
42
|
+
}
|
|
43
|
+
export function List({ label, testid, items }: ListOptions & { items: readonly { key: string; content: ReactNode }[] }) {
|
|
44
|
+
return h('ul', { className: 'medeo-ui medeo-list', 'data-testid': testid, 'aria-label': label },
|
|
45
|
+
items.map(item => h('li', { key: item.key, 'data-testid': `${testid}.${item.key}` }, item.content)));
|
|
46
|
+
}
|
|
47
|
+
export function EmptyState({ title, description, testid }: CardOptions) {
|
|
48
|
+
return h('section', { className: 'medeo-ui medeo-empty', 'data-testid': testid, 'aria-label': title },
|
|
49
|
+
h('h2', null, title), description !== undefined && h('p', { className: 'medeo-description' }, description));
|
|
50
|
+
}
|
package/src/registry.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Generated by platform-core from the capability registry; do not edit.
|
|
2
|
+
export const METHODS = {
|
|
3
|
+
"capabilities.list": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
4
|
+
"capabilities.labels": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
5
|
+
"providers.list": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
6
|
+
"permissions.request": { permission: null, provider: null, ui: true, confirm: true, adapter: "host" },
|
|
7
|
+
"i18n.state": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
8
|
+
"i18n.setLocale": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
9
|
+
"i18n.t": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
10
|
+
"i18n.catalog": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
11
|
+
"operations.state": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
12
|
+
"operations.list": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
13
|
+
"operations.cancel": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
14
|
+
"operations.release": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
15
|
+
"config.get": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
16
|
+
"config.set": { permission: null, provider: null, ui: false, confirm: false, adapter: "host" },
|
|
17
|
+
"storage.get": { permission: "storage", provider: "medeo.storage", ui: false, confirm: false, adapter: "host" },
|
|
18
|
+
"storage.set": { permission: "storage", provider: "medeo.storage", ui: false, confirm: false, adapter: "host" },
|
|
19
|
+
"storage.remove": { permission: "storage", provider: "medeo.storage", ui: false, confirm: false, adapter: "host" },
|
|
20
|
+
"creation.open": { permission: "creation_open", provider: "medeo.creation", ui: false, confirm: false, adapter: "host" },
|
|
21
|
+
"resources.choose_directories": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "host" },
|
|
22
|
+
"resources.request_directory": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: true, adapter: "host" },
|
|
23
|
+
"resources.list": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
24
|
+
"resources.revoke": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "host" },
|
|
25
|
+
"files.roots": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
26
|
+
"files.mkdir": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
27
|
+
"files.history": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
28
|
+
"files.history_page": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
29
|
+
"files.backup_preview": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
30
|
+
"files.archive_backup": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: true, adapter: "host" },
|
|
31
|
+
"files.forget_preview": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
32
|
+
"files.forget_record": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: true, adapter: "host" },
|
|
33
|
+
"files.restore": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
34
|
+
"files.list": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
35
|
+
"files.stat": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
36
|
+
"files.read_text": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
37
|
+
"files.write_text": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
38
|
+
"files.move": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
39
|
+
"files.trash": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: true, adapter: "host" },
|
|
40
|
+
"files.trash_preview": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
41
|
+
"files.discard_preview": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
42
|
+
"files.watch": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
43
|
+
"files.events": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
44
|
+
"files.watches": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
45
|
+
"files.unwatch": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
46
|
+
"files.chooseFiles": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "host" },
|
|
47
|
+
"files.import": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
48
|
+
"files.export": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "host" },
|
|
49
|
+
"files.open": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "electron" },
|
|
50
|
+
"files.reveal": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "electron" },
|
|
51
|
+
"files.startDrag": { permission: "file_access", provider: "medeo.workspace", ui: true, confirm: false, adapter: "electron" },
|
|
52
|
+
"files.read_link": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
53
|
+
"files.measure": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
54
|
+
"files.list_tree": { permission: "file_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
55
|
+
"files.trash_direct": { permission: "file_full_access", provider: "medeo.workspace", ui: false, confirm: false, adapter: "host" },
|
|
56
|
+
"network.request": { permission: "network", provider: "medeo.network", ui: false, confirm: false, adapter: "host" },
|
|
57
|
+
"network.read": { permission: "network", provider: "medeo.network", ui: false, confirm: false, adapter: "host" },
|
|
58
|
+
"network.origins": { permission: "network", provider: "medeo.network", ui: false, confirm: false, adapter: "host" },
|
|
59
|
+
"network.request_origin": { permission: "network", provider: "medeo.network", ui: true, confirm: true, adapter: "host" },
|
|
60
|
+
"network.revoke_origin": { permission: "network", provider: "medeo.network", ui: true, confirm: false, adapter: "host" },
|
|
61
|
+
"agent.models": { permission: "agent_api", provider: "medeo.agent", ui: false, confirm: false, adapter: "host" },
|
|
62
|
+
"agent.start": { permission: "agent_api", provider: "medeo.agent", ui: false, confirm: false, adapter: "host" },
|
|
63
|
+
"drive.state": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
64
|
+
"drive.folders": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
65
|
+
"drive.transfers": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
66
|
+
"drive.recent": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
67
|
+
"drive.failed": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
68
|
+
"drive.fileStates": { permission: "drive_read", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
69
|
+
"drive.requestFolder": { permission: "drive_read", provider: "medeo.drive", ui: true, confirm: true, adapter: "host" },
|
|
70
|
+
"drive.retry": { permission: "drive_manage", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
71
|
+
"drive.download": { permission: "drive_manage", provider: "medeo.drive", ui: false, confirm: false, adapter: "electron" },
|
|
72
|
+
"clipboard.read": { permission: "clipboard_read", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
73
|
+
"clipboard.write": { permission: "clipboard_write", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
74
|
+
"capture.status": { permission: "screen_capture", provider: "medeo.capture", ui: false, confirm: false, adapter: "host" },
|
|
75
|
+
"capture.sources": { permission: "screen_capture", provider: "medeo.capture", ui: true, confirm: false, adapter: "host" },
|
|
76
|
+
"capture.start": { permission: "screen_capture", provider: "medeo.capture", ui: true, confirm: true, adapter: "host" },
|
|
77
|
+
"capture.state": { permission: "screen_capture", provider: "medeo.capture", ui: false, confirm: false, adapter: "host" },
|
|
78
|
+
"capture.stop": { permission: "screen_capture", provider: "medeo.capture", ui: false, confirm: false, adapter: "host" },
|
|
79
|
+
"capture.cancel": { permission: "screen_capture", provider: "medeo.capture", ui: false, confirm: false, adapter: "host" },
|
|
80
|
+
"capture.open": { permission: "screen_capture", provider: "medeo.capture", ui: true, confirm: false, adapter: "electron" },
|
|
81
|
+
"capture.recover": { permission: "screen_capture", provider: "medeo.capture", ui: true, confirm: false, adapter: "host" },
|
|
82
|
+
"camera.status": { permission: "camera", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
83
|
+
"camera.start": { permission: "camera", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
84
|
+
"camera.stop": { permission: "camera", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
85
|
+
"camera.state": { permission: "camera", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
86
|
+
"camera.read": { permission: "camera", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
87
|
+
"microphone.status": { permission: "microphone", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
88
|
+
"microphone.start": { permission: "microphone", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
89
|
+
"microphone.stop": { permission: "microphone", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
90
|
+
"microphone.state": { permission: "microphone", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
91
|
+
"microphone.read": { permission: "microphone", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
92
|
+
"system_audio.status": { permission: "system_audio", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
93
|
+
"system_audio.start": { permission: "system_audio", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
94
|
+
"system_audio.stop": { permission: "system_audio", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
95
|
+
"system_audio.state": { permission: "system_audio", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
96
|
+
"system_audio.read": { permission: "system_audio", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
97
|
+
"applications.list": { permission: "applications", provider: "medeo.applications", ui: false, confirm: false, adapter: "host" },
|
|
98
|
+
"applications.launch": { permission: "applications", provider: "medeo.applications", ui: false, confirm: false, adapter: "host" },
|
|
99
|
+
"applications.state": { permission: "applications", provider: "medeo.applications", ui: false, confirm: false, adapter: "host" },
|
|
100
|
+
"applications.icons": { permission: "applications", provider: "medeo.applications", ui: false, confirm: false, adapter: "host" },
|
|
101
|
+
"plugins.commands": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "host" },
|
|
102
|
+
"plugins.run": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
103
|
+
"plugins.open": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
104
|
+
"plugins.center": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
105
|
+
"plugins.suggestInstall": { permission: "plugin_commands", provider: "medeo.plugins", ui: true, confirm: false, adapter: "electron" },
|
|
106
|
+
"launcher.search": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
107
|
+
"launcher.act": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
108
|
+
"launcher.answer": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
109
|
+
"launcher.events": { permission: "plugin_commands", provider: "medeo.plugins", ui: false, confirm: false, adapter: "electron" },
|
|
110
|
+
"shortcuts.register": { permission: "global_shortcuts", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
111
|
+
"shortcuts.unregister": { permission: "global_shortcuts", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
112
|
+
"shortcuts.events": { permission: "global_shortcuts", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
113
|
+
"shortcuts.list": { permission: "global_shortcuts", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
114
|
+
"notifications.show": { permission: "notifications", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
115
|
+
"notifications.close": { permission: "notifications", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
116
|
+
"tray.set": { permission: "tray", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
117
|
+
"tray.remove": { permission: "tray", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
118
|
+
"tray.events": { permission: "tray", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
119
|
+
"windows.open": { permission: "app_windows", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
120
|
+
"windows.close": { permission: "app_windows", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
121
|
+
"windows.focus": { permission: "app_windows", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
122
|
+
"windows.hide": { permission: "app_windows", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
123
|
+
"windows.list": { permission: "app_windows", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
124
|
+
"accessibility.status": { permission: "accessibility", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
125
|
+
"accessibility.request": { permission: "accessibility", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
126
|
+
"accessibility.windows": { permission: "accessibility", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
127
|
+
"accessibility.focus": { permission: "accessibility", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
128
|
+
"accessibility.selected_text": { permission: "accessibility", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
129
|
+
"accessibility.insert_text": { permission: "accessibility", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
130
|
+
"window_management.status": { permission: "window_management", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
131
|
+
"window_management.request": { permission: "window_management", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
132
|
+
"window_management.focused": { permission: "window_management", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
133
|
+
"window_management.set_frame": { permission: "window_management", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
134
|
+
"window_management.screens": { permission: "window_management", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
135
|
+
"window_management.pointer": { permission: "window_management", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
136
|
+
"input_method.status": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
137
|
+
"input_method.configure": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
138
|
+
"input_method.enable": { permission: "input_method", provider: "medeo.input-method", ui: true, confirm: false, adapter: "host" },
|
|
139
|
+
"input_method.disable": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
140
|
+
"input_method.events": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
141
|
+
"input_method.respond": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
142
|
+
"input_method.system": { permission: "input_method", provider: "medeo.input-method", ui: false, confirm: false, adapter: "host" },
|
|
143
|
+
"input_method.setup": { permission: "input_method", provider: "medeo.input-method", ui: true, confirm: true, adapter: "host" },
|
|
144
|
+
"input_method.remove": { permission: "input_method", provider: "medeo.input-method", ui: true, confirm: true, adapter: "host" },
|
|
145
|
+
"input_method.settings": { permission: "input_method", provider: "medeo.input-method", ui: true, confirm: false, adapter: "host" },
|
|
146
|
+
"external.open": { permission: "external_open", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
147
|
+
"browser.open": { permission: "browser_view", provider: "medeo.desktop", ui: true, confirm: false, adapter: "electron" },
|
|
148
|
+
"browser.close": { permission: "browser_view", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
149
|
+
"browser.state": { permission: "browser_view", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
150
|
+
"system.info": { permission: "system_info", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
151
|
+
"system.running": { permission: "system_info", provider: "medeo.desktop", ui: false, confirm: false, adapter: "host" },
|
|
152
|
+
"secrets.get": { permission: "secrets", provider: "medeo.secrets", ui: false, confirm: false, adapter: "host" },
|
|
153
|
+
"secrets.set": { permission: "secrets", provider: "medeo.secrets", ui: false, confirm: false, adapter: "host" },
|
|
154
|
+
"secrets.remove": { permission: "secrets", provider: "medeo.secrets", ui: false, confirm: false, adapter: "host" },
|
|
155
|
+
"secrets.result": { permission: "secrets", provider: "medeo.secrets", ui: false, confirm: false, adapter: "host" },
|
|
156
|
+
"background.schedule": { permission: "background_tasks", provider: "medeo.background", ui: false, confirm: false, adapter: "host" },
|
|
157
|
+
"background.list": { permission: "background_tasks", provider: "medeo.background", ui: false, confirm: false, adapter: "host" },
|
|
158
|
+
"background.cancel": { permission: "background_tasks", provider: "medeo.background", ui: false, confirm: false, adapter: "host" },
|
|
159
|
+
"background.events": { permission: "background_tasks", provider: "medeo.background", ui: false, confirm: false, adapter: "host" },
|
|
160
|
+
"power.start": { permission: "power", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
161
|
+
"power.stop": { permission: "power", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
162
|
+
"power.state": { permission: "power", provider: "medeo.desktop", ui: false, confirm: false, adapter: "electron" },
|
|
163
|
+
"process.start": { permission: "process_spawn", provider: "medeo.process", ui: false, confirm: false, adapter: "host" },
|
|
164
|
+
"process.state": { permission: "process_spawn", provider: "medeo.process", ui: false, confirm: false, adapter: "host" },
|
|
165
|
+
"process.write": { permission: "process_spawn", provider: "medeo.process", ui: false, confirm: false, adapter: "host" },
|
|
166
|
+
"process.stop": { permission: "process_spawn", provider: "medeo.process", ui: false, confirm: false, adapter: "host" },
|
|
167
|
+
"system.trash": { permission: "node", provider: "medeo.node", ui: false, confirm: false, adapter: "electron" },
|
|
168
|
+
"approvals.request": { permission: "approvals", provider: "medeo.approvals", ui: true, confirm: true, adapter: "host" },
|
|
169
|
+
"approvals.rules": { permission: "approvals", provider: "medeo.approvals", ui: false, confirm: false, adapter: "host" },
|
|
170
|
+
"approvals.forget": { permission: "approvals", provider: "medeo.approvals", ui: false, confirm: false, adapter: "host" },
|
|
171
|
+
"server.start": { permission: "local_server", provider: "medeo.local-server", ui: false, confirm: false, adapter: "host" },
|
|
172
|
+
"server.state": { permission: "local_server", provider: "medeo.local-server", ui: false, confirm: false, adapter: "host" },
|
|
173
|
+
"server.stop": { permission: "local_server", provider: "medeo.local-server", ui: false, confirm: false, adapter: "host" },
|
|
174
|
+
"server.events": { permission: "local_server", provider: "medeo.local-server", ui: false, confirm: false, adapter: "host" },
|
|
175
|
+
"server.respond": { permission: "local_server", provider: "medeo.local-server", ui: false, confirm: false, adapter: "host" },
|
|
176
|
+
"printing.print": { permission: "printing", provider: "medeo.desktop", ui: true, confirm: false, adapter: "electron" },
|
|
177
|
+
"contacts.status": { permission: "contacts", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
178
|
+
"contacts.request": { permission: "contacts", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
179
|
+
"contacts.list": { permission: "contacts", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
180
|
+
"contacts.save": { permission: "contacts", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
181
|
+
"calendar.status": { permission: "calendar", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
182
|
+
"calendar.request": { permission: "calendar", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
183
|
+
"calendar.list": { permission: "calendar", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
184
|
+
"calendar.save": { permission: "calendar", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
185
|
+
"location.status": { permission: "location", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
186
|
+
"location.request": { permission: "location", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
187
|
+
"location.current": { permission: "location", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
188
|
+
"devices.choose": { permission: "device_access", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
189
|
+
"devices.open": { permission: "device_access", provider: "medeo.system-services", ui: true, confirm: false, adapter: "host" },
|
|
190
|
+
"devices.read": { permission: "device_access", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
191
|
+
"devices.write": { permission: "device_access", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
192
|
+
"devices.close": { permission: "device_access", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
193
|
+
"devices.list": { permission: "device_access", provider: "medeo.system-services", ui: false, confirm: false, adapter: "host" },
|
|
194
|
+
} as const;
|
|
195
|
+
export type MethodName = keyof typeof METHODS;
|
|
196
|
+
export const LIMITS = {
|
|
197
|
+
notifications: 16,
|
|
198
|
+
power: 8,
|
|
199
|
+
shortcuts: 16,
|
|
200
|
+
windows: 8,
|
|
201
|
+
browser_windows: 4,
|
|
202
|
+
media_streams: 4,
|
|
203
|
+
queued_events: 64,
|
|
204
|
+
} as const;
|
package/src/rpc.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { channel, type Disposable } from './index.ts';
|
|
2
|
+
|
|
3
|
+
/** A transport is injectable for offline tests; the default is this plugin's own channel. */
|
|
4
|
+
export interface RpcTransport {
|
|
5
|
+
post(message: unknown): Promise<void>;
|
|
6
|
+
onMessage(listener: (message: unknown) => void): () => void;
|
|
7
|
+
}
|
|
8
|
+
export interface RpcOptions { transport?: RpcTransport; timeoutMs?: number; signal?: AbortSignal }
|
|
9
|
+
type Handler = (...args: any[]) => unknown;
|
|
10
|
+
export type WorkerApiClient<T> = { [K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<Awaited<R>> : never } & { $dispose(): void };
|
|
11
|
+
const PROTOCOL = 'medeo.rpc.v1';
|
|
12
|
+
const MAX_PENDING = 64;
|
|
13
|
+
const MAX_BYTES = 60 * 1024;
|
|
14
|
+
interface Packet { protocol: typeof PROTOCOL; kind: 'request' | 'result' | 'error'; client: string; id: string; method?: string; args?: unknown[]; value?: unknown; error?: string }
|
|
15
|
+
function packet(value: unknown): value is Packet {
|
|
16
|
+
if (!value || typeof value !== 'object') return false;
|
|
17
|
+
const p = value as Packet;
|
|
18
|
+
return p.protocol === PROTOCOL && ['request', 'result', 'error'].includes(p.kind) && typeof p.client === 'string' && p.client.length <= 80 && typeof p.id === 'string' && p.id.length <= 80;
|
|
19
|
+
}
|
|
20
|
+
/** Reject values JSON would silently drop or change, rather than corrupting an RPC argument. */
|
|
21
|
+
function checked<T>(value: T): T {
|
|
22
|
+
const seen = new Set<object>();
|
|
23
|
+
function visit(v: unknown): void {
|
|
24
|
+
if (v === null || typeof v === 'string' || typeof v === 'boolean') return;
|
|
25
|
+
if (typeof v === 'number' && Number.isFinite(v)) return;
|
|
26
|
+
if (!v || typeof v !== 'object' || (!Array.isArray(v) && Object.getPrototypeOf(v) !== Object.prototype && Object.getPrototypeOf(v) !== null) || seen.has(v)) throw new Error('rpc_invalid_json');
|
|
27
|
+
seen.add(v);
|
|
28
|
+
const array = Array.isArray(v);
|
|
29
|
+
if (array && Object.keys(v).length !== v.length) throw new Error('rpc_invalid_json');
|
|
30
|
+
for (const key of Reflect.ownKeys(v)) {
|
|
31
|
+
if (array && key === 'length') continue;
|
|
32
|
+
const descriptor = Object.getOwnPropertyDescriptor(v, key)!;
|
|
33
|
+
if (typeof key !== 'string' || !descriptor.enumerable || !('value' in descriptor) ||
|
|
34
|
+
(array && (!/^(0|[1-9]\d*)$/.test(key) || Number(key) >= v.length))) throw new Error('rpc_invalid_json');
|
|
35
|
+
visit(descriptor.value);
|
|
36
|
+
}
|
|
37
|
+
seen.delete(v);
|
|
38
|
+
}
|
|
39
|
+
visit(value);
|
|
40
|
+
const json = JSON.stringify(value);
|
|
41
|
+
if (new TextEncoder().encode(json).length > MAX_BYTES) throw new Error('rpc_message_too_large');
|
|
42
|
+
return JSON.parse(json) as T;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Register in activate, then push the returned disposable into context.subscriptions.
|
|
46
|
+
* Requests only reach this plugin's worker; this is not a cross-plugin service API.
|
|
47
|
+
*/
|
|
48
|
+
export function defineWorkerApi<T extends { [K in keyof T]: Handler }>(handlers: T, options: Pick<RpcOptions, 'transport'> = {}): Disposable {
|
|
49
|
+
for (const name of ['then', '$dispose']) {
|
|
50
|
+
if (Object.hasOwn(handlers, name)) throw new Error(`rpc_reserved_method: ${name}`);
|
|
51
|
+
}
|
|
52
|
+
const transport = options.transport ?? channel;
|
|
53
|
+
const active = new Set<string>();
|
|
54
|
+
let stopped = false;
|
|
55
|
+
const respond = (p: Packet) => {
|
|
56
|
+
if (stopped) return;
|
|
57
|
+
const message = checked(p);
|
|
58
|
+
void Promise.resolve().then(() => { if (!stopped) return transport.post(message); }).catch(() => {});
|
|
59
|
+
};
|
|
60
|
+
const off = transport.onMessage(message => {
|
|
61
|
+
if (stopped || !packet(message) || message.kind !== 'request') return;
|
|
62
|
+
const key = JSON.stringify([message.client, message.id]);
|
|
63
|
+
if (active.has(key)) return;
|
|
64
|
+
const reply = { protocol: PROTOCOL, client: message.client, id: message.id } as const;
|
|
65
|
+
if (active.size >= MAX_PENDING) { respond({ ...reply, kind: 'error', error: 'rpc_busy' }); return; }
|
|
66
|
+
if (typeof message.method !== 'string' || !Object.hasOwn(handlers, message.method) || typeof handlers[message.method as keyof T] !== 'function' || !Array.isArray(message.args)) {
|
|
67
|
+
respond({ ...reply, kind: 'error', error: 'rpc_method_not_found' }); return;
|
|
68
|
+
}
|
|
69
|
+
active.add(key);
|
|
70
|
+
void Promise.resolve().then(() => {
|
|
71
|
+
if (stopped) return;
|
|
72
|
+
const request = checked(message);
|
|
73
|
+
return handlers[request.method as keyof T](...request.args!);
|
|
74
|
+
}).then(value => {
|
|
75
|
+
respond({ ...reply, kind: 'result', value: checked(value === undefined ? null : value) });
|
|
76
|
+
}).catch(error => {
|
|
77
|
+
let detail = 'rpc_failed';
|
|
78
|
+
try { detail = String(error instanceof Error ? error.message : error).slice(0, 1000); } catch {}
|
|
79
|
+
respond({ ...reply, kind: 'error', error: detail });
|
|
80
|
+
}).finally(() => active.delete(key));
|
|
81
|
+
});
|
|
82
|
+
return { dispose() { stopped = true; off(); active.clear(); } };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Create once per window, dispose on unmount. Timeout/disposal cancels waiting,
|
|
86
|
+
* not a worker side effect that already started. All arguments/results are JSON.
|
|
87
|
+
*/
|
|
88
|
+
export function useWorkerApi<T>(options: RpcOptions = {}): WorkerApiClient<T> {
|
|
89
|
+
const transport = options.transport ?? channel;
|
|
90
|
+
const timeoutMs = options.timeoutMs ?? 30_000;
|
|
91
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0 || timeoutMs > 300_000) throw new Error('rpc_invalid_timeout');
|
|
92
|
+
const client = globalThis.crypto.randomUUID();
|
|
93
|
+
let sequence = 0, stopped = false;
|
|
94
|
+
const pending = new Map<string, { resolve(value: unknown): void; reject(error: Error): void; timer: ReturnType<typeof setTimeout> }>();
|
|
95
|
+
const settle = (id: string, value: unknown, error?: string) => {
|
|
96
|
+
const request = pending.get(id); if (!request) return;
|
|
97
|
+
pending.delete(id); clearTimeout(request.timer);
|
|
98
|
+
if (error !== undefined) request.reject(new Error(error)); else request.resolve(value);
|
|
99
|
+
};
|
|
100
|
+
const off = transport.onMessage(message => {
|
|
101
|
+
if (!packet(message) || message.client !== client || message.kind === 'request') return;
|
|
102
|
+
try {
|
|
103
|
+
const response = checked(message);
|
|
104
|
+
if (response.kind === 'error') settle(response.id, null, typeof response.error === 'string' ? response.error : 'rpc_failed');
|
|
105
|
+
else settle(response.id, checked(response.value));
|
|
106
|
+
} catch (error) { settle(message.id, null, error instanceof Error ? error.message : 'rpc_invalid_json'); }
|
|
107
|
+
});
|
|
108
|
+
const dispose = () => {
|
|
109
|
+
if (stopped) return;
|
|
110
|
+
stopped = true; off(); options.signal?.removeEventListener('abort', dispose);
|
|
111
|
+
for (const id of pending.keys()) settle(id, null, 'rpc_disposed');
|
|
112
|
+
};
|
|
113
|
+
options.signal?.addEventListener('abort', dispose, { once: true });
|
|
114
|
+
if (options.signal?.aborted) dispose();
|
|
115
|
+
return new Proxy(Object.create(null), {
|
|
116
|
+
get(_target, method) {
|
|
117
|
+
if (method === '$dispose') return dispose;
|
|
118
|
+
if (method === 'then' || typeof method !== 'string') return undefined;
|
|
119
|
+
return (...args: unknown[]) => {
|
|
120
|
+
if (stopped) return Promise.reject(new Error('rpc_disposed'));
|
|
121
|
+
if (pending.size >= MAX_PENDING) return Promise.reject(new Error('rpc_busy'));
|
|
122
|
+
const id = String(++sequence);
|
|
123
|
+
let message: Packet;
|
|
124
|
+
try { message = checked({ protocol: PROTOCOL, kind: 'request', client, id, method, args }); }
|
|
125
|
+
catch (error) { return Promise.reject(error); }
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
const timer = setTimeout(() => settle(id, null, 'rpc_timeout'), timeoutMs);
|
|
128
|
+
pending.set(id, { resolve, reject, timer });
|
|
129
|
+
void Promise.resolve().then(() => {
|
|
130
|
+
if (!stopped && pending.has(id)) return transport.post(message);
|
|
131
|
+
}).catch(error => settle(id, null, error instanceof Error ? error.message : 'rpc_send_failed'));
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
}) as WorkerApiClient<T>;
|
|
136
|
+
}
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { PluginAPI, PluginContext, PlatformEvent, PluginLocaleState } from './index.ts';
|
|
2
|
+
import { METHODS } from './registry.ts';
|
|
3
|
+
|
|
4
|
+
export type FakeHandler = (args: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
5
|
+
type CommandHandler = Parameters<PluginContext['commands']['register']>[1];
|
|
6
|
+
export interface FakeHostOptions {
|
|
7
|
+
storage?: Record<string, unknown>;
|
|
8
|
+
/** Declared configuration keys and their initial/default values. */
|
|
9
|
+
config?: Record<string, unknown>;
|
|
10
|
+
handlers?: Record<string, FakeHandler>;
|
|
11
|
+
commands?: Record<string, CommandHandler>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Preserve descriptors as well as values, including an originally absent global.
|
|
15
|
+
// Removed nested installations are skipped so out-of-order cleanup cannot revive one.
|
|
16
|
+
type Installation = { api: PluginAPI; previous?: PropertyDescriptor; removed: boolean };
|
|
17
|
+
const installations: Installation[] = [];
|
|
18
|
+
function installAPI(api: PluginAPI): () => void {
|
|
19
|
+
const entry: Installation = { api, previous: Object.getOwnPropertyDescriptor(globalThis, 'medeoPlugin'), removed: false };
|
|
20
|
+
Object.defineProperty(globalThis, 'medeoPlugin', { configurable: true, writable: true, value: api });
|
|
21
|
+
installations.push(entry);
|
|
22
|
+
return () => {
|
|
23
|
+
if (entry.removed) return;
|
|
24
|
+
entry.removed = true;
|
|
25
|
+
while (installations.at(-1)?.removed) {
|
|
26
|
+
const top = installations.pop()!;
|
|
27
|
+
// Never overwrite a bridge installed by unrelated test code.
|
|
28
|
+
if (Object.getOwnPropertyDescriptor(globalThis, 'medeoPlugin')?.value !== top.api) continue;
|
|
29
|
+
if (top.previous) Object.defineProperty(globalThis, 'medeoPlugin', top.previous);
|
|
30
|
+
else Reflect.deleteProperty(globalThis, 'medeoPlugin');
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Offline bridge for real SDK calls. No OS, permissions, persistence or manifest
|
|
36
|
+
* validation is simulated. Channel messages loop back to this host's listeners.
|
|
37
|
+
* Install only within a serial test scope (globals cannot isolate concurrent tests).
|
|
38
|
+
* Every unimplemented capability fails unless supplied in `handlers`.
|
|
39
|
+
*/
|
|
40
|
+
export function createFakeHost(options: FakeHostOptions = {}) {
|
|
41
|
+
const clone = <T>(value: T): T => structuredClone(value);
|
|
42
|
+
const storage = new Map(Object.entries(clone(options.storage ?? {})));
|
|
43
|
+
const config = new Map(Object.entries(clone(options.config ?? {})));
|
|
44
|
+
const handlers = new Map(Object.entries(options.handlers ?? {}));
|
|
45
|
+
const commands = new Map<string, CommandHandler>();
|
|
46
|
+
const messages = new Set<(message: unknown) => void>();
|
|
47
|
+
const events = new Set<(event: PlatformEvent) => void>();
|
|
48
|
+
const locales = new Set<(state: PluginLocaleState) => void>();
|
|
49
|
+
const restores = new Set<() => void>();
|
|
50
|
+
// Medeo Launcher Protocol: the provider the plugin registered, and views its commands pushed.
|
|
51
|
+
let launcherHandler: ((request: { op: string; payload: any }) => unknown) | null = null;
|
|
52
|
+
const launcherViews: { session: string; op: 'update' | 'prompt'; view: unknown }[] = [];
|
|
53
|
+
let launcherAnswer: (view: unknown) => unknown = () => ({ confirmed: true });
|
|
54
|
+
let disposed = false;
|
|
55
|
+
const assertActive = () => { if (disposed) throw new Error('fake_host_disposed'); };
|
|
56
|
+
const subscribe = <T>(listeners: Set<(value: T) => void>, callback: (value: T) => void) => {
|
|
57
|
+
assertActive(); listeners.add(callback); return () => { listeners.delete(callback); };
|
|
58
|
+
};
|
|
59
|
+
const emit = <T>(listeners: Set<(value: T) => void>, value: T) => {
|
|
60
|
+
assertActive(); for (const listener of [...listeners]) listener(clone(value));
|
|
61
|
+
};
|
|
62
|
+
const key = (args: Record<string, unknown>) => {
|
|
63
|
+
if (typeof args.key !== 'string') throw new Error('invalid_key');
|
|
64
|
+
return args.key;
|
|
65
|
+
};
|
|
66
|
+
const builtins: Record<string, FakeHandler> = {
|
|
67
|
+
'storage.get': args => clone(storage.get(key(args)) ?? null),
|
|
68
|
+
'storage.set': args => { storage.set(key(args), clone(args.value)); return null; },
|
|
69
|
+
'storage.remove': args => { storage.delete(key(args)); return null; },
|
|
70
|
+
'config.get': args => {
|
|
71
|
+
if (args.key === undefined) return Object.fromEntries(config);
|
|
72
|
+
const name = key(args);
|
|
73
|
+
if (!config.has(name)) throw new Error(`unknown_configuration: ${name}`);
|
|
74
|
+
return config.get(name);
|
|
75
|
+
},
|
|
76
|
+
'config.set': args => {
|
|
77
|
+
const name = key(args);
|
|
78
|
+
if (!config.has(name)) throw new Error(`unknown_configuration: ${name}`);
|
|
79
|
+
config.set(name, clone(args.value));
|
|
80
|
+
emit(events, { type: 'signal', channel: 'config' });
|
|
81
|
+
return null;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
const api: PluginAPI = {
|
|
85
|
+
async invoke(method, args = {}) {
|
|
86
|
+
assertActive();
|
|
87
|
+
const handler = handlers.get(method) ?? (Object.hasOwn(builtins, method) ? builtins[method] : undefined);
|
|
88
|
+
if (!handler) throw new Error(`${Object.hasOwn(METHODS, method) ? 'unimplemented_method' : 'unknown_method'}: ${method}`);
|
|
89
|
+
return clone(await handler(clone(args)));
|
|
90
|
+
},
|
|
91
|
+
async run(command, args) {
|
|
92
|
+
assertActive();
|
|
93
|
+
const handler = commands.get(command);
|
|
94
|
+
if (!handler) throw new Error(`unregistered_command: ${command}`);
|
|
95
|
+
return clone(await handler(clone(args)));
|
|
96
|
+
},
|
|
97
|
+
async post(message) { emit(messages, message); },
|
|
98
|
+
onMessage: callback => subscribe(messages, callback),
|
|
99
|
+
onEvent: callback => subscribe(events, callback),
|
|
100
|
+
onLocaleChanged: callback => subscribe(locales, callback),
|
|
101
|
+
onLauncher: handler => { assertActive(); launcherHandler = handler; return () => { if (launcherHandler === handler) launcherHandler = null; }; },
|
|
102
|
+
async launcher(op, session, view) {
|
|
103
|
+
assertActive(); launcherViews.push(clone({ session, op, view }));
|
|
104
|
+
return op === 'prompt' ? clone(await launcherAnswer(view)) : null;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
const context: PluginContext = {
|
|
108
|
+
subscriptions: [],
|
|
109
|
+
commands: {
|
|
110
|
+
register(id, handler) {
|
|
111
|
+
assertActive();
|
|
112
|
+
if (commands.has(id)) throw new Error(`duplicate_command: ${id}`);
|
|
113
|
+
commands.set(id, handler);
|
|
114
|
+
const disposable = { dispose() { if (commands.get(id) === handler) commands.delete(id); } };
|
|
115
|
+
context.subscriptions.push(disposable);
|
|
116
|
+
return disposable;
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
for (const [id, handler] of Object.entries(options.commands ?? {})) context.commands.register(id, handler);
|
|
121
|
+
return {
|
|
122
|
+
api, context,
|
|
123
|
+
/** Explicit installation: importing this module never changes the global. */
|
|
124
|
+
install() {
|
|
125
|
+
assertActive();
|
|
126
|
+
const restore = installAPI(api);
|
|
127
|
+
const off = () => { restore(); restores.delete(off); };
|
|
128
|
+
restores.add(off);
|
|
129
|
+
return off;
|
|
130
|
+
},
|
|
131
|
+
emitEvent: (event: PlatformEvent) => emit(events, event),
|
|
132
|
+
emitMessage: (message: unknown) => emit(messages, message),
|
|
133
|
+
emitLocaleChanged: (state: PluginLocaleState) => emit(locales, state),
|
|
134
|
+
/** What the plugin's `launcher.provide` answers, as a launcher would ask. */
|
|
135
|
+
async launcherSearch(query: string, options: { scoped?: boolean; limit?: number } = {}) {
|
|
136
|
+
if (!launcherHandler) throw new Error('no_launcher_provider');
|
|
137
|
+
return clone(await launcherHandler({ op: 'search', payload: { query, scoped: options.scoped ?? false, limit: options.limit ?? 20 } }));
|
|
138
|
+
},
|
|
139
|
+
async launcherAct(item: string, action: string, query = '') {
|
|
140
|
+
if (!launcherHandler) throw new Error('no_launcher_provider');
|
|
141
|
+
return clone(await launcherHandler({ op: 'action', payload: { item, action, query } }));
|
|
142
|
+
},
|
|
143
|
+
/** Runs a command as a launcher does (with a session); its views are in `launcherViews`. */
|
|
144
|
+
runFromLauncher(command: string, args: Record<string, unknown> = {}, session = 'test-session') {
|
|
145
|
+
return api.run(command, { ...args, launcher: { session } } as never);
|
|
146
|
+
},
|
|
147
|
+
launcherViews,
|
|
148
|
+
/** How prompts are answered (default: confirmed). */
|
|
149
|
+
answerPrompts(answer: (view: unknown) => unknown) { launcherAnswer = answer; },
|
|
150
|
+
async dispose() {
|
|
151
|
+
if (disposed) return;
|
|
152
|
+
disposed = true;
|
|
153
|
+
for (const restore of [...restores].reverse()) restore();
|
|
154
|
+
messages.clear(); events.clear(); locales.clear(); commands.clear();
|
|
155
|
+
const errors: unknown[] = [];
|
|
156
|
+
for (const subscription of context.subscriptions.splice(0).reverse()) {
|
|
157
|
+
try { await subscription.dispose(); } catch (error) { errors.push(error); }
|
|
158
|
+
}
|
|
159
|
+
if (errors.length) throw new AggregateError(errors, 'fake_host_cleanup_failed');
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Installs a bridge for a callback and restores it even when the callback throws. */
|
|
165
|
+
export async function withFakeHost<T>(options: FakeHostOptions, run: (host: ReturnType<typeof createFakeHost>) => T | Promise<T>): Promise<T> {
|
|
166
|
+
const host = createFakeHost(options);
|
|
167
|
+
host.install();
|
|
168
|
+
try { return await run(host); } finally { await host.dispose(); }
|
|
169
|
+
}
|
package/src/ui/styles.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Layout only: colors, type and dimensions remain owned by /_medeo/theme.css. */
|
|
2
|
+
export const uiStyles = `
|
|
3
|
+
.medeo-ui{font-family:var(--font-family-plain);color:var(--color-surface-on-surface);font-size:var(--type-label-large-size);line-height:var(--type-label-large-leading)}
|
|
4
|
+
.medeo-card{padding:var(--space-s5);border:1px solid var(--color-surface-outline-variant);border-radius:var(--shape-radius-16);background:var(--color-surface-surface-container-lowest);display:grid;gap:var(--space-s3)}
|
|
5
|
+
.medeo-card h2,.medeo-empty h2{margin:0;font-size:var(--type-title-medium-size);line-height:var(--type-title-medium-leading);font-weight:var(--type-title-medium-weight);letter-spacing:var(--type-title-medium-tracking)}
|
|
6
|
+
.medeo-description{margin:0;color:var(--color-surface-on-surface-variant)}
|
|
7
|
+
.medeo-pill{display:inline-flex;align-items:center;justify-self:start;padding:var(--space-s1) var(--space-s3);border-radius:var(--shape-radius-full);background:var(--medeo-selected);font-size:var(--type-label-medium-size);line-height:var(--type-label-medium-leading);font-weight:var(--type-label-medium-prominent-weight)}
|
|
8
|
+
.medeo-pill[data-tone=error]{color:var(--color-schemes-error);background:var(--medeo-error-wash)}
|
|
9
|
+
.medeo-pill[data-tone=neutral]{background:var(--medeo-hover)}
|
|
10
|
+
.medeo-toggle{display:inline-flex;align-items:center;gap:var(--space-s2);min-height:var(--space-s8);cursor:pointer}
|
|
11
|
+
.medeo-toggle input{width:var(--space-s4);height:var(--space-s4);accent-color:var(--color-schemes-primary)}
|
|
12
|
+
.medeo-toggle:has(input:disabled){opacity:.45;cursor:default}
|
|
13
|
+
.medeo-segmented{display:flex;flex-wrap:wrap;gap:var(--space-s1);padding:var(--space-s1);border:1px solid var(--color-surface-outline-variant);border-radius:var(--shape-radius-12)}
|
|
14
|
+
.medeo-segmented button{font:inherit;color:inherit;border:0;border-radius:var(--shape-radius-8);background:transparent;min-height:var(--space-s8);padding:var(--space-s2) var(--space-s3);cursor:pointer}
|
|
15
|
+
.medeo-segmented button[aria-checked=true]{background:var(--color-schemes-primary);color:var(--color-schemes-on-primary)}
|
|
16
|
+
.medeo-segmented button:hover:not(:disabled):not([aria-checked=true]){background:var(--medeo-hover)}
|
|
17
|
+
.medeo-segmented button:disabled{opacity:.45;cursor:default}
|
|
18
|
+
.medeo-ui :focus-visible{outline:2px solid var(--color-schemes-primary);outline-offset:2px}
|
|
19
|
+
.medeo-list{list-style:none;margin:0;padding:0;display:grid;gap:var(--space-s2)}
|
|
20
|
+
.medeo-list>li{padding:var(--space-s3);border-bottom:1px solid var(--color-surface-outline-variant)}
|
|
21
|
+
.medeo-empty{display:grid;gap:var(--space-s2);padding:var(--space-s7);text-align:center;border:1px dashed var(--color-surface-outline);border-radius:var(--shape-radius-12)}
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
/** Explicit and idempotent; importing this module has no DOM side effects. */
|
|
25
|
+
export function installStyles(doc: Document = document): void {
|
|
26
|
+
if (doc.querySelector('style[data-medeo-ui]')) return;
|
|
27
|
+
const style = doc.createElement('style');
|
|
28
|
+
style.dataset.medeoUi = '';
|
|
29
|
+
style.textContent = uiStyles;
|
|
30
|
+
doc.head.append(style);
|
|
31
|
+
}
|