@rollrook-ai/sdk 0.0.0-stage → 0.1.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/dist/agents.d.ts +9 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +13 -0
- package/dist/agents.js.map +1 -0
- package/dist/assets.d.ts +35 -0
- package/dist/assets.d.ts.map +1 -0
- package/dist/assets.js +66 -0
- package/dist/assets.js.map +1 -0
- package/dist/assistant.d.ts +76 -0
- package/dist/assistant.d.ts.map +1 -0
- package/dist/assistant.js +161 -0
- package/dist/assistant.js.map +1 -0
- package/dist/audio.d.ts +55 -0
- package/dist/audio.d.ts.map +1 -0
- package/dist/audio.js +82 -0
- package/dist/audio.js.map +1 -0
- package/dist/bridge.d.ts +51 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +78 -0
- package/dist/bridge.js.map +1 -0
- package/dist/calendar.d.ts +33 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +63 -0
- package/dist/calendar.js.map +1 -0
- package/dist/context.d.ts +19 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +25 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +74 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +111 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +36 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +81 -0
- package/dist/events.js.map +1 -0
- package/dist/files.d.ts +29 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +43 -0
- package/dist/files.js.map +1 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/dist/mock/index.d.ts +4 -0
- package/dist/mock/index.d.ts.map +1 -0
- package/dist/mock/index.js +3 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/mock/mock-host.d.ts +255 -0
- package/dist/mock/mock-host.d.ts.map +1 -0
- package/dist/mock/mock-host.js +1274 -0
- package/dist/mock/mock-host.js.map +1 -0
- package/dist/mock/samples.d.ts +56 -0
- package/dist/mock/samples.d.ts.map +1 -0
- package/dist/mock/samples.js +283 -0
- package/dist/mock/samples.js.map +1 -0
- package/dist/mock/toolbar.d.ts +95 -0
- package/dist/mock/toolbar.d.ts.map +1 -0
- package/dist/mock/toolbar.js +279 -0
- package/dist/mock/toolbar.js.map +1 -0
- package/dist/net.d.ts +13 -0
- package/dist/net.d.ts.map +1 -0
- package/dist/net.js +26 -0
- package/dist/net.js.map +1 -0
- package/dist/plugin.d.ts +11 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +13 -0
- package/dist/plugin.js.map +1 -0
- package/dist/raven.d.ts +51 -0
- package/dist/raven.d.ts.map +1 -0
- package/dist/raven.js +100 -0
- package/dist/raven.js.map +1 -0
- package/dist/segment-display.d.ts +93 -0
- package/dist/segment-display.d.ts.map +1 -0
- package/dist/segment-display.js +324 -0
- package/dist/segment-display.js.map +1 -0
- package/dist/settings.d.ts +19 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +27 -0
- package/dist/settings.js.map +1 -0
- package/dist/storage.d.ts +28 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +54 -0
- package/dist/storage.js.map +1 -0
- package/dist/system.d.ts +19 -0
- package/dist/system.d.ts.map +1 -0
- package/dist/system.js +24 -0
- package/dist/system.js.map +1 -0
- package/dist/theme.d.ts +51 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +85 -0
- package/dist/theme.js.map +1 -0
- package/dist/types.d.ts +278 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui.d.ts +57 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +78 -0
- package/dist/ui.js.map +1 -0
- package/dist/usage.d.ts +14 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +19 -0
- package/dist/usage.js.map +1 -0
- package/dist/video.d.ts +11 -0
- package/dist/video.d.ts.map +1 -0
- package/dist/video.js +17 -0
- package/dist/video.js.map +1 -0
- package/dist/wire.d.ts +214 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +538 -0
- package/dist/wire.js.map +1 -0
- package/package.json +39 -4
- package/src/agents.ts +14 -0
- package/src/assets.ts +80 -0
- package/src/assistant.ts +185 -0
- package/src/audio.ts +90 -0
- package/src/bridge.ts +96 -0
- package/src/calendar.ts +68 -0
- package/src/context.ts +28 -0
- package/src/errors.ts +131 -0
- package/src/events.ts +90 -0
- package/src/files.ts +47 -0
- package/src/index.ts +152 -0
- package/src/mock/index.ts +3 -0
- package/src/mock/mock-host.ts +1408 -0
- package/src/mock/samples.ts +300 -0
- package/src/mock/toolbar.ts +321 -0
- package/src/net.ts +27 -0
- package/src/plugin.ts +14 -0
- package/src/raven.ts +116 -0
- package/src/segment-display.ts +377 -0
- package/src/settings.ts +29 -0
- package/src/storage.ts +59 -0
- package/src/system.ts +26 -0
- package/src/theme.ts +97 -0
- package/src/types.ts +327 -0
- package/src/ui.ts +88 -0
- package/src/usage.ts +21 -0
- package/src/video.ts +17 -0
- package/src/wire.ts +608 -0
- package/theme.css +407 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1274 @@
|
|
|
1
|
+
import { AssetsApi } from "../assets.js";
|
|
2
|
+
import { AssistantApi } from "../assistant.js";
|
|
3
|
+
import { Bridge } from "../bridge.js";
|
|
4
|
+
import { CalendarApi } from "../calendar.js";
|
|
5
|
+
import { ContextApi } from "../context.js";
|
|
6
|
+
import { Events } from "../events.js";
|
|
7
|
+
import { StorageApi } from "../storage.js";
|
|
8
|
+
import { ThemeApi } from "../theme.js";
|
|
9
|
+
import { Wire } from "../wire.js";
|
|
10
|
+
import { MockSamples } from "./samples.js";
|
|
11
|
+
import { MockToolbar } from "./toolbar.js";
|
|
12
|
+
/**
|
|
13
|
+
* A stand-in for RollRook in an ordinary browser, so a plugin can be built
|
|
14
|
+
* with `vite dev`. It installs the same bridge RollRook does
|
|
15
|
+
* (`window.webkit.messageHandlers.rollrook`, `window.__rollrook`, the theme's
|
|
16
|
+
* CSS variables) and answers every method: storage, files and settings live
|
|
17
|
+
* in `localStorage`, Raven gives canned answers with `raven.turn` events,
|
|
18
|
+
* agents, usage and audio return sample data that moves, popovers open as
|
|
19
|
+
* an overlay, and instances in other frames and tabs of the same origin
|
|
20
|
+
* reach one another through a broadcast channel. A plugin with a background
|
|
21
|
+
* view gets it running in a hidden frame of one open page, which answers
|
|
22
|
+
* assistant tools (`call_tool`) and `calendar.move`; calendar items are kept
|
|
23
|
+
* in `localStorage` and read with `calendar_items`.
|
|
24
|
+
*/
|
|
25
|
+
export class MockHost {
|
|
26
|
+
/** The prefix of every `localStorage` key the mock writes. */
|
|
27
|
+
static STORAGE_PREFIX = "rollrook-mock";
|
|
28
|
+
/** The `localStorage` key that remembers the theme the toolbar switched to. */
|
|
29
|
+
static THEME_KEY = "rollrook-mock/theme";
|
|
30
|
+
/** The `localStorage` key that remembers whether the high-contrast theme is on. */
|
|
31
|
+
static CONTRAST_KEY = "rollrook-mock/high-contrast";
|
|
32
|
+
/** The query parameter naming the view a page is. */
|
|
33
|
+
static VIEW_PARAMETER = "rr-view";
|
|
34
|
+
/** The id of the `<style>` holding the theme's variables, as RollRook names it. */
|
|
35
|
+
static THEME_STYLE_ID = "rollrook-theme";
|
|
36
|
+
/** The permissions every plugin has without asking. */
|
|
37
|
+
static LIST_DEFAULT_PERMISSIONS = ["context", "storage", "files", "settings", "ui", "system", "plugin"];
|
|
38
|
+
/** Raven's default delay before answering. */
|
|
39
|
+
static RAVEN_DELAY_MS = 900;
|
|
40
|
+
/** How many pieces Raven's answer streams in. */
|
|
41
|
+
static RAVEN_CHUNK_COUNT = 8;
|
|
42
|
+
/** The time between two streamed pieces. */
|
|
43
|
+
static RAVEN_CHUNK_INTERVAL_MS = 90;
|
|
44
|
+
/** The largest total size of the storage values, as the host enforces it. */
|
|
45
|
+
static STORAGE_MAX_BYTES = 5 * 1024 * 1024;
|
|
46
|
+
/** How long a usage read takes in the mock. */
|
|
47
|
+
static USAGE_READ_MS = 1200;
|
|
48
|
+
/** How often the mock reads usage by itself. */
|
|
49
|
+
static USAGE_INTERVAL_MS = 30_000;
|
|
50
|
+
/** How often the meters send levels, about 30 times a second. */
|
|
51
|
+
static METER_INTERVAL_MS = 33;
|
|
52
|
+
/** The message a popover frame posts to its opener to close. */
|
|
53
|
+
static CLOSE_POPOVER_MESSAGE = "rollrook-mock/close-popover";
|
|
54
|
+
/** The lock the page running the background view holds, so only one page of the origin runs it. */
|
|
55
|
+
static BACKGROUND_LOCK_PREFIX = "rollrook-mock/background";
|
|
56
|
+
/** The message a popover or settings view in a frame sends its opener to change its height. */
|
|
57
|
+
static HEIGHT_MESSAGE = "rollrook-mock/height";
|
|
58
|
+
/** A settings view's height when its manifest gives none. */
|
|
59
|
+
static SETTINGS_DEFAULT_HEIGHT = 320;
|
|
60
|
+
/** The width of the sheet a settings view shows in, as Settings' pane. */
|
|
61
|
+
static SETTINGS_SHEET_WIDTH = 520;
|
|
62
|
+
/** The largest a popover or settings view may be. */
|
|
63
|
+
static VIEW_MAX_SIZE = 800;
|
|
64
|
+
plugin_id;
|
|
65
|
+
plugin_version;
|
|
66
|
+
app_version;
|
|
67
|
+
instance_id;
|
|
68
|
+
view;
|
|
69
|
+
list_views;
|
|
70
|
+
map_setting_defaults;
|
|
71
|
+
set_permissions;
|
|
72
|
+
raven_delay_ms;
|
|
73
|
+
map_conversations = new Map();
|
|
74
|
+
set_timeouts = new Set();
|
|
75
|
+
set_intervals = new Set();
|
|
76
|
+
toolbar;
|
|
77
|
+
channel;
|
|
78
|
+
list_tracks;
|
|
79
|
+
on_window_message = (event) => this.receive_window_message(event);
|
|
80
|
+
popover = undefined;
|
|
81
|
+
meter_timer = undefined;
|
|
82
|
+
background_frame = undefined;
|
|
83
|
+
release_background = undefined;
|
|
84
|
+
is_dark;
|
|
85
|
+
is_high_contrast;
|
|
86
|
+
is_shown = true;
|
|
87
|
+
is_audio_enabled = true;
|
|
88
|
+
is_usage_refreshing = false;
|
|
89
|
+
usage_read_at = new Date();
|
|
90
|
+
conversation_count = 0;
|
|
91
|
+
reported_width = undefined;
|
|
92
|
+
constructor(options) {
|
|
93
|
+
const manifest = Wire.is_record(options.manifest) ? options.manifest : {};
|
|
94
|
+
this.plugin_id = typeof manifest["id"] === "string" ? manifest["id"] : "dev.plugin";
|
|
95
|
+
this.plugin_version = typeof manifest["version"] === "string" ? manifest["version"] : "0.0.0";
|
|
96
|
+
this.app_version = options.app_version ?? "0.7.0";
|
|
97
|
+
this.instance_id = `mock-${Math.random().toString(36).slice(2, 10)}`;
|
|
98
|
+
this.list_views = MockHost.read_views(manifest);
|
|
99
|
+
this.view = MockHost.pick_view(this.list_views, options.view_id);
|
|
100
|
+
this.map_setting_defaults = MockHost.read_setting_defaults(manifest);
|
|
101
|
+
this.set_permissions = MockHost.granted_permissions(options);
|
|
102
|
+
this.raven_delay_ms = options.raven_delay_ms ?? MockHost.RAVEN_DELAY_MS;
|
|
103
|
+
this.is_dark = options.is_dark ?? MockHost.stored_flag(MockHost.THEME_KEY, "dark") ?? MockHost.prefers_dark();
|
|
104
|
+
this.is_high_contrast = options.is_high_contrast ?? MockHost.stored_flag(MockHost.CONTRAST_KEY, "on") ?? false;
|
|
105
|
+
this.list_tracks = this.sample_tracks();
|
|
106
|
+
this.channel = typeof BroadcastChannel === "function"
|
|
107
|
+
? new BroadcastChannel(`${MockHost.STORAGE_PREFIX}/${this.plugin_id}`)
|
|
108
|
+
: undefined;
|
|
109
|
+
const is_top = typeof window === "undefined" || window.top === window;
|
|
110
|
+
this.toolbar = options.show_toolbar ?? is_top
|
|
111
|
+
? new MockToolbar({
|
|
112
|
+
on_toggle_theme: () => this.toggle_theme(),
|
|
113
|
+
on_toggle_contrast: () => this.toggle_contrast(),
|
|
114
|
+
on_open_settings: this.list_views.some((view) => view.slot === "settings")
|
|
115
|
+
? () => this.open_settings_view()
|
|
116
|
+
: undefined,
|
|
117
|
+
on_toggle_visibility: () => this.show(!this.is_shown),
|
|
118
|
+
})
|
|
119
|
+
: undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Install the mock host on the page, unless RollRook's own bridge is
|
|
123
|
+
* there already.
|
|
124
|
+
*
|
|
125
|
+
* @param options - the manifest, the view, permissions and behaviour
|
|
126
|
+
* @returns the mock host, or undefined inside RollRook
|
|
127
|
+
*/
|
|
128
|
+
static install(options = {}) {
|
|
129
|
+
if (Bridge.is_available()) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
const host = new MockHost(options);
|
|
133
|
+
host.attach();
|
|
134
|
+
return host;
|
|
135
|
+
}
|
|
136
|
+
/** The view this page is, as the mock picked it from the manifest. */
|
|
137
|
+
get current_view() {
|
|
138
|
+
return this.view;
|
|
139
|
+
}
|
|
140
|
+
/** The width a footbar view last reported with `ui.width`, or undefined. */
|
|
141
|
+
get width() {
|
|
142
|
+
return this.reported_width;
|
|
143
|
+
}
|
|
144
|
+
/** Remove the bridge, the theme, the toolbar, the popover and every pending timer. */
|
|
145
|
+
uninstall() {
|
|
146
|
+
for (const timer of this.set_timeouts) {
|
|
147
|
+
clearTimeout(timer);
|
|
148
|
+
}
|
|
149
|
+
for (const timer of this.set_intervals) {
|
|
150
|
+
clearInterval(timer);
|
|
151
|
+
}
|
|
152
|
+
this.set_timeouts.clear();
|
|
153
|
+
this.set_intervals.clear();
|
|
154
|
+
this.stop_meters();
|
|
155
|
+
this.close_popover();
|
|
156
|
+
this.release_background?.();
|
|
157
|
+
this.release_background = undefined;
|
|
158
|
+
this.background_frame?.remove();
|
|
159
|
+
this.background_frame = undefined;
|
|
160
|
+
this.channel?.close();
|
|
161
|
+
window.removeEventListener("message", this.on_window_message);
|
|
162
|
+
this.toolbar?.remove();
|
|
163
|
+
document.getElementById(MockHost.THEME_STYLE_ID)?.remove();
|
|
164
|
+
Reflect.deleteProperty(globalThis, "webkit");
|
|
165
|
+
Reflect.deleteProperty(globalThis, ContextApi.GLOBAL_NAME);
|
|
166
|
+
}
|
|
167
|
+
/** Switch between the light and the dark theme, as the user would in RollRook. */
|
|
168
|
+
toggle_theme() {
|
|
169
|
+
this.is_dark = !this.is_dark;
|
|
170
|
+
localStorage.setItem(MockHost.THEME_KEY, this.is_dark ? "dark" : "light");
|
|
171
|
+
this.change_theme();
|
|
172
|
+
this.channel?.postMessage({ kind: "theme", isDark: this.is_dark, isHighContrast: this.is_high_contrast });
|
|
173
|
+
}
|
|
174
|
+
/** Switch the high-contrast theme on or off. */
|
|
175
|
+
toggle_contrast() {
|
|
176
|
+
this.is_high_contrast = !this.is_high_contrast;
|
|
177
|
+
localStorage.setItem(MockHost.CONTRAST_KEY, this.is_high_contrast ? "on" : "off");
|
|
178
|
+
this.change_theme();
|
|
179
|
+
this.channel?.postMessage({ kind: "theme", isDark: this.is_dark, isHighContrast: this.is_high_contrast });
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Show or hide the view, as RollRook does when the user switches tabs.
|
|
183
|
+
*
|
|
184
|
+
* @param is_shown - whether the view is shown
|
|
185
|
+
*/
|
|
186
|
+
show(is_shown) {
|
|
187
|
+
this.is_shown = is_shown;
|
|
188
|
+
this.toolbar?.render_visibility(is_shown);
|
|
189
|
+
this.emit("visibility", { isShown: is_shown });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Deliver an event through `window.__rollrookEvent`, as RollRook does.
|
|
193
|
+
*
|
|
194
|
+
* @param name - the event's name
|
|
195
|
+
* @param payload - the payload in wire form
|
|
196
|
+
*/
|
|
197
|
+
emit(name, payload) {
|
|
198
|
+
const receiver = Reflect.get(globalThis, Events.RECEIVER_NAME);
|
|
199
|
+
if (typeof receiver === "function") {
|
|
200
|
+
receiver(name, payload);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The items the plugin last published to the calendar.
|
|
205
|
+
*
|
|
206
|
+
* @returns the items in wire form, as RollRook received them
|
|
207
|
+
*/
|
|
208
|
+
calendar_items() {
|
|
209
|
+
const stored = localStorage.getItem(this.calendar_key());
|
|
210
|
+
const value = stored === null ? [] : JSON.parse(stored);
|
|
211
|
+
return Array.isArray(value) ? value.filter((item) => Wire.is_record(item) && Wire.is_json(item)) : [];
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Click one of the plugin's calendar items, as the user would in
|
|
215
|
+
* RollRook: the first dock view receives `calendar.open`, else the first
|
|
216
|
+
* sidebar view, else the background view, in every open page showing it.
|
|
217
|
+
*
|
|
218
|
+
* @param id - the item's id
|
|
219
|
+
*/
|
|
220
|
+
open_calendar_item(id) {
|
|
221
|
+
const payload = { id };
|
|
222
|
+
this.channel?.postMessage({ kind: "calendar.open", payload });
|
|
223
|
+
if (this.receives_calendar_open()) {
|
|
224
|
+
this.emit("calendar.open", payload);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Drag one of the plugin's calendar items to new times, as the user
|
|
229
|
+
* would in RollRook: the background view receives `calendar.move`, or
|
|
230
|
+
* every view when the plugin has none.
|
|
231
|
+
*
|
|
232
|
+
* @param move - the item's id and its new times, ISO 8601
|
|
233
|
+
*/
|
|
234
|
+
move_calendar_item(move) {
|
|
235
|
+
const payload = { id: move.id };
|
|
236
|
+
for (const [key, value] of Object.entries({ start: move.start, end: move.end, due: move.due })) {
|
|
237
|
+
if (value !== undefined) {
|
|
238
|
+
payload[key] = value;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
this.channel?.postMessage({ kind: "calendar.move", payload });
|
|
242
|
+
if (this.receives_calendar_move()) {
|
|
243
|
+
this.emit("calendar.move", payload);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Call one of the plugin's assistant tools, as RollRook's assistant
|
|
248
|
+
* does, in the background view this page runs or is.
|
|
249
|
+
*
|
|
250
|
+
* @param name - the tool's name
|
|
251
|
+
* @param input - its input
|
|
252
|
+
* @param phase - `preview` or `run`
|
|
253
|
+
* @returns what the tool returned
|
|
254
|
+
* @throws Error when no background view runs in this page, or as the tool fails
|
|
255
|
+
*/
|
|
256
|
+
async call_tool(name, input, phase = "run") {
|
|
257
|
+
const target = this.view.slot === "background"
|
|
258
|
+
? globalThis
|
|
259
|
+
: this.background_frame?.contentWindow;
|
|
260
|
+
const receiver = target === null || target === undefined
|
|
261
|
+
? undefined
|
|
262
|
+
: Reflect.get(target, AssistantApi.RECEIVER_NAME);
|
|
263
|
+
if (typeof receiver !== "function") {
|
|
264
|
+
throw new Error("failed: no background view answers tools in this page");
|
|
265
|
+
}
|
|
266
|
+
const result = await receiver(name, input, phase);
|
|
267
|
+
if (!Wire.is_record(result) || !Wire.is_json(result)) {
|
|
268
|
+
throw new Error(`failed: the tool ${name} did not return a JSON object`);
|
|
269
|
+
}
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Answer one bridge message, as RollRook's handler does: resolve with the
|
|
274
|
+
* result or reject with an error whose message begins with its code.
|
|
275
|
+
*
|
|
276
|
+
* @param message - the posted message
|
|
277
|
+
* @returns the method's result
|
|
278
|
+
*/
|
|
279
|
+
async handle(message) {
|
|
280
|
+
const record = Wire.is_record(message) ? message : {};
|
|
281
|
+
const method = record["method"];
|
|
282
|
+
const params = Wire.is_record(record["params"]) ? record["params"] : {};
|
|
283
|
+
if (typeof method !== "string") {
|
|
284
|
+
throw new Error("invalid: the message has no method");
|
|
285
|
+
}
|
|
286
|
+
this.check_permission(method, params);
|
|
287
|
+
switch (method) {
|
|
288
|
+
case "context.get":
|
|
289
|
+
return this.context();
|
|
290
|
+
case "storage.get":
|
|
291
|
+
return this.storage_get(params);
|
|
292
|
+
case "storage.set":
|
|
293
|
+
return this.storage_set(params);
|
|
294
|
+
case "storage.delete":
|
|
295
|
+
return this.storage_delete(params);
|
|
296
|
+
case "storage.keys":
|
|
297
|
+
return { keys: this.storage_keys() };
|
|
298
|
+
case "files.read":
|
|
299
|
+
return this.files_read(params);
|
|
300
|
+
case "files.write":
|
|
301
|
+
return this.files_write(params);
|
|
302
|
+
case "files.list":
|
|
303
|
+
return this.files_list(params);
|
|
304
|
+
case "files.delete":
|
|
305
|
+
return this.files_delete(params);
|
|
306
|
+
case "settings.get":
|
|
307
|
+
return { values: this.setting_values() };
|
|
308
|
+
case "settings.set":
|
|
309
|
+
return this.settings_set(params);
|
|
310
|
+
case "ui.toast":
|
|
311
|
+
this.toolbar?.toast(MockHost.string_param(params, "text"));
|
|
312
|
+
return {};
|
|
313
|
+
case "ui.badge":
|
|
314
|
+
this.toolbar?.render_badge(typeof params["text"] === "string" ? params["text"] : undefined);
|
|
315
|
+
return {};
|
|
316
|
+
case "ui.caption":
|
|
317
|
+
this.toolbar?.toast(typeof params["text"] === "string" ? `Caption: ${params["text"]}` : "Caption cleared");
|
|
318
|
+
return {};
|
|
319
|
+
case "ui.height":
|
|
320
|
+
return this.ui_height(params);
|
|
321
|
+
case "system.searchCities":
|
|
322
|
+
return this.search_cities(params);
|
|
323
|
+
case "ui.width":
|
|
324
|
+
return this.ui_width(params);
|
|
325
|
+
case "ui.openView":
|
|
326
|
+
return this.open_view(params);
|
|
327
|
+
case "ui.popover":
|
|
328
|
+
return this.open_popover(params);
|
|
329
|
+
case "ui.closePopover":
|
|
330
|
+
this.request_close_popover();
|
|
331
|
+
return {};
|
|
332
|
+
case "ui.openSettings":
|
|
333
|
+
return this.open_settings(params);
|
|
334
|
+
case "system.openURL":
|
|
335
|
+
return this.open_url(params);
|
|
336
|
+
case "plugin.broadcast":
|
|
337
|
+
return this.broadcast(params);
|
|
338
|
+
case "raven.ask":
|
|
339
|
+
return this.raven_ask(params);
|
|
340
|
+
case "raven.answer":
|
|
341
|
+
return this.raven_answer(params);
|
|
342
|
+
case "raven.open":
|
|
343
|
+
this.conversation(params);
|
|
344
|
+
this.toolbar?.toast("Raven would open this conversation");
|
|
345
|
+
return {};
|
|
346
|
+
case "agents.list":
|
|
347
|
+
return { agents: MockSamples.LIST_AGENTS };
|
|
348
|
+
case "usage.list":
|
|
349
|
+
return this.usage_snapshot();
|
|
350
|
+
case "usage.refresh":
|
|
351
|
+
this.refresh_usage();
|
|
352
|
+
return {};
|
|
353
|
+
case "audio.status":
|
|
354
|
+
return { isEnabled: this.is_audio_enabled, isPermitted: true };
|
|
355
|
+
case "audio.enable":
|
|
356
|
+
return this.audio_enable(params);
|
|
357
|
+
case "audio.requestPermission":
|
|
358
|
+
return { isPermitted: true };
|
|
359
|
+
case "audio.list":
|
|
360
|
+
if (!this.is_audio_enabled) {
|
|
361
|
+
throw new Error("failed: the mixer is off");
|
|
362
|
+
}
|
|
363
|
+
return { tracks: this.list_tracks };
|
|
364
|
+
case "audio.update":
|
|
365
|
+
return this.audio_update(params, false);
|
|
366
|
+
case "audio.reset":
|
|
367
|
+
return this.audio_update(params, true);
|
|
368
|
+
case "audio.meters":
|
|
369
|
+
return this.audio_meters(params);
|
|
370
|
+
case "video.togglePicture":
|
|
371
|
+
MockHost.number_param(params, "sessionId");
|
|
372
|
+
MockHost.number_param(params, "tabId");
|
|
373
|
+
this.toolbar?.toast("RollRook would show the tab's video in the sidebar");
|
|
374
|
+
return {};
|
|
375
|
+
case "net.fetch":
|
|
376
|
+
return this.net_fetch(params);
|
|
377
|
+
case "calendar.publish":
|
|
378
|
+
return this.calendar_publish(params);
|
|
379
|
+
default:
|
|
380
|
+
throw new Error(`unknown: no method ${method}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
attach() {
|
|
384
|
+
const handler = {
|
|
385
|
+
postMessage: (message) => this.handle(message),
|
|
386
|
+
[Bridge.MOCK_MARKER]: true,
|
|
387
|
+
};
|
|
388
|
+
const webkit = Reflect.get(globalThis, "webkit");
|
|
389
|
+
if (Wire.is_record(webkit) && Wire.is_record(webkit["messageHandlers"])) {
|
|
390
|
+
webkit["messageHandlers"][Bridge.HANDLER_NAME] = handler;
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
Reflect.set(globalThis, "webkit", { messageHandlers: { [Bridge.HANDLER_NAME]: handler } });
|
|
394
|
+
}
|
|
395
|
+
Reflect.set(globalThis, ContextApi.GLOBAL_NAME, this.context());
|
|
396
|
+
this.publish_theme();
|
|
397
|
+
this.toolbar?.attach();
|
|
398
|
+
if (this.channel !== undefined) {
|
|
399
|
+
this.channel.onmessage = (event) => this.receive_channel_message(event.data);
|
|
400
|
+
}
|
|
401
|
+
window.addEventListener("message", this.on_window_message);
|
|
402
|
+
if (this.set_permissions === undefined || this.set_permissions.has("agents.read")) {
|
|
403
|
+
this.repeat(() => this.refresh_usage(), MockHost.USAGE_INTERVAL_MS);
|
|
404
|
+
}
|
|
405
|
+
this.start_background();
|
|
406
|
+
}
|
|
407
|
+
start_background() {
|
|
408
|
+
const view = this.list_views.find((candidate) => candidate.slot === "background");
|
|
409
|
+
const is_top = typeof window !== "undefined" && window.top === window;
|
|
410
|
+
if (view === undefined || !is_top || this.view.slot === "background") {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const spawn = () => {
|
|
414
|
+
const url = new URL(view.entry, location.href);
|
|
415
|
+
url.searchParams.set(MockHost.VIEW_PARAMETER, view.id);
|
|
416
|
+
const frame = document.createElement("iframe");
|
|
417
|
+
frame.src = url.href;
|
|
418
|
+
frame.title = "Background view";
|
|
419
|
+
frame.setAttribute("aria-hidden", "true");
|
|
420
|
+
frame.tabIndex = -1;
|
|
421
|
+
frame.style.cssText = "position:fixed;width:0;height:0;border:0;visibility:hidden;pointer-events:none;";
|
|
422
|
+
document.body.append(frame);
|
|
423
|
+
this.background_frame = frame;
|
|
424
|
+
};
|
|
425
|
+
if (typeof navigator === "undefined" || navigator.locks === undefined) {
|
|
426
|
+
spawn();
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const held = new Promise((resolve) => {
|
|
430
|
+
this.release_background = resolve;
|
|
431
|
+
});
|
|
432
|
+
navigator.locks.request(`${MockHost.BACKGROUND_LOCK_PREFIX}/${this.plugin_id}`, () => {
|
|
433
|
+
spawn();
|
|
434
|
+
return held;
|
|
435
|
+
}).catch((err) => console.error(err));
|
|
436
|
+
}
|
|
437
|
+
receives_calendar_open() {
|
|
438
|
+
const opened = this.list_views.find((candidate) => candidate.slot === "dock")
|
|
439
|
+
?? this.list_views.find((candidate) => candidate.slot === "sidebar")
|
|
440
|
+
?? this.list_views.find((candidate) => candidate.slot === "background");
|
|
441
|
+
return opened?.id === this.view.id;
|
|
442
|
+
}
|
|
443
|
+
receives_calendar_move() {
|
|
444
|
+
const has_background = this.list_views.some((candidate) => candidate.slot === "background");
|
|
445
|
+
return has_background ? this.view.slot === "background" : true;
|
|
446
|
+
}
|
|
447
|
+
calendar_key() {
|
|
448
|
+
return `${MockHost.STORAGE_PREFIX}/${this.plugin_id}/calendar`;
|
|
449
|
+
}
|
|
450
|
+
calendar_publish(params) {
|
|
451
|
+
const items = params["items"];
|
|
452
|
+
if (!Array.isArray(items)) {
|
|
453
|
+
throw new Error("invalid: items must be an array");
|
|
454
|
+
}
|
|
455
|
+
if (items.length > CalendarApi.ITEM_LIMIT) {
|
|
456
|
+
throw new Error(`invalid: calendar.publish takes at most ${CalendarApi.ITEM_LIMIT} items`);
|
|
457
|
+
}
|
|
458
|
+
const list_items = [];
|
|
459
|
+
const set_ids = new Set();
|
|
460
|
+
for (const item of items) {
|
|
461
|
+
if (!Wire.is_record(item) || !Wire.is_json(item) || typeof item["id"] !== "string" || typeof item["title"] !== "string") {
|
|
462
|
+
throw new Error("invalid: each item has an id and a title");
|
|
463
|
+
}
|
|
464
|
+
const placed = {
|
|
465
|
+
id: item["id"],
|
|
466
|
+
title: item["title"],
|
|
467
|
+
};
|
|
468
|
+
for (const key of ["start", "end", "due"]) {
|
|
469
|
+
const value = item[key];
|
|
470
|
+
if (value === undefined || value === null) {
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (typeof value !== "string" || Number.isNaN(new Date(value).getTime())) {
|
|
474
|
+
throw new Error(`invalid: ${key} of ${item["id"]} is not an ISO 8601 time`);
|
|
475
|
+
}
|
|
476
|
+
placed[key] = value;
|
|
477
|
+
}
|
|
478
|
+
if (!CalendarApi.is_placed(placed)) {
|
|
479
|
+
throw new Error(`invalid: ${item["id"]} needs a start and an end, or a due`);
|
|
480
|
+
}
|
|
481
|
+
if (set_ids.has(item["id"])) {
|
|
482
|
+
throw new Error(`invalid: two items have the id ${item["id"]}`);
|
|
483
|
+
}
|
|
484
|
+
const color = item["color"];
|
|
485
|
+
if (color !== undefined && color !== null && (typeof color !== "string" || !CalendarApi.COLOR_PATTERN.test(color))) {
|
|
486
|
+
throw new Error(`invalid: the item ${item["id"]}'s color is not #rrggbb`);
|
|
487
|
+
}
|
|
488
|
+
set_ids.add(item["id"]);
|
|
489
|
+
list_items.push(item);
|
|
490
|
+
}
|
|
491
|
+
localStorage.setItem(this.calendar_key(), JSON.stringify(list_items));
|
|
492
|
+
return {};
|
|
493
|
+
}
|
|
494
|
+
theme() {
|
|
495
|
+
return MockSamples.theme(this.is_dark, this.is_high_contrast);
|
|
496
|
+
}
|
|
497
|
+
context() {
|
|
498
|
+
return {
|
|
499
|
+
protocol: Bridge.PROTOCOL,
|
|
500
|
+
pluginId: this.plugin_id,
|
|
501
|
+
pluginVersion: this.plugin_version,
|
|
502
|
+
appVersion: this.app_version,
|
|
503
|
+
viewId: this.view.id,
|
|
504
|
+
slot: this.view.slot,
|
|
505
|
+
instanceId: this.instance_id,
|
|
506
|
+
theme: this.theme(),
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
change_theme() {
|
|
510
|
+
this.publish_theme();
|
|
511
|
+
this.emit("theme", this.theme());
|
|
512
|
+
}
|
|
513
|
+
publish_theme() {
|
|
514
|
+
const theme = Wire.theme(this.theme());
|
|
515
|
+
const list_declarations = Object.entries(ThemeApi.map_variables).map(([key, variable]) => `${variable}: ${String(Reflect.get(theme.colors, key))};`);
|
|
516
|
+
list_declarations.push(`${ThemeApi.FONT_VARIABLE}: ${MockSamples.FONT};`);
|
|
517
|
+
list_declarations.push(`color-scheme: ${theme.is_dark ? "dark" : "light"};`);
|
|
518
|
+
let style = document.getElementById(MockHost.THEME_STYLE_ID);
|
|
519
|
+
if (style === null) {
|
|
520
|
+
style = document.createElement("style");
|
|
521
|
+
style.id = MockHost.THEME_STYLE_ID;
|
|
522
|
+
document.head.prepend(style);
|
|
523
|
+
}
|
|
524
|
+
style.textContent = `:root { ${list_declarations.join(" ")} }`;
|
|
525
|
+
const context = Reflect.get(globalThis, ContextApi.GLOBAL_NAME);
|
|
526
|
+
if (Wire.is_record(context)) {
|
|
527
|
+
context["theme"] = this.theme();
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
receive_channel_message(data) {
|
|
531
|
+
if (!Wire.is_record(data)) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (data["kind"] === "theme" && typeof data["isDark"] === "boolean" && typeof data["isHighContrast"] === "boolean") {
|
|
535
|
+
this.is_dark = data["isDark"];
|
|
536
|
+
this.is_high_contrast = data["isHighContrast"];
|
|
537
|
+
this.change_theme();
|
|
538
|
+
}
|
|
539
|
+
else if (data["kind"] === "settings") {
|
|
540
|
+
this.emit("settings.changed", { values: this.setting_values() });
|
|
541
|
+
}
|
|
542
|
+
else if (data["kind"] === "message" && Wire.is_json(data["payload"])) {
|
|
543
|
+
this.emit("message", data["payload"]);
|
|
544
|
+
}
|
|
545
|
+
else if (data["kind"] === "calendar.open" && this.receives_calendar_open() && Wire.is_json(data["payload"])) {
|
|
546
|
+
this.emit("calendar.open", data["payload"]);
|
|
547
|
+
}
|
|
548
|
+
else if (data["kind"] === "calendar.move" && this.receives_calendar_move() && Wire.is_json(data["payload"])) {
|
|
549
|
+
this.emit("calendar.move", data["payload"]);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
receive_window_message(event) {
|
|
553
|
+
if (event.origin !== location.origin) {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (event.data === MockHost.CLOSE_POPOVER_MESSAGE) {
|
|
557
|
+
this.close_popover();
|
|
558
|
+
}
|
|
559
|
+
else if (Wire.is_record(event.data)
|
|
560
|
+
&& event.data["type"] === MockHost.HEIGHT_MESSAGE
|
|
561
|
+
&& typeof event.data["height"] === "number"
|
|
562
|
+
&& this.popover !== undefined) {
|
|
563
|
+
MockToolbar.resize_popover(this.popover, event.data["height"]);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
check_permission(method, params) {
|
|
567
|
+
if (this.set_permissions === undefined) {
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
const map_needed = {
|
|
571
|
+
raven: "raven",
|
|
572
|
+
agents: "agents.read",
|
|
573
|
+
usage: "agents.read",
|
|
574
|
+
audio: "audio",
|
|
575
|
+
video: "video",
|
|
576
|
+
};
|
|
577
|
+
const needed = map_needed[method.split(".")[0] ?? ""];
|
|
578
|
+
if (needed !== undefined && !this.set_permissions.has(needed)) {
|
|
579
|
+
throw new Error(`permission: ${method} needs the ${needed} permission`);
|
|
580
|
+
}
|
|
581
|
+
if (method === "net.fetch") {
|
|
582
|
+
const host = MockHost.url_of(params).hostname;
|
|
583
|
+
if (!this.may_fetch(host)) {
|
|
584
|
+
throw new Error(`permission: net.fetch to ${host} needs the net:${host} permission`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
may_fetch(host) {
|
|
589
|
+
for (const permission of this.set_permissions ?? []) {
|
|
590
|
+
if (!permission.startsWith("net:")) {
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
const pattern = permission.slice("net:".length);
|
|
594
|
+
if (pattern === "*" || pattern === host) {
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
if (pattern.startsWith("*.") && host.endsWith(pattern.slice(1))) {
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
storage_key(key) {
|
|
604
|
+
return `${MockHost.STORAGE_PREFIX}/${this.plugin_id}/storage/${key}`;
|
|
605
|
+
}
|
|
606
|
+
files_key(path) {
|
|
607
|
+
return `${MockHost.STORAGE_PREFIX}/${this.plugin_id}/files/${path}`;
|
|
608
|
+
}
|
|
609
|
+
settings_key() {
|
|
610
|
+
return `${MockHost.STORAGE_PREFIX}/${this.plugin_id}/settings`;
|
|
611
|
+
}
|
|
612
|
+
storage_get(params) {
|
|
613
|
+
const stored = localStorage.getItem(this.storage_key(MockHost.storage_key_param(params)));
|
|
614
|
+
const value = stored === null ? null : JSON.parse(stored);
|
|
615
|
+
return { value: Wire.is_json(value) ? value : null };
|
|
616
|
+
}
|
|
617
|
+
storage_set(params) {
|
|
618
|
+
const key = MockHost.storage_key_param(params);
|
|
619
|
+
const value = params["value"];
|
|
620
|
+
if (value === undefined || !Wire.is_json(value)) {
|
|
621
|
+
throw new Error("invalid: value must be JSON");
|
|
622
|
+
}
|
|
623
|
+
const text = JSON.stringify(value);
|
|
624
|
+
let total = text.length;
|
|
625
|
+
for (const other of this.storage_keys()) {
|
|
626
|
+
if (other !== key) {
|
|
627
|
+
total += localStorage.getItem(this.storage_key(other))?.length ?? 0;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (total > MockHost.STORAGE_MAX_BYTES) {
|
|
631
|
+
throw new Error("failed: storage is full (5 MB)");
|
|
632
|
+
}
|
|
633
|
+
localStorage.setItem(this.storage_key(key), text);
|
|
634
|
+
return {};
|
|
635
|
+
}
|
|
636
|
+
storage_delete(params) {
|
|
637
|
+
localStorage.removeItem(this.storage_key(MockHost.storage_key_param(params)));
|
|
638
|
+
return {};
|
|
639
|
+
}
|
|
640
|
+
storage_keys() {
|
|
641
|
+
const prefix = this.storage_key("");
|
|
642
|
+
return MockHost.local_keys().filter((key) => key.startsWith(prefix)).map((key) => key.slice(prefix.length)).sort();
|
|
643
|
+
}
|
|
644
|
+
files_read(params) {
|
|
645
|
+
const path = MockHost.path_param(params, false);
|
|
646
|
+
const text = localStorage.getItem(this.files_key(path));
|
|
647
|
+
if (text === null) {
|
|
648
|
+
throw new Error(`failed: no file ${path}`);
|
|
649
|
+
}
|
|
650
|
+
return { text };
|
|
651
|
+
}
|
|
652
|
+
files_write(params) {
|
|
653
|
+
const path = MockHost.path_param(params, false);
|
|
654
|
+
if (this.list_paths_under(path).length > 0) {
|
|
655
|
+
throw new Error(`failed: ${path} cannot be written: it is a folder`);
|
|
656
|
+
}
|
|
657
|
+
localStorage.setItem(this.files_key(path), MockHost.string_param(params, "text"));
|
|
658
|
+
return {};
|
|
659
|
+
}
|
|
660
|
+
files_list(params) {
|
|
661
|
+
const path = MockHost.path_param(params, true);
|
|
662
|
+
const list_paths = this.list_paths_under(path);
|
|
663
|
+
if (path !== "" && list_paths.length === 0) {
|
|
664
|
+
throw new Error(`failed: no folder ${path}`);
|
|
665
|
+
}
|
|
666
|
+
const prefix = path === "" ? "" : `${path}/`;
|
|
667
|
+
const map_entries = new Map();
|
|
668
|
+
for (const file_path of list_paths) {
|
|
669
|
+
const rest = file_path.slice(prefix.length);
|
|
670
|
+
const slash = rest.indexOf("/");
|
|
671
|
+
if (slash >= 0) {
|
|
672
|
+
const name = rest.slice(0, slash);
|
|
673
|
+
map_entries.set(name, {
|
|
674
|
+
name,
|
|
675
|
+
isDirectory: true,
|
|
676
|
+
size: 0,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
const text = localStorage.getItem(this.files_key(file_path)) ?? "";
|
|
681
|
+
map_entries.set(rest, {
|
|
682
|
+
name: rest,
|
|
683
|
+
isDirectory: false,
|
|
684
|
+
size: new TextEncoder().encode(text).length,
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
const list_entries = [...map_entries.values()];
|
|
689
|
+
list_entries.sort((a, b) => String(a["name"]).localeCompare(String(b["name"])));
|
|
690
|
+
return { entries: list_entries };
|
|
691
|
+
}
|
|
692
|
+
files_delete(params) {
|
|
693
|
+
const path = MockHost.path_param(params, false);
|
|
694
|
+
const list_paths = localStorage.getItem(this.files_key(path)) === null
|
|
695
|
+
? this.list_paths_under(path)
|
|
696
|
+
: [path];
|
|
697
|
+
if (list_paths.length === 0) {
|
|
698
|
+
throw new Error(`failed: ${path} cannot be deleted`);
|
|
699
|
+
}
|
|
700
|
+
for (const file_path of list_paths) {
|
|
701
|
+
localStorage.removeItem(this.files_key(file_path));
|
|
702
|
+
}
|
|
703
|
+
return {};
|
|
704
|
+
}
|
|
705
|
+
list_paths_under(path) {
|
|
706
|
+
const prefix = this.files_key(path === "" ? "" : `${path}/`);
|
|
707
|
+
const root = this.files_key("");
|
|
708
|
+
return MockHost.local_keys().filter((key) => key.startsWith(prefix)).map((key) => key.slice(root.length));
|
|
709
|
+
}
|
|
710
|
+
stored_settings() {
|
|
711
|
+
const stored = localStorage.getItem(this.settings_key());
|
|
712
|
+
const value = stored === null ? {} : JSON.parse(stored);
|
|
713
|
+
return Wire.is_record(value) && Wire.is_json(value) && !Array.isArray(value) ? { ...value } : {};
|
|
714
|
+
}
|
|
715
|
+
setting_values() {
|
|
716
|
+
return {
|
|
717
|
+
...this.map_setting_defaults,
|
|
718
|
+
...this.stored_settings(),
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
settings_set(params) {
|
|
722
|
+
const key = MockHost.string_param(params, "key");
|
|
723
|
+
const value = params["value"];
|
|
724
|
+
if (!(key in this.map_setting_defaults)) {
|
|
725
|
+
throw new Error(`invalid: the manifest declares no setting ${key}`);
|
|
726
|
+
}
|
|
727
|
+
if (value === undefined || !Wire.is_json(value)) {
|
|
728
|
+
throw new Error(`invalid: the setting ${key} takes JSON`);
|
|
729
|
+
}
|
|
730
|
+
const map_stored = this.stored_settings();
|
|
731
|
+
map_stored[key] = value;
|
|
732
|
+
localStorage.setItem(this.settings_key(), JSON.stringify(map_stored));
|
|
733
|
+
this.emit("settings.changed", { values: this.setting_values() });
|
|
734
|
+
this.channel?.postMessage({ kind: "settings" });
|
|
735
|
+
return {};
|
|
736
|
+
}
|
|
737
|
+
ui_width(params) {
|
|
738
|
+
const width = MockHost.number_param(params, "width");
|
|
739
|
+
if (width <= 0) {
|
|
740
|
+
throw new Error("invalid: a width is a positive number of points");
|
|
741
|
+
}
|
|
742
|
+
this.reported_width = width;
|
|
743
|
+
return {};
|
|
744
|
+
}
|
|
745
|
+
open_view(params) {
|
|
746
|
+
const view = this.view_param(params);
|
|
747
|
+
if (view.slot !== "dock" && view.slot !== "sidebar") {
|
|
748
|
+
throw new Error(`invalid: ${view.id} is not a dock or sidebar view`);
|
|
749
|
+
}
|
|
750
|
+
this.toolbar?.toast(`RollRook would show the view ${view.id}`);
|
|
751
|
+
return {};
|
|
752
|
+
}
|
|
753
|
+
open_popover(params) {
|
|
754
|
+
const view = this.view_param(params);
|
|
755
|
+
if (view.slot !== "popover") {
|
|
756
|
+
throw new Error(`invalid: ${view.id} is not a popover view`);
|
|
757
|
+
}
|
|
758
|
+
this.close_popover();
|
|
759
|
+
const url = new URL(view.entry, location.href);
|
|
760
|
+
url.searchParams.set(MockHost.VIEW_PARAMETER, view.id);
|
|
761
|
+
this.popover = MockToolbar.popover({
|
|
762
|
+
url: url.href,
|
|
763
|
+
width: view.width ?? MockToolbar.POPOVER_DEFAULT_SIZE,
|
|
764
|
+
height: view.height ?? MockToolbar.POPOVER_DEFAULT_SIZE,
|
|
765
|
+
on_dismiss: () => this.close_popover(),
|
|
766
|
+
is_sheet: false,
|
|
767
|
+
});
|
|
768
|
+
return {};
|
|
769
|
+
}
|
|
770
|
+
open_settings_view() {
|
|
771
|
+
const view = this.list_views.find((candidate) => candidate.slot === "settings");
|
|
772
|
+
if (view === undefined) {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
this.close_popover();
|
|
776
|
+
const url = new URL(view.entry, location.href);
|
|
777
|
+
url.searchParams.set(MockHost.VIEW_PARAMETER, view.id);
|
|
778
|
+
this.popover = MockToolbar.popover({
|
|
779
|
+
url: url.href,
|
|
780
|
+
width: MockHost.SETTINGS_SHEET_WIDTH,
|
|
781
|
+
height: view.height ?? MockHost.SETTINGS_DEFAULT_HEIGHT,
|
|
782
|
+
on_dismiss: () => this.close_popover(),
|
|
783
|
+
is_sheet: true,
|
|
784
|
+
});
|
|
785
|
+
return true;
|
|
786
|
+
}
|
|
787
|
+
ui_height(params) {
|
|
788
|
+
const height = MockHost.number_param(params, "height");
|
|
789
|
+
if (height < 1 || height > MockHost.VIEW_MAX_SIZE) {
|
|
790
|
+
throw new Error(`invalid: a height is from 1 to ${MockHost.VIEW_MAX_SIZE} points`);
|
|
791
|
+
}
|
|
792
|
+
if (this.view.slot !== "popover" && this.view.slot !== "settings") {
|
|
793
|
+
throw new Error("invalid: only a popover or a settings view sets its height");
|
|
794
|
+
}
|
|
795
|
+
if (window.parent !== window) {
|
|
796
|
+
window.parent.postMessage({ type: MockHost.HEIGHT_MESSAGE, height }, location.origin);
|
|
797
|
+
}
|
|
798
|
+
return {};
|
|
799
|
+
}
|
|
800
|
+
search_cities(params) {
|
|
801
|
+
const query = params["query"];
|
|
802
|
+
if (typeof query !== "string") {
|
|
803
|
+
throw new Error("invalid: query is a string");
|
|
804
|
+
}
|
|
805
|
+
return { cities: MockSamples.search_cities(query) };
|
|
806
|
+
}
|
|
807
|
+
request_close_popover() {
|
|
808
|
+
if (this.popover !== undefined) {
|
|
809
|
+
this.close_popover();
|
|
810
|
+
}
|
|
811
|
+
else if (window.parent !== window) {
|
|
812
|
+
window.parent.postMessage(MockHost.CLOSE_POPOVER_MESSAGE, location.origin);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
close_popover() {
|
|
816
|
+
const popover = this.popover;
|
|
817
|
+
this.popover = undefined;
|
|
818
|
+
if (popover !== undefined) {
|
|
819
|
+
MockToolbar.dismiss_popover(popover);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
open_settings(params) {
|
|
823
|
+
const pane = params["pane"];
|
|
824
|
+
if (pane !== "plugin" && pane !== "codeAgents") {
|
|
825
|
+
throw new Error("invalid: pane is plugin or codeAgents");
|
|
826
|
+
}
|
|
827
|
+
if (pane === "plugin" && window.parent === window && this.open_settings_view()) {
|
|
828
|
+
return {};
|
|
829
|
+
}
|
|
830
|
+
this.toolbar?.toast(pane === "plugin" ? "RollRook would open this plugin's settings" : "RollRook would open the code agent accounts");
|
|
831
|
+
return {};
|
|
832
|
+
}
|
|
833
|
+
open_url(params) {
|
|
834
|
+
const text = MockHost.string_param(params, "url");
|
|
835
|
+
let url;
|
|
836
|
+
try {
|
|
837
|
+
url = new URL(text);
|
|
838
|
+
}
|
|
839
|
+
catch (err) {
|
|
840
|
+
throw new Error(`invalid: ${text} is not a URL`, { cause: err });
|
|
841
|
+
}
|
|
842
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
843
|
+
throw new Error(`invalid: ${text} is not http or https`);
|
|
844
|
+
}
|
|
845
|
+
window.open(url.href, "_blank", "noopener");
|
|
846
|
+
return {};
|
|
847
|
+
}
|
|
848
|
+
broadcast(params) {
|
|
849
|
+
const message = params["message"];
|
|
850
|
+
if (message === undefined || !Wire.is_json(message)) {
|
|
851
|
+
throw new Error("invalid: message must be JSON");
|
|
852
|
+
}
|
|
853
|
+
this.channel?.postMessage({
|
|
854
|
+
kind: "message",
|
|
855
|
+
payload: {
|
|
856
|
+
fromViewId: this.view.id,
|
|
857
|
+
fromInstanceId: this.instance_id,
|
|
858
|
+
message,
|
|
859
|
+
},
|
|
860
|
+
});
|
|
861
|
+
return {};
|
|
862
|
+
}
|
|
863
|
+
raven_ask(params) {
|
|
864
|
+
const prompt = MockHost.string_param(params, "prompt");
|
|
865
|
+
if (prompt.trim() === "") {
|
|
866
|
+
throw new Error("invalid: the prompt is empty");
|
|
867
|
+
}
|
|
868
|
+
let conversation_id;
|
|
869
|
+
if (params["conversationId"] === undefined || params["conversationId"] === null) {
|
|
870
|
+
this.conversation_count += 1;
|
|
871
|
+
conversation_id = `mock-conversation-${this.conversation_count}`;
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
conversation_id = MockHost.string_param(params, "conversationId");
|
|
875
|
+
this.conversation(params);
|
|
876
|
+
}
|
|
877
|
+
const previous = this.map_conversations.get(conversation_id)?.answer ?? "";
|
|
878
|
+
this.map_conversations.set(conversation_id, {
|
|
879
|
+
is_running: true,
|
|
880
|
+
answer: previous,
|
|
881
|
+
});
|
|
882
|
+
this.later(() => this.stream_answer(conversation_id, MockSamples.answer(prompt), 1), this.raven_delay_ms);
|
|
883
|
+
this.emit_turn(conversation_id);
|
|
884
|
+
return { conversationId: conversation_id };
|
|
885
|
+
}
|
|
886
|
+
stream_answer(conversation_id, answer, chunk_index) {
|
|
887
|
+
const list_words = answer.split(/(?<=\s)/);
|
|
888
|
+
const word_count = Math.ceil(list_words.length * chunk_index / MockHost.RAVEN_CHUNK_COUNT);
|
|
889
|
+
const is_last = chunk_index >= MockHost.RAVEN_CHUNK_COUNT;
|
|
890
|
+
this.map_conversations.set(conversation_id, {
|
|
891
|
+
is_running: !is_last,
|
|
892
|
+
answer: is_last ? answer : list_words.slice(0, word_count).join(""),
|
|
893
|
+
});
|
|
894
|
+
this.emit_turn(conversation_id);
|
|
895
|
+
if (!is_last) {
|
|
896
|
+
this.later(() => this.stream_answer(conversation_id, answer, chunk_index + 1), MockHost.RAVEN_CHUNK_INTERVAL_MS);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
emit_turn(conversation_id) {
|
|
900
|
+
const state = this.map_conversations.get(conversation_id);
|
|
901
|
+
if (state !== undefined) {
|
|
902
|
+
this.emit("raven.turn", {
|
|
903
|
+
conversationId: conversation_id,
|
|
904
|
+
isRunning: state.is_running,
|
|
905
|
+
answer: state.answer,
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
raven_answer(params) {
|
|
910
|
+
const state = this.conversation(params);
|
|
911
|
+
return {
|
|
912
|
+
isRunning: state.is_running,
|
|
913
|
+
answer: state.answer,
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
conversation(params) {
|
|
917
|
+
const conversation_id = MockHost.string_param(params, "conversationId");
|
|
918
|
+
const state = this.map_conversations.get(conversation_id);
|
|
919
|
+
if (state === undefined) {
|
|
920
|
+
throw new Error(`invalid: ${conversation_id} is not a conversation this plugin started`);
|
|
921
|
+
}
|
|
922
|
+
return state;
|
|
923
|
+
}
|
|
924
|
+
usage_snapshot() {
|
|
925
|
+
return {
|
|
926
|
+
accounts: MockSamples.accounts(this.usage_read_at),
|
|
927
|
+
isRefreshing: this.is_usage_refreshing,
|
|
928
|
+
nextRefreshAt: this.is_usage_refreshing
|
|
929
|
+
? null
|
|
930
|
+
: new Date(this.usage_read_at.getTime() + MockHost.USAGE_INTERVAL_MS).toISOString(),
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
refresh_usage() {
|
|
934
|
+
if (this.is_usage_refreshing) {
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
this.is_usage_refreshing = true;
|
|
938
|
+
this.emit("usage.changed", this.usage_snapshot());
|
|
939
|
+
this.later(() => {
|
|
940
|
+
this.is_usage_refreshing = false;
|
|
941
|
+
this.usage_read_at = new Date();
|
|
942
|
+
this.emit("usage.changed", this.usage_snapshot());
|
|
943
|
+
}, MockHost.USAGE_READ_MS);
|
|
944
|
+
}
|
|
945
|
+
sample_tracks() {
|
|
946
|
+
const assets = new AssetsApi();
|
|
947
|
+
const track = (target, name, icon_url, is_playing) => ({
|
|
948
|
+
target,
|
|
949
|
+
name,
|
|
950
|
+
iconURL: icon_url,
|
|
951
|
+
isPlaying: is_playing,
|
|
952
|
+
isCapturing: false,
|
|
953
|
+
hasVideo: target["kind"] === "tab",
|
|
954
|
+
settings: { ...MockSamples.DEFAULT_AUDIO_SETTINGS },
|
|
955
|
+
});
|
|
956
|
+
return [
|
|
957
|
+
track({ kind: "app", bundleId: "com.apple.Music" }, "Music", assets.app_icon("com.apple.Music"), true),
|
|
958
|
+
track({ kind: "tab", sessionId: 1, tabId: 42 }, "Lo-fi beats to focus to", assets.app_icon("com.google.Chrome"), true),
|
|
959
|
+
track({ kind: "app", bundleId: "com.tinyspeck.slackmacgap" }, "Slack", assets.app_icon("com.tinyspeck.slackmacgap"), false),
|
|
960
|
+
];
|
|
961
|
+
}
|
|
962
|
+
audio_enable(params) {
|
|
963
|
+
if (typeof params["isEnabled"] !== "boolean") {
|
|
964
|
+
throw new Error("invalid: isEnabled must be a boolean");
|
|
965
|
+
}
|
|
966
|
+
this.is_audio_enabled = params["isEnabled"];
|
|
967
|
+
if (!this.is_audio_enabled) {
|
|
968
|
+
this.stop_meters();
|
|
969
|
+
}
|
|
970
|
+
return {};
|
|
971
|
+
}
|
|
972
|
+
audio_update(params, is_reset) {
|
|
973
|
+
const key = MockHost.target_key(params["target"]);
|
|
974
|
+
const track = this.list_tracks.find((candidate) => MockHost.target_key(candidate["target"]) === key);
|
|
975
|
+
if (track === undefined) {
|
|
976
|
+
throw new Error(`failed: no track ${key}`);
|
|
977
|
+
}
|
|
978
|
+
const current = Wire.is_record(track["settings"]) ? track["settings"] : {};
|
|
979
|
+
const changes = is_reset ? MockSamples.DEFAULT_AUDIO_SETTINGS : params["settings"];
|
|
980
|
+
if (!Wire.is_record(changes)) {
|
|
981
|
+
throw new Error("invalid: settings must be an object");
|
|
982
|
+
}
|
|
983
|
+
const merged = {};
|
|
984
|
+
for (const [name, default_value] of Object.entries(MockSamples.DEFAULT_AUDIO_SETTINGS)) {
|
|
985
|
+
const value = name in changes ? changes[name] : current[name];
|
|
986
|
+
merged[name] = MockHost.clamp_audio_setting(name, value, default_value);
|
|
987
|
+
}
|
|
988
|
+
track["settings"] = merged;
|
|
989
|
+
this.emit("audio.tracks", { tracks: this.list_tracks });
|
|
990
|
+
return {};
|
|
991
|
+
}
|
|
992
|
+
audio_meters(params) {
|
|
993
|
+
if (typeof params["isOn"] !== "boolean") {
|
|
994
|
+
throw new Error("invalid: isOn must be a boolean");
|
|
995
|
+
}
|
|
996
|
+
this.stop_meters();
|
|
997
|
+
if (params["isOn"]) {
|
|
998
|
+
const started_at = performance.now();
|
|
999
|
+
this.meter_timer = setInterval(() => this.send_levels(performance.now() - started_at), MockHost.METER_INTERVAL_MS);
|
|
1000
|
+
}
|
|
1001
|
+
return {};
|
|
1002
|
+
}
|
|
1003
|
+
send_levels(elapsed_ms) {
|
|
1004
|
+
if (!this.is_shown || !this.is_audio_enabled) {
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
const list_levels = this.list_tracks.map((track, index) => {
|
|
1008
|
+
const settings = Wire.is_record(track["settings"]) ? track["settings"] : {};
|
|
1009
|
+
const is_silent = track["isPlaying"] !== true || settings["isMuted"] === true;
|
|
1010
|
+
const volume = typeof settings["volume"] === "number" ? settings["volume"] : 1;
|
|
1011
|
+
const phase = elapsed_ms / 1000 + index * 1.7;
|
|
1012
|
+
const level = (offset) => is_silent
|
|
1013
|
+
? 0
|
|
1014
|
+
: Math.min(1, volume * (0.35 + 0.25 * Math.sin(phase * 2.3 + offset) + 0.2 * Math.abs(Math.sin(phase * 7.1 + offset))));
|
|
1015
|
+
return {
|
|
1016
|
+
key: MockHost.target_key(track["target"]),
|
|
1017
|
+
left: level(0),
|
|
1018
|
+
right: level(0.9),
|
|
1019
|
+
};
|
|
1020
|
+
});
|
|
1021
|
+
this.emit("audio.levels", { levels: list_levels });
|
|
1022
|
+
}
|
|
1023
|
+
stop_meters() {
|
|
1024
|
+
clearInterval(this.meter_timer);
|
|
1025
|
+
this.meter_timer = undefined;
|
|
1026
|
+
}
|
|
1027
|
+
async net_fetch(params) {
|
|
1028
|
+
const url = MockHost.url_of(params);
|
|
1029
|
+
const map_headers = {};
|
|
1030
|
+
if (Wire.is_record(params["headers"])) {
|
|
1031
|
+
for (const [name, value] of Object.entries(params["headers"])) {
|
|
1032
|
+
if (typeof value === "string") {
|
|
1033
|
+
map_headers[name] = value;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
const init = {
|
|
1038
|
+
method: typeof params["method"] === "string" ? params["method"] : "GET",
|
|
1039
|
+
headers: map_headers,
|
|
1040
|
+
};
|
|
1041
|
+
if (typeof params["body"] === "string") {
|
|
1042
|
+
init.body = params["body"];
|
|
1043
|
+
}
|
|
1044
|
+
let response;
|
|
1045
|
+
try {
|
|
1046
|
+
response = await fetch(url, init);
|
|
1047
|
+
}
|
|
1048
|
+
catch (err) {
|
|
1049
|
+
throw new Error(`failed: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
|
|
1050
|
+
}
|
|
1051
|
+
const map_reply_headers = {};
|
|
1052
|
+
response.headers.forEach((value, name) => {
|
|
1053
|
+
map_reply_headers[name.toLowerCase()] = value;
|
|
1054
|
+
});
|
|
1055
|
+
return {
|
|
1056
|
+
status: response.status,
|
|
1057
|
+
headers: map_reply_headers,
|
|
1058
|
+
body: await response.text(),
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
view_param(params) {
|
|
1062
|
+
const view_id = MockHost.string_param(params, "viewId");
|
|
1063
|
+
const view = this.list_views.find((candidate) => candidate.id === view_id);
|
|
1064
|
+
if (view === undefined) {
|
|
1065
|
+
throw new Error(`invalid: the manifest has no view ${view_id}`);
|
|
1066
|
+
}
|
|
1067
|
+
return view;
|
|
1068
|
+
}
|
|
1069
|
+
later(action, delay_ms) {
|
|
1070
|
+
const timer = setTimeout(() => {
|
|
1071
|
+
this.set_timeouts.delete(timer);
|
|
1072
|
+
action();
|
|
1073
|
+
}, delay_ms);
|
|
1074
|
+
this.set_timeouts.add(timer);
|
|
1075
|
+
}
|
|
1076
|
+
repeat(action, interval_ms) {
|
|
1077
|
+
this.set_intervals.add(setInterval(action, interval_ms));
|
|
1078
|
+
}
|
|
1079
|
+
static read_views(manifest) {
|
|
1080
|
+
const list_views = [];
|
|
1081
|
+
const views = manifest["views"];
|
|
1082
|
+
if (Array.isArray(views)) {
|
|
1083
|
+
for (const raw of views) {
|
|
1084
|
+
if (!Wire.is_record(raw) || typeof raw["id"] !== "string" || typeof raw["entry"] !== "string") {
|
|
1085
|
+
continue;
|
|
1086
|
+
}
|
|
1087
|
+
const slot = Wire.LIST_SLOTS.find((candidate) => candidate === raw["slot"]) ?? "dock";
|
|
1088
|
+
list_views.push({
|
|
1089
|
+
id: raw["id"],
|
|
1090
|
+
slot,
|
|
1091
|
+
entry: raw["entry"],
|
|
1092
|
+
width: typeof raw["width"] === "number" ? raw["width"] : undefined,
|
|
1093
|
+
height: typeof raw["height"] === "number" ? raw["height"] : undefined,
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
else if (typeof manifest["main"] === "string") {
|
|
1098
|
+
list_views.push({
|
|
1099
|
+
id: "main",
|
|
1100
|
+
slot: "dock",
|
|
1101
|
+
entry: manifest["main"],
|
|
1102
|
+
width: undefined,
|
|
1103
|
+
height: undefined,
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
return list_views;
|
|
1107
|
+
}
|
|
1108
|
+
static pick_view(list_views, view_id) {
|
|
1109
|
+
const query_id = typeof location === "undefined"
|
|
1110
|
+
? null
|
|
1111
|
+
: new URLSearchParams(location.search).get(MockHost.VIEW_PARAMETER);
|
|
1112
|
+
const wanted_id = view_id ?? query_id;
|
|
1113
|
+
const page = typeof location === "undefined" ? "" : location.pathname.split("/").at(-1) ?? "";
|
|
1114
|
+
const page_name = page === "" ? "index.html" : page;
|
|
1115
|
+
return list_views.find((view) => view.id === wanted_id)
|
|
1116
|
+
?? list_views.find((view) => view.entry.split("/").at(-1) === page_name && view.slot !== "popover")
|
|
1117
|
+
?? list_views.find((view) => view.entry.split("/").at(-1) === page_name)
|
|
1118
|
+
?? list_views.find((view) => view.slot !== "popover" && view.slot !== "settings" && view.slot !== "background")
|
|
1119
|
+
?? {
|
|
1120
|
+
id: "main",
|
|
1121
|
+
slot: "dock",
|
|
1122
|
+
entry: "index.html",
|
|
1123
|
+
width: undefined,
|
|
1124
|
+
height: undefined,
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
static read_setting_defaults(manifest) {
|
|
1128
|
+
const map_defaults = {};
|
|
1129
|
+
const settings = manifest["settings"];
|
|
1130
|
+
if (!Array.isArray(settings)) {
|
|
1131
|
+
return map_defaults;
|
|
1132
|
+
}
|
|
1133
|
+
for (const raw of settings) {
|
|
1134
|
+
if (!Wire.is_record(raw) || typeof raw["key"] !== "string") {
|
|
1135
|
+
continue;
|
|
1136
|
+
}
|
|
1137
|
+
const value = raw["default"];
|
|
1138
|
+
map_defaults[raw["key"]] = value !== undefined && Wire.is_json(value) ? value : MockHost.fallback_setting(raw);
|
|
1139
|
+
}
|
|
1140
|
+
return map_defaults;
|
|
1141
|
+
}
|
|
1142
|
+
static fallback_setting(raw) {
|
|
1143
|
+
switch (raw["type"]) {
|
|
1144
|
+
case "string":
|
|
1145
|
+
return "";
|
|
1146
|
+
case "number":
|
|
1147
|
+
return typeof raw["min"] === "number" ? raw["min"] : 0;
|
|
1148
|
+
case "boolean":
|
|
1149
|
+
return false;
|
|
1150
|
+
case "enum": {
|
|
1151
|
+
const options = raw["options"];
|
|
1152
|
+
const first = Array.isArray(options) ? options[0] : undefined;
|
|
1153
|
+
return Wire.is_record(first) && typeof first["value"] === "string" ? first["value"] : null;
|
|
1154
|
+
}
|
|
1155
|
+
default:
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
static clamp_audio_setting(name, value, default_value) {
|
|
1160
|
+
const map_ranges = {
|
|
1161
|
+
volume: [0, 2],
|
|
1162
|
+
pan: [-1, 1],
|
|
1163
|
+
width: [0, 2],
|
|
1164
|
+
reverbMix: [0, 1],
|
|
1165
|
+
};
|
|
1166
|
+
const range = map_ranges[name];
|
|
1167
|
+
if (range !== undefined) {
|
|
1168
|
+
return typeof value === "number" ? Math.min(range[1], Math.max(range[0], value)) : default_value;
|
|
1169
|
+
}
|
|
1170
|
+
if (name === "equalizerGains") {
|
|
1171
|
+
return Array.isArray(value) && value.length === 10
|
|
1172
|
+
? value.map((gain) => typeof gain === "number" ? Math.min(12, Math.max(-12, gain)) : 0)
|
|
1173
|
+
: default_value;
|
|
1174
|
+
}
|
|
1175
|
+
if (name === "reverbRoom") {
|
|
1176
|
+
return Wire.LIST_REVERB_ROOMS.find((room) => room === value) ?? default_value;
|
|
1177
|
+
}
|
|
1178
|
+
return typeof value === typeof default_value && Wire.is_json(value) ? value : default_value;
|
|
1179
|
+
}
|
|
1180
|
+
static target_key(raw) {
|
|
1181
|
+
if (!Wire.is_record(raw)) {
|
|
1182
|
+
throw new Error("invalid: target must be an object");
|
|
1183
|
+
}
|
|
1184
|
+
if (raw["kind"] === "app" && typeof raw["bundleId"] === "string") {
|
|
1185
|
+
return `app:${raw["bundleId"]}`;
|
|
1186
|
+
}
|
|
1187
|
+
if (raw["kind"] === "tab" && typeof raw["sessionId"] === "number" && typeof raw["tabId"] === "number") {
|
|
1188
|
+
return `tab:${raw["sessionId"]}:${raw["tabId"]}`;
|
|
1189
|
+
}
|
|
1190
|
+
throw new Error("invalid: target is { kind: app, bundleId } or { kind: tab, sessionId, tabId }");
|
|
1191
|
+
}
|
|
1192
|
+
static granted_permissions(options) {
|
|
1193
|
+
if (options.list_permissions !== undefined) {
|
|
1194
|
+
return new Set([...MockHost.LIST_DEFAULT_PERMISSIONS, ...options.list_permissions]);
|
|
1195
|
+
}
|
|
1196
|
+
const manifest = options.manifest;
|
|
1197
|
+
if (!Wire.is_record(manifest)) {
|
|
1198
|
+
return undefined;
|
|
1199
|
+
}
|
|
1200
|
+
const permissions = manifest["permissions"];
|
|
1201
|
+
const list_permissions = Array.isArray(permissions)
|
|
1202
|
+
? permissions.filter((permission) => typeof permission === "string")
|
|
1203
|
+
: [];
|
|
1204
|
+
return new Set([...MockHost.LIST_DEFAULT_PERMISSIONS, ...list_permissions]);
|
|
1205
|
+
}
|
|
1206
|
+
static stored_flag(key, on_value) {
|
|
1207
|
+
const stored = localStorage.getItem(key);
|
|
1208
|
+
return stored === null ? undefined : stored === on_value;
|
|
1209
|
+
}
|
|
1210
|
+
static prefers_dark() {
|
|
1211
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
|
|
1212
|
+
}
|
|
1213
|
+
static local_keys() {
|
|
1214
|
+
const list_keys = [];
|
|
1215
|
+
for (let index = 0; index < localStorage.length; index += 1) {
|
|
1216
|
+
const key = localStorage.key(index);
|
|
1217
|
+
if (key !== null) {
|
|
1218
|
+
list_keys.push(key);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return list_keys;
|
|
1222
|
+
}
|
|
1223
|
+
static string_param(params, name) {
|
|
1224
|
+
const value = params[name];
|
|
1225
|
+
if (typeof value !== "string") {
|
|
1226
|
+
throw new Error(`invalid: ${name} must be a string`);
|
|
1227
|
+
}
|
|
1228
|
+
return value;
|
|
1229
|
+
}
|
|
1230
|
+
static number_param(params, name) {
|
|
1231
|
+
const value = params[name];
|
|
1232
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
1233
|
+
throw new Error(`invalid: ${name} must be a number`);
|
|
1234
|
+
}
|
|
1235
|
+
return value;
|
|
1236
|
+
}
|
|
1237
|
+
static storage_key_param(params) {
|
|
1238
|
+
const key = MockHost.string_param(params, "key");
|
|
1239
|
+
if (key.length > StorageApi.KEY_MAX_LENGTH) {
|
|
1240
|
+
throw new Error(`invalid: keys are at most ${StorageApi.KEY_MAX_LENGTH} characters`);
|
|
1241
|
+
}
|
|
1242
|
+
return key;
|
|
1243
|
+
}
|
|
1244
|
+
static path_param(params, may_be_root) {
|
|
1245
|
+
const raw = params["path"];
|
|
1246
|
+
const path = raw === undefined && may_be_root ? "" : MockHost.string_param(params, "path");
|
|
1247
|
+
if (path.startsWith("/") || path.startsWith("~") || path.includes("\\")) {
|
|
1248
|
+
throw new Error(`invalid: ${path} is not a relative path`);
|
|
1249
|
+
}
|
|
1250
|
+
const list_parts = path.split("/").filter((part) => part !== "" && part !== ".");
|
|
1251
|
+
if (list_parts.includes("..")) {
|
|
1252
|
+
throw new Error(`invalid: ${path} leaves the data folder`);
|
|
1253
|
+
}
|
|
1254
|
+
if (!may_be_root && list_parts.length === 0) {
|
|
1255
|
+
throw new Error("invalid: the path is empty");
|
|
1256
|
+
}
|
|
1257
|
+
return list_parts.join("/");
|
|
1258
|
+
}
|
|
1259
|
+
static url_of(params) {
|
|
1260
|
+
const text = MockHost.string_param(params, "url");
|
|
1261
|
+
let url;
|
|
1262
|
+
try {
|
|
1263
|
+
url = new URL(text);
|
|
1264
|
+
}
|
|
1265
|
+
catch (err) {
|
|
1266
|
+
throw new Error(`invalid: ${text} is not a URL`, { cause: err });
|
|
1267
|
+
}
|
|
1268
|
+
if (url.protocol !== "https:") {
|
|
1269
|
+
throw new Error("invalid: net.fetch takes an https URL");
|
|
1270
|
+
}
|
|
1271
|
+
return url;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
//# sourceMappingURL=mock-host.js.map
|