@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.
Files changed (143) hide show
  1. package/dist/agents.d.ts +9 -0
  2. package/dist/agents.d.ts.map +1 -0
  3. package/dist/agents.js +13 -0
  4. package/dist/agents.js.map +1 -0
  5. package/dist/assets.d.ts +35 -0
  6. package/dist/assets.d.ts.map +1 -0
  7. package/dist/assets.js +66 -0
  8. package/dist/assets.js.map +1 -0
  9. package/dist/assistant.d.ts +76 -0
  10. package/dist/assistant.d.ts.map +1 -0
  11. package/dist/assistant.js +161 -0
  12. package/dist/assistant.js.map +1 -0
  13. package/dist/audio.d.ts +55 -0
  14. package/dist/audio.d.ts.map +1 -0
  15. package/dist/audio.js +82 -0
  16. package/dist/audio.js.map +1 -0
  17. package/dist/bridge.d.ts +51 -0
  18. package/dist/bridge.d.ts.map +1 -0
  19. package/dist/bridge.js +78 -0
  20. package/dist/bridge.js.map +1 -0
  21. package/dist/calendar.d.ts +33 -0
  22. package/dist/calendar.d.ts.map +1 -0
  23. package/dist/calendar.js +63 -0
  24. package/dist/calendar.js.map +1 -0
  25. package/dist/context.d.ts +19 -0
  26. package/dist/context.d.ts.map +1 -0
  27. package/dist/context.js +25 -0
  28. package/dist/context.js.map +1 -0
  29. package/dist/errors.d.ts +74 -0
  30. package/dist/errors.d.ts.map +1 -0
  31. package/dist/errors.js +111 -0
  32. package/dist/errors.js.map +1 -0
  33. package/dist/events.d.ts +36 -0
  34. package/dist/events.d.ts.map +1 -0
  35. package/dist/events.js +81 -0
  36. package/dist/events.js.map +1 -0
  37. package/dist/files.d.ts +29 -0
  38. package/dist/files.d.ts.map +1 -0
  39. package/dist/files.js +43 -0
  40. package/dist/files.js.map +1 -0
  41. package/dist/index.d.ts +84 -0
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +78 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/mock/index.d.ts +4 -0
  46. package/dist/mock/index.d.ts.map +1 -0
  47. package/dist/mock/index.js +3 -0
  48. package/dist/mock/index.js.map +1 -0
  49. package/dist/mock/mock-host.d.ts +255 -0
  50. package/dist/mock/mock-host.d.ts.map +1 -0
  51. package/dist/mock/mock-host.js +1274 -0
  52. package/dist/mock/mock-host.js.map +1 -0
  53. package/dist/mock/samples.d.ts +56 -0
  54. package/dist/mock/samples.d.ts.map +1 -0
  55. package/dist/mock/samples.js +283 -0
  56. package/dist/mock/samples.js.map +1 -0
  57. package/dist/mock/toolbar.d.ts +95 -0
  58. package/dist/mock/toolbar.d.ts.map +1 -0
  59. package/dist/mock/toolbar.js +279 -0
  60. package/dist/mock/toolbar.js.map +1 -0
  61. package/dist/net.d.ts +13 -0
  62. package/dist/net.d.ts.map +1 -0
  63. package/dist/net.js +26 -0
  64. package/dist/net.js.map +1 -0
  65. package/dist/plugin.d.ts +11 -0
  66. package/dist/plugin.d.ts.map +1 -0
  67. package/dist/plugin.js +13 -0
  68. package/dist/plugin.js.map +1 -0
  69. package/dist/raven.d.ts +51 -0
  70. package/dist/raven.d.ts.map +1 -0
  71. package/dist/raven.js +100 -0
  72. package/dist/raven.js.map +1 -0
  73. package/dist/segment-display.d.ts +93 -0
  74. package/dist/segment-display.d.ts.map +1 -0
  75. package/dist/segment-display.js +324 -0
  76. package/dist/segment-display.js.map +1 -0
  77. package/dist/settings.d.ts +19 -0
  78. package/dist/settings.d.ts.map +1 -0
  79. package/dist/settings.js +27 -0
  80. package/dist/settings.js.map +1 -0
  81. package/dist/storage.d.ts +28 -0
  82. package/dist/storage.d.ts.map +1 -0
  83. package/dist/storage.js +54 -0
  84. package/dist/storage.js.map +1 -0
  85. package/dist/system.d.ts +19 -0
  86. package/dist/system.d.ts.map +1 -0
  87. package/dist/system.js +24 -0
  88. package/dist/system.js.map +1 -0
  89. package/dist/theme.d.ts +51 -0
  90. package/dist/theme.d.ts.map +1 -0
  91. package/dist/theme.js +85 -0
  92. package/dist/theme.js.map +1 -0
  93. package/dist/types.d.ts +278 -0
  94. package/dist/types.d.ts.map +1 -0
  95. package/dist/types.js +2 -0
  96. package/dist/types.js.map +1 -0
  97. package/dist/ui.d.ts +57 -0
  98. package/dist/ui.d.ts.map +1 -0
  99. package/dist/ui.js +78 -0
  100. package/dist/ui.js.map +1 -0
  101. package/dist/usage.d.ts +14 -0
  102. package/dist/usage.d.ts.map +1 -0
  103. package/dist/usage.js +19 -0
  104. package/dist/usage.js.map +1 -0
  105. package/dist/video.d.ts +11 -0
  106. package/dist/video.d.ts.map +1 -0
  107. package/dist/video.js +17 -0
  108. package/dist/video.js.map +1 -0
  109. package/dist/wire.d.ts +214 -0
  110. package/dist/wire.d.ts.map +1 -0
  111. package/dist/wire.js +538 -0
  112. package/dist/wire.js.map +1 -0
  113. package/package.json +39 -4
  114. package/src/agents.ts +14 -0
  115. package/src/assets.ts +80 -0
  116. package/src/assistant.ts +185 -0
  117. package/src/audio.ts +90 -0
  118. package/src/bridge.ts +96 -0
  119. package/src/calendar.ts +68 -0
  120. package/src/context.ts +28 -0
  121. package/src/errors.ts +131 -0
  122. package/src/events.ts +90 -0
  123. package/src/files.ts +47 -0
  124. package/src/index.ts +152 -0
  125. package/src/mock/index.ts +3 -0
  126. package/src/mock/mock-host.ts +1408 -0
  127. package/src/mock/samples.ts +300 -0
  128. package/src/mock/toolbar.ts +321 -0
  129. package/src/net.ts +27 -0
  130. package/src/plugin.ts +14 -0
  131. package/src/raven.ts +116 -0
  132. package/src/segment-display.ts +377 -0
  133. package/src/settings.ts +29 -0
  134. package/src/storage.ts +59 -0
  135. package/src/system.ts +26 -0
  136. package/src/theme.ts +97 -0
  137. package/src/types.ts +327 -0
  138. package/src/ui.ts +88 -0
  139. package/src/usage.ts +21 -0
  140. package/src/video.ts +17 -0
  141. package/src/wire.ts +608 -0
  142. package/theme.css +407 -0
  143. package/README.md +0 -3
package/src/raven.ts ADDED
@@ -0,0 +1,116 @@
1
+ import { Bridge } from "./bridge.js";
2
+ import type { Events, Unsubscribe } from "./events.js";
3
+ import type { JsonObject, RavenAnswer, RavenTurn } from "./types.js";
4
+ import { Wire } from "./wire.js";
5
+
6
+ /** Options for waiting on Raven's answer. */
7
+ export interface RavenWaitOptions {
8
+ /** Stops the wait; the promise then rejects with the signal's reason. */
9
+ signal?: AbortSignal;
10
+ /** Called with each partial answer while Raven works. */
11
+ on_progress?: (answer: RavenAnswer) => void;
12
+ }
13
+
14
+ /**
15
+ * Ask Raven, RollRook's assistant, in conversations the plugin starts or
16
+ * continues. They appear in Raven's list as the plugin's. Needs the `raven`
17
+ * permission.
18
+ */
19
+ export class RavenApi {
20
+ /** How often a wait checks the answer, besides listening for `raven.turn`. */
21
+ static readonly POLL_INTERVAL_MS = 3000;
22
+
23
+ private readonly events: Events;
24
+
25
+ /**
26
+ * @param events - the event hub `raven.turn` arrives on
27
+ */
28
+ constructor(events: Events) {
29
+ this.events = events;
30
+ }
31
+
32
+ /**
33
+ * Send a prompt as the user would.
34
+ *
35
+ * @param prompt - what to ask
36
+ * @param conversation_id - a conversation the plugin started, to continue it; a new one otherwise
37
+ * @returns the conversation's id
38
+ */
39
+ async ask(prompt: string, conversation_id?: string): Promise<string> {
40
+ const params: JsonObject = { prompt };
41
+ if (conversation_id !== undefined) {
42
+ params["conversationId"] = conversation_id;
43
+ }
44
+ const record = Wire.record(await Bridge.call("raven.ask", params), "raven.ask reply");
45
+ return Wire.string(record, "conversationId", "raven.ask reply");
46
+ }
47
+
48
+ /**
49
+ * @param conversation_id - a conversation the plugin started
50
+ * @returns Raven's last answer in it and whether a turn still runs
51
+ */
52
+ async answer(conversation_id: string): Promise<RavenAnswer> {
53
+ return Wire.raven_answer(await Bridge.call("raven.answer", { conversationId: conversation_id }));
54
+ }
55
+
56
+ /**
57
+ * Show the conversation in Raven.
58
+ *
59
+ * @param conversation_id - a conversation the plugin started
60
+ */
61
+ async open(conversation_id: string): Promise<void> {
62
+ await Bridge.call("raven.open", { conversationId: conversation_id });
63
+ }
64
+
65
+ /**
66
+ * Wait until Raven finishes the conversation's current turn.
67
+ *
68
+ * @param conversation_id - a conversation the plugin started
69
+ * @param options - an abort signal and a progress callback
70
+ * @returns the finished answer
71
+ */
72
+ wait(conversation_id: string, options: RavenWaitOptions = {}): Promise<RavenAnswer> {
73
+ return new Promise<RavenAnswer>((resolve, reject) => {
74
+ const signal = options.signal;
75
+ if (signal?.aborted === true) {
76
+ reject(signal.reason);
77
+ return;
78
+ }
79
+ let unsubscribe: Unsubscribe | undefined = undefined;
80
+ let poll_timer: ReturnType<typeof setInterval> | undefined = undefined;
81
+ const finish = (): void => {
82
+ unsubscribe?.();
83
+ clearInterval(poll_timer);
84
+ signal?.removeEventListener("abort", on_abort);
85
+ };
86
+ const on_abort = (): void => {
87
+ finish();
88
+ reject(signal?.reason);
89
+ };
90
+ const on_answer = (answer: RavenAnswer): void => {
91
+ options.on_progress?.(answer);
92
+ if (!answer.is_running) {
93
+ finish();
94
+ resolve(answer);
95
+ }
96
+ };
97
+ const on_turn = (turn: RavenTurn): void => {
98
+ if (turn.conversation_id === conversation_id) {
99
+ on_answer({
100
+ is_running: turn.is_running,
101
+ answer: turn.answer,
102
+ });
103
+ }
104
+ };
105
+ const poll = (): void => {
106
+ this.answer(conversation_id).then(on_answer, (err: unknown) => {
107
+ finish();
108
+ reject(err);
109
+ });
110
+ };
111
+ signal?.addEventListener("abort", on_abort);
112
+ unsubscribe = this.events.on("raven.turn", on_turn);
113
+ poll_timer = setInterval(poll, RavenApi.POLL_INTERVAL_MS);
114
+ });
115
+ }
116
+ }
@@ -0,0 +1,377 @@
1
+ /** A segment of a character cell: the seven of a digit, and for letters the middle split in two, a center stroke in two halves and four diagonals. */
2
+ export type Segment =
3
+ | "top"
4
+ | "top_right"
5
+ | "bottom_right"
6
+ | "bottom"
7
+ | "bottom_left"
8
+ | "top_left"
9
+ | "middle"
10
+ | "middle_left"
11
+ | "middle_right"
12
+ | "center_top"
13
+ | "center_bottom"
14
+ | "diagonal_top_left"
15
+ | "diagonal_top_right"
16
+ | "diagonal_bottom_left"
17
+ | "diagonal_bottom_right";
18
+
19
+ /** A point of a segment's outline, before the display leans. */
20
+ export interface SegmentPoint {
21
+ x: number;
22
+ y: number;
23
+ }
24
+
25
+ /** How a display is drawn. */
26
+ export interface SegmentDisplayOptions {
27
+ /** A digit's height in CSS pixels. */
28
+ digit_height: number;
29
+ /** Whether the colons blink with the seconds, lit on even ones. */
30
+ is_blinking?: boolean;
31
+ }
32
+
33
+ /**
34
+ * Text drawn as RollRook's segment displays draw it, as on a digital clock:
35
+ * digits on seven segments, letters on fourteen as an alphanumeric display
36
+ * draws them, colons and dashes between; unlit segments faintly showing in
37
+ * the lit color, and in the dark theme a glow around what is lit. The
38
+ * drawing is an SVG in `currentColor`, which `theme.css` sets to
39
+ * `--rr-segment`; set `color` on it for another.
40
+ */
41
+ export class SegmentDisplay {
42
+ /** Every segment of a cell, in drawing order. */
43
+ static readonly LIST_SEGMENTS: readonly Segment[] = [
44
+ "top",
45
+ "top_right",
46
+ "bottom_right",
47
+ "bottom",
48
+ "bottom_left",
49
+ "top_left",
50
+ "middle",
51
+ "middle_left",
52
+ "middle_right",
53
+ "center_top",
54
+ "center_bottom",
55
+ "diagonal_top_left",
56
+ "diagonal_top_right",
57
+ "diagonal_bottom_left",
58
+ "diagonal_bottom_right",
59
+ ];
60
+
61
+ /** The seven segments of a digit's cell. */
62
+ static readonly LIST_DIGIT_SEGMENTS: readonly Segment[] = [
63
+ "top",
64
+ "top_right",
65
+ "bottom_right",
66
+ "bottom",
67
+ "bottom_left",
68
+ "top_left",
69
+ "middle",
70
+ ];
71
+
72
+ /** The segments each character lights; a character drawn only with digit segments gets a digit's cell. */
73
+ static readonly map_glyph_segments: Record<string, readonly Segment[]> = {
74
+ "0": ["top", "top_right", "bottom_right", "bottom", "bottom_left", "top_left"],
75
+ "1": ["top_right", "bottom_right"],
76
+ "2": ["top", "top_right", "middle", "bottom_left", "bottom"],
77
+ "3": ["top", "top_right", "middle", "bottom_right", "bottom"],
78
+ "4": ["top_left", "middle", "top_right", "bottom_right"],
79
+ "5": ["top", "top_left", "middle", "bottom_right", "bottom"],
80
+ "6": ["top", "top_left", "middle", "bottom_left", "bottom_right", "bottom"],
81
+ "7": ["top", "top_right", "bottom_right"],
82
+ "8": ["top", "top_right", "bottom_right", "bottom", "bottom_left", "top_left", "middle"],
83
+ "9": ["top", "top_right", "bottom_right", "bottom", "top_left", "middle"],
84
+ "a": ["top", "top_right", "middle", "bottom_left", "bottom", "bottom_right"],
85
+ "d": ["top_right", "bottom_right", "bottom", "bottom_left", "middle"],
86
+ "e": ["top", "top_right", "top_left", "middle", "bottom_left", "bottom"],
87
+ "h": ["top_left", "bottom_left", "middle", "bottom_right"],
88
+ "i": ["center_bottom"],
89
+ "n": ["middle", "bottom_left", "bottom_right"],
90
+ "o": ["middle", "bottom_left", "bottom", "bottom_right"],
91
+ "r": ["middle", "bottom_left"],
92
+ "t": ["top_left", "middle", "bottom_left", "bottom"],
93
+ "u": ["bottom_left", "bottom", "bottom_right"],
94
+ "A": ["top", "top_right", "bottom_right", "top_left", "bottom_left", "middle_left", "middle_right"],
95
+ "D": ["top", "top_right", "bottom_right", "bottom", "center_top", "center_bottom"],
96
+ "E": ["top", "top_left", "bottom_left", "bottom", "middle_left"],
97
+ "F": ["top", "top_left", "bottom_left", "middle_left"],
98
+ "H": ["top_left", "bottom_left", "top_right", "bottom_right", "middle_left", "middle_right"],
99
+ "I": ["top", "bottom", "center_top", "center_bottom"],
100
+ "M": ["top_left", "bottom_left", "top_right", "bottom_right", "diagonal_top_left", "diagonal_top_right"],
101
+ "N": ["top_left", "bottom_left", "top_right", "bottom_right", "diagonal_top_left", "diagonal_bottom_right"],
102
+ "O": ["top", "top_right", "bottom_right", "bottom", "bottom_left", "top_left"],
103
+ "R": ["top", "top_right", "top_left", "bottom_left", "middle_left", "middle_right", "diagonal_bottom_right"],
104
+ "S": ["top", "top_left", "middle_left", "middle_right", "bottom_right", "bottom"],
105
+ "T": ["top", "center_top", "center_bottom"],
106
+ "U": ["top_left", "bottom_left", "bottom", "bottom_right", "top_right"],
107
+ "W": ["top_left", "bottom_left", "top_right", "bottom_right", "diagonal_bottom_left", "diagonal_bottom_right"],
108
+ };
109
+
110
+ /** A digit's width, as a share of its height. */
111
+ static readonly WIDTH_RATIO = 0.58;
112
+ /** A segment's thickness, as a share of the height. */
113
+ static readonly THICKNESS_RATIO = 0.115;
114
+ /** The thinnest a segment gets, so small displays stay legible. */
115
+ static readonly MINIMUM_THICKNESS = 1.1;
116
+ /** The space between cells, as a share of the height. */
117
+ static readonly SPACING_RATIO = 0.2;
118
+ /** A colon's cell width, as a share of the height. */
119
+ static readonly COLON_WIDTH_RATIO = 0.26;
120
+ /** A dash's cell width, as a share of the height. */
121
+ static readonly DASH_WIDTH_RATIO = 0.34;
122
+ /** How far the display leans, as a share of its height. */
123
+ static readonly SLANT = 0.08;
124
+ /** The glow's radius in the dark theme, as a share of the height. */
125
+ static readonly GLOW_RADIUS_RATIO = 0.18;
126
+ /** The gap where two segments meet, as a share of the thickness. */
127
+ static readonly GAP_RATIO = 0.3;
128
+ /** The heights of a colon's two dots, as shares of the height. */
129
+ static readonly LIST_COLON_HEIGHTS = [0.32, 0.7];
130
+ /** The period of the colons' blink: lit for one second, dark for the next. */
131
+ static readonly BLINK_PERIOD_MS = 2000;
132
+ /** The class of the drawing. */
133
+ static readonly CLASS_NAME = "rr-segment-display";
134
+
135
+ private static readonly SVG_NAMESPACE = "http://www.w3.org/2000/svg";
136
+
137
+ private readonly digit_height: number;
138
+ private readonly is_blinking: boolean;
139
+
140
+ /**
141
+ * @param options - the digit height and whether the colons blink
142
+ */
143
+ constructor(options: SegmentDisplayOptions) {
144
+ this.digit_height = options.digit_height;
145
+ this.is_blinking = options.is_blinking ?? false;
146
+ }
147
+
148
+ /**
149
+ * The width some text takes, without its glow, for lining displays up in columns.
150
+ *
151
+ * @param text - the text
152
+ * @returns the width in CSS pixels
153
+ */
154
+ width_of(text: string): number {
155
+ const list_widths = [...text].map((character: string) => this.cell_width(character));
156
+ const spacing = this.spacing() * Math.max(list_widths.length - 1, 0);
157
+ return list_widths.reduce((total: number, width: number) => total + width, 0)
158
+ + spacing
159
+ + SegmentDisplay.SLANT * this.digit_height;
160
+ }
161
+
162
+ /**
163
+ * Draw text as an SVG the size of the display; the glow spills outside it.
164
+ *
165
+ * @param text - digits, the letters the display knows, colons and dashes
166
+ * @returns the drawing
167
+ */
168
+ render(text: string): SVGSVGElement {
169
+ const list_lit: string[] = [];
170
+ const list_unlit: string[] = [];
171
+ const list_lit_strokes: string[] = [];
172
+ const list_unlit_strokes: string[] = [];
173
+ const list_colons: string[] = [];
174
+ let x = 0;
175
+ for (const character of text) {
176
+ if (character === ":") {
177
+ list_colons.push(...this.colon_paths(x));
178
+ } else if (character === "-") {
179
+ list_lit.push(this.horizontal({ from_x: x, to_x: x + this.cell_width("-"), y: this.digit_height / 2, gap: 0 }));
180
+ } else {
181
+ const list_glyph = SegmentDisplay.map_glyph_segments[character] ?? [];
182
+ const is_digit_cell = list_glyph.every((segment: Segment) => SegmentDisplay.LIST_DIGIT_SEGMENTS.includes(segment));
183
+ for (const segment of SegmentDisplay.LIST_SEGMENTS) {
184
+ const is_in_cell = is_digit_cell
185
+ ? SegmentDisplay.LIST_DIGIT_SEGMENTS.includes(segment)
186
+ : segment !== "middle";
187
+ if (!is_in_cell) {
188
+ continue;
189
+ }
190
+ const is_lit = list_glyph.includes(segment);
191
+ if (segment.startsWith("diagonal_")) {
192
+ (is_lit ? list_lit_strokes : list_unlit_strokes).push(this.diagonal(segment, x));
193
+ } else {
194
+ (is_lit ? list_lit : list_unlit).push(this.segment_path(segment, x));
195
+ }
196
+ }
197
+ }
198
+ x += this.cell_width(character) + this.spacing();
199
+ }
200
+ const width = this.width_of(text);
201
+ const svg = document.createElementNS(SegmentDisplay.SVG_NAMESPACE, "svg");
202
+ svg.setAttribute("width", width.toFixed(2));
203
+ svg.setAttribute("height", String(this.digit_height));
204
+ svg.setAttribute("viewBox", `0 0 ${width.toFixed(3)} ${this.digit_height}`);
205
+ svg.setAttribute("class", this.is_blinking ? `${SegmentDisplay.CLASS_NAME} is-blinking` : SegmentDisplay.CLASS_NAME);
206
+ svg.setAttribute("role", "img");
207
+ svg.setAttribute("aria-label", text);
208
+ svg.style.setProperty("--rr-segment-glow", `${(this.digit_height * SegmentDisplay.GLOW_RADIUS_RATIO).toFixed(2)}px`);
209
+ if (this.is_blinking) {
210
+ svg.style.setProperty("--rr-segment-blink-delay", `${SegmentDisplay.blink_delay_ms(Date.now())}ms`);
211
+ }
212
+ const stroke_width = (this.thickness() * 0.85).toFixed(3);
213
+ svg.append(
214
+ SegmentDisplay.path_element("unlit", [...list_unlit, ...list_colons].join(" ")),
215
+ SegmentDisplay.path_element("unlit stroke", list_unlit_strokes.join(" "), stroke_width),
216
+ SegmentDisplay.path_element("lit", list_lit.join(" ")),
217
+ SegmentDisplay.path_element("lit stroke", list_lit_strokes.join(" "), stroke_width),
218
+ SegmentDisplay.path_element("colons", list_colons.join(" ")),
219
+ );
220
+ return svg;
221
+ }
222
+
223
+ /**
224
+ * The delay that starts the colons' blink on an even second of the wall clock.
225
+ *
226
+ * @param now_ms - the time now, in milliseconds since the epoch
227
+ * @returns a negative CSS animation delay, in milliseconds
228
+ */
229
+ static blink_delay_ms(now_ms: number): number {
230
+ return -(now_ms % SegmentDisplay.BLINK_PERIOD_MS);
231
+ }
232
+
233
+ private cell_width(character: string): number {
234
+ if (character === ":") {
235
+ return this.digit_height * SegmentDisplay.COLON_WIDTH_RATIO;
236
+ }
237
+ if (character === "-") {
238
+ return this.digit_height * SegmentDisplay.DASH_WIDTH_RATIO;
239
+ }
240
+ return this.digit_height * SegmentDisplay.WIDTH_RATIO;
241
+ }
242
+
243
+ private spacing(): number {
244
+ return this.digit_height * SegmentDisplay.SPACING_RATIO;
245
+ }
246
+
247
+ private thickness(): number {
248
+ return Math.max(this.digit_height * SegmentDisplay.THICKNESS_RATIO, SegmentDisplay.MINIMUM_THICKNESS);
249
+ }
250
+
251
+ private colon_paths(origin_x: number): string[] {
252
+ const size = this.thickness();
253
+ const center_x = origin_x + this.cell_width(":") / 2;
254
+ const radius = size * 0.25;
255
+ return SegmentDisplay.LIST_COLON_HEIGHTS.map((ratio: number) => {
256
+ const left = center_x - size / 2;
257
+ const top = this.digit_height * ratio - size / 2;
258
+ return this.outline([
259
+ { x: left + radius, y: top },
260
+ { x: left + size - radius, y: top },
261
+ { x: left + size, y: top + radius },
262
+ { x: left + size, y: top + size - radius },
263
+ { x: left + size - radius, y: top + size },
264
+ { x: left + radius, y: top + size },
265
+ { x: left, y: top + size - radius },
266
+ { x: left, y: top + radius },
267
+ ]);
268
+ });
269
+ }
270
+
271
+ private segment_path(segment: Segment, origin_x: number): string {
272
+ const half = this.thickness() / 2;
273
+ const gap = this.thickness() * SegmentDisplay.GAP_RATIO;
274
+ const left = origin_x + half;
275
+ const right = origin_x + this.cell_width("0") - half;
276
+ const center = origin_x + this.cell_width("0") / 2;
277
+ const top = half;
278
+ const middle = this.digit_height / 2;
279
+ const bottom = this.digit_height - half;
280
+ switch (segment) {
281
+ case "top":
282
+ return this.horizontal({ from_x: left, to_x: right, y: top, gap });
283
+ case "middle":
284
+ return this.horizontal({ from_x: left, to_x: right, y: middle, gap });
285
+ case "middle_left":
286
+ return this.horizontal({ from_x: left, to_x: center, y: middle, gap });
287
+ case "middle_right":
288
+ return this.horizontal({ from_x: center, to_x: right, y: middle, gap });
289
+ case "bottom":
290
+ return this.horizontal({ from_x: left, to_x: right, y: bottom, gap });
291
+ case "top_left":
292
+ return this.vertical({ from_y: top, to_y: middle, x: left, gap });
293
+ case "top_right":
294
+ return this.vertical({ from_y: top, to_y: middle, x: right, gap });
295
+ case "bottom_left":
296
+ return this.vertical({ from_y: middle, to_y: bottom, x: left, gap });
297
+ case "bottom_right":
298
+ return this.vertical({ from_y: middle, to_y: bottom, x: right, gap });
299
+ case "center_top":
300
+ return this.vertical({ from_y: top, to_y: middle, x: center, gap });
301
+ case "center_bottom":
302
+ return this.vertical({ from_y: middle, to_y: bottom, x: center, gap });
303
+ default:
304
+ return this.diagonal(segment, origin_x);
305
+ }
306
+ }
307
+
308
+ private diagonal(segment: Segment, origin_x: number): string {
309
+ const half = this.thickness() / 2;
310
+ const left = origin_x + half;
311
+ const right = origin_x + this.cell_width("0") - half;
312
+ const center = origin_x + this.cell_width("0") / 2;
313
+ const top = half;
314
+ const middle = this.digit_height / 2;
315
+ const bottom = this.digit_height - half;
316
+ const start: SegmentPoint = {
317
+ x: segment === "diagonal_top_left" || segment === "diagonal_bottom_left" ? left : right,
318
+ y: segment === "diagonal_top_left" || segment === "diagonal_top_right" ? top : bottom,
319
+ };
320
+ const end: SegmentPoint = { x: center, y: middle };
321
+ const inset = this.thickness() * 1.2;
322
+ const length = Math.hypot(end.x - start.x, end.y - start.y);
323
+ const unit_x = (end.x - start.x) / length;
324
+ const unit_y = (end.y - start.y) / length;
325
+ return this.outline([
326
+ { x: start.x + unit_x * inset, y: start.y + unit_y * inset },
327
+ { x: end.x - unit_x * inset * 0.6, y: end.y - unit_y * inset * 0.6 },
328
+ ], false);
329
+ }
330
+
331
+ private horizontal(span: { from_x: number; to_x: number; y: number; gap: number }): string {
332
+ const half = this.thickness() / 2;
333
+ const start = span.from_x + span.gap;
334
+ const end = span.to_x - span.gap;
335
+ return this.outline([
336
+ { x: start, y: span.y },
337
+ { x: start + half, y: span.y - half },
338
+ { x: end - half, y: span.y - half },
339
+ { x: end, y: span.y },
340
+ { x: end - half, y: span.y + half },
341
+ { x: start + half, y: span.y + half },
342
+ ]);
343
+ }
344
+
345
+ private vertical(span: { from_y: number; to_y: number; x: number; gap: number }): string {
346
+ const half = this.thickness() / 2;
347
+ const start = span.from_y + span.gap;
348
+ const end = span.to_y - span.gap;
349
+ return this.outline([
350
+ { x: span.x, y: start },
351
+ { x: span.x + half, y: start + half },
352
+ { x: span.x + half, y: end - half },
353
+ { x: span.x, y: end },
354
+ { x: span.x - half, y: end - half },
355
+ { x: span.x - half, y: start + half },
356
+ ]);
357
+ }
358
+
359
+ private outline(list_points: SegmentPoint[], is_closed = true): string {
360
+ const lean = SegmentDisplay.SLANT * this.digit_height;
361
+ const list_commands = list_points.map((point: SegmentPoint, index: number) => {
362
+ const x = point.x - SegmentDisplay.SLANT * point.y + lean;
363
+ return `${index === 0 ? "M" : "L"}${x.toFixed(3)} ${point.y.toFixed(3)}`;
364
+ });
365
+ return is_closed ? `${list_commands.join(" ")} Z` : list_commands.join(" ");
366
+ }
367
+
368
+ private static path_element(class_name: string, d: string, stroke_width?: string): SVGPathElement {
369
+ const path = document.createElementNS(SegmentDisplay.SVG_NAMESPACE, "path");
370
+ path.setAttribute("class", class_name);
371
+ path.setAttribute("d", d);
372
+ if (stroke_width !== undefined) {
373
+ path.setAttribute("stroke-width", stroke_width);
374
+ }
375
+ return path;
376
+ }
377
+ }
@@ -0,0 +1,29 @@
1
+ import { Bridge } from "./bridge.js";
2
+ import type { JsonValue, SettingValues } from "./types.js";
3
+ import { Wire } from "./wire.js";
4
+
5
+ /**
6
+ * The settings the manifest declares, which RollRook shows under Plugins in
7
+ * its Settings. A change, here or there, arrives as `settings.changed`.
8
+ */
9
+ export class SettingsApi {
10
+ /**
11
+ * @returns each setting's value, or its default when unset
12
+ */
13
+ async get(): Promise<SettingValues> {
14
+ return Wire.setting_values(await Bridge.call("settings.get"));
15
+ }
16
+
17
+ /**
18
+ * Change a setting; RollRook checks the value against the setting's type.
19
+ *
20
+ * @param key - the setting's key, as the manifest declares it
21
+ * @param value - the new value
22
+ */
23
+ async set(key: string, value: JsonValue): Promise<void> {
24
+ await Bridge.call("settings.set", {
25
+ key,
26
+ value,
27
+ });
28
+ }
29
+ }
package/src/storage.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { Bridge } from "./bridge.js";
2
+ import { FailedError } from "./errors.js";
3
+ import type { JsonValue } from "./types.js";
4
+ import { Wire } from "./wire.js";
5
+
6
+ /**
7
+ * Small JSON values kept for the plugin by key. Keys are at most 200
8
+ * characters, and all values together at most 5 MB.
9
+ */
10
+ export class StorageApi {
11
+ /** The longest key the host accepts. */
12
+ static readonly KEY_MAX_LENGTH = 200;
13
+
14
+ /**
15
+ * @param key - the value's key
16
+ * @returns the stored value, or undefined when the key is unset
17
+ */
18
+ async get(key: string): Promise<JsonValue | undefined> {
19
+ const result = await Bridge.call("storage.get", { key });
20
+ if (result === null || result === undefined) {
21
+ return undefined;
22
+ }
23
+ const record = Wire.record(result, "storage.get reply");
24
+ const value = record["value"];
25
+ if (value === null || value === undefined) {
26
+ return undefined;
27
+ }
28
+ if (!Wire.is_json(value)) {
29
+ throw new FailedError("malformed storage.get reply: value is not JSON", "storage.get");
30
+ }
31
+ return value;
32
+ }
33
+
34
+ /**
35
+ * @param key - the value's key
36
+ * @param value - any JSON value
37
+ */
38
+ async set(key: string, value: JsonValue): Promise<void> {
39
+ await Bridge.call("storage.set", {
40
+ key,
41
+ value,
42
+ });
43
+ }
44
+
45
+ /**
46
+ * @param key - the key to unset
47
+ */
48
+ async delete(key: string): Promise<void> {
49
+ await Bridge.call("storage.delete", { key });
50
+ }
51
+
52
+ /**
53
+ * @returns every key that holds a value
54
+ */
55
+ async keys(): Promise<string[]> {
56
+ const record = Wire.record(await Bridge.call("storage.keys"), "storage.keys reply");
57
+ return Wire.array(record, "keys", "storage.keys reply").filter((key: unknown) => typeof key === "string");
58
+ }
59
+ }
package/src/system.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { Bridge } from "./bridge.js";
2
+ import type { City } from "./types.js";
3
+ import { Wire } from "./wire.js";
4
+
5
+ /** What the plugin may ask of the Mac. */
6
+ export class SystemApi {
7
+ /**
8
+ * Open a web page in the user's browser.
9
+ *
10
+ * @param url - an http or https URL
11
+ */
12
+ async open_url(url: string): Promise<void> {
13
+ await Bridge.call("system.openURL", { url });
14
+ }
15
+
16
+ /**
17
+ * Find cities by name, as the Mac's time zones and Apple Maps know them,
18
+ * each with the time zone it keeps.
19
+ *
20
+ * @param query - what the user typed
21
+ * @returns up to 8 cities, the best first
22
+ */
23
+ async search_cities(query: string): Promise<City[]> {
24
+ return Wire.cities(await Bridge.call("system.searchCities", { query }));
25
+ }
26
+ }