@pioneercompany/app-sdk 0.1.2 → 0.2.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.
@@ -1,15 +1,13 @@
1
1
  import { failure } from "../error.js";
2
2
  export { PioneerError } from "../error.js";
3
3
 
4
- export interface Theme {
5
- readonly appearance: "light" | "dark";
6
- readonly background: string;
7
- readonly foreground: string;
8
- readonly muted: string;
9
- readonly border: string;
10
- readonly accent: string;
11
- readonly "accent-foreground": string;
12
- }
4
+ import { decodeTheme, type Theme } from "./theme.js";
5
+ export { THEME_COLOR_KEYS, THEME_CONFIG_COLOR_KEYS } from "./theme.js";
6
+ export type {
7
+ Theme, ThemeMode, ThemeValue, ThemeObject, ThemeColorName, ThemeColors,
8
+ ThemeConfig, ThemeConfigColors, ThemeConfigColorName, ThemeToken,
9
+ } from "./theme.js";
10
+
13
11
  export interface ConnectAppOptions {
14
12
  /** Exact grant root, absolute or relative to document URL; must end in /. */
15
13
  baseUrl?: string;
@@ -79,14 +77,10 @@ export async function connectApp(options: ConnectAppOptions = {}): Promise<AppCl
79
77
  }
80
78
  return;
81
79
  }
82
- if (!data || data.type !== "pioneer.theme" || !data.palette) return;
83
- const value = data.palette;
84
- if (value.appearance !== "dark" && value.appearance !== "light") return;
85
- const colors = ["background", "foreground", "muted", "border", "accent", "accent-foreground"] as const;
86
- for (const key of colors) {
87
- if (typeof value[key] !== "string" || value[key].length > 128 || !CSS.supports("color", value[key])) return;
88
- }
89
- current = Object.freeze({ appearance: value.appearance, ...Object.fromEntries(colors.map(key => [key, value[key]])) }) as Readonly<Theme>;
80
+ if (!data || data.type !== "pioneer.theme") return;
81
+ const theme = decodeTheme(data.theme);
82
+ if (!theme) return;
83
+ current = theme;
90
84
  for (const listener of [...listeners]) {
91
85
  if (disposed) break;
92
86
  if (!listeners.has(listener)) continue;
@@ -0,0 +1,418 @@
1
+ /** JSON theme snapshot: native field names are retained without aliases. */
2
+ export type ThemeMode = "light" | "dark";
3
+ export type ThemeValue = null | boolean | number | string | readonly ThemeValue[] | ThemeObject;
4
+ export interface ThemeObject { readonly [key: string]: ThemeValue | undefined }
5
+
6
+ /** Resolved native colors, including roles absent from a user's theme file. */
7
+ export const THEME_COLOR_KEYS = [
8
+ "accent",
9
+ "accent_foreground",
10
+ "accordion",
11
+ "background",
12
+ "border",
13
+ "button",
14
+ "button_active",
15
+ "button_foreground",
16
+ "button_hover",
17
+ "button_danger",
18
+ "button_danger_active",
19
+ "button_danger_foreground",
20
+ "button_danger_hover",
21
+ "button_info",
22
+ "button_info_active",
23
+ "button_info_foreground",
24
+ "button_info_hover",
25
+ "button_primary",
26
+ "button_primary_active",
27
+ "button_primary_foreground",
28
+ "button_primary_hover",
29
+ "button_secondary",
30
+ "button_secondary_active",
31
+ "button_secondary_foreground",
32
+ "button_secondary_hover",
33
+ "button_success",
34
+ "button_success_active",
35
+ "button_success_foreground",
36
+ "button_success_hover",
37
+ "button_warning",
38
+ "button_warning_active",
39
+ "button_warning_foreground",
40
+ "button_warning_hover",
41
+ "group_box",
42
+ "group_box_foreground",
43
+ "caret",
44
+ "chart_1",
45
+ "chart_2",
46
+ "chart_3",
47
+ "chart_4",
48
+ "chart_5",
49
+ "chart_bullish",
50
+ "chart_bearish",
51
+ "chart_grid",
52
+ "danger",
53
+ "danger_active",
54
+ "danger_foreground",
55
+ "danger_hover",
56
+ "description_list_label",
57
+ "description_list_label_foreground",
58
+ "drag_border",
59
+ "drop_target",
60
+ "foreground",
61
+ "info",
62
+ "info_active",
63
+ "info_foreground",
64
+ "info_hover",
65
+ "input",
66
+ "link",
67
+ "link_active",
68
+ "link_hover",
69
+ "list",
70
+ "list_active",
71
+ "list_active_border",
72
+ "list_even",
73
+ "list_head",
74
+ "list_hover",
75
+ "muted",
76
+ "muted_foreground",
77
+ "popover",
78
+ "popover_foreground",
79
+ "primary",
80
+ "primary_active",
81
+ "primary_foreground",
82
+ "primary_hover",
83
+ "progress_bar",
84
+ "ring",
85
+ "scrollbar",
86
+ "scrollbar_thumb",
87
+ "scrollbar_thumb_hover",
88
+ "secondary",
89
+ "secondary_active",
90
+ "secondary_foreground",
91
+ "secondary_hover",
92
+ "selection",
93
+ "sidebar",
94
+ "sidebar_accent",
95
+ "sidebar_accent_foreground",
96
+ "sidebar_border",
97
+ "sidebar_foreground",
98
+ "sidebar_primary",
99
+ "sidebar_primary_foreground",
100
+ "skeleton",
101
+ "slider_bar",
102
+ "slider_thumb",
103
+ "success",
104
+ "success_foreground",
105
+ "success_hover",
106
+ "success_active",
107
+ "switch",
108
+ "switch_thumb",
109
+ "tab",
110
+ "tab_active",
111
+ "tab_active_foreground",
112
+ "tab_bar",
113
+ "tab_bar_segmented",
114
+ "tab_foreground",
115
+ "table",
116
+ "table_active",
117
+ "table_active_border",
118
+ "table_even",
119
+ "table_head",
120
+ "table_head_foreground",
121
+ "table_foot",
122
+ "table_foot_foreground",
123
+ "table_hover",
124
+ "table_row_border",
125
+ "title_bar",
126
+ "title_bar_border",
127
+ "status_bar",
128
+ "status_bar_border",
129
+ "warning",
130
+ "warning_active",
131
+ "warning_hover",
132
+ "warning_foreground",
133
+ "overlay",
134
+ "window_border",
135
+ "red",
136
+ "red_light",
137
+ "green",
138
+ "green_light",
139
+ "blue",
140
+ "blue_light",
141
+ "yellow",
142
+ "yellow_light",
143
+ "magenta",
144
+ "magenta_light",
145
+ "cyan",
146
+ "cyan_light",
147
+ ] as const;
148
+ export type ThemeColorName = typeof THEME_COLOR_KEYS[number];
149
+ export type ThemeColors = Readonly<Record<ThemeColorName, string> & Record<string, string>>;
150
+
151
+ /** Theme-file keys from the complete GPUI ThemeConfigColors schema. */
152
+ export const THEME_CONFIG_COLOR_KEYS = [
153
+ "accent.background",
154
+ "accent.foreground",
155
+ "accordion.background",
156
+ "background",
157
+ "border",
158
+ "button.background",
159
+ "button.active.background",
160
+ "button.foreground",
161
+ "button.hover.background",
162
+ "button.danger.background",
163
+ "button.danger.active.background",
164
+ "button.danger.foreground",
165
+ "button.danger.hover.background",
166
+ "button.info.background",
167
+ "button.info.active.background",
168
+ "button.info.foreground",
169
+ "button.info.hover.background",
170
+ "button.primary.background",
171
+ "button.primary.active.background",
172
+ "button.primary.foreground",
173
+ "button.primary.hover.background",
174
+ "button.secondary.background",
175
+ "button.secondary.active.background",
176
+ "button.secondary.foreground",
177
+ "button.secondary.hover.background",
178
+ "button.success.background",
179
+ "button.success.active.background",
180
+ "button.success.foreground",
181
+ "button.success.hover.background",
182
+ "button.warning.background",
183
+ "button.warning.active.background",
184
+ "button.warning.foreground",
185
+ "button.warning.hover.background",
186
+ "group_box.background",
187
+ "group_box.foreground",
188
+ "group_box.title.foreground",
189
+ "caret",
190
+ "chart.1",
191
+ "chart.2",
192
+ "chart.3",
193
+ "chart.4",
194
+ "chart.5",
195
+ "chart.bullish",
196
+ "chart.bearish",
197
+ "chart.grid",
198
+ "danger.background",
199
+ "danger.active.background",
200
+ "danger.foreground",
201
+ "danger.hover.background",
202
+ "description_list.label.background",
203
+ "description_list.label.foreground",
204
+ "drag.border",
205
+ "drop_target.background",
206
+ "foreground",
207
+ "info.background",
208
+ "info.active.background",
209
+ "info.foreground",
210
+ "info.hover.background",
211
+ "input.border",
212
+ "link",
213
+ "link.active",
214
+ "link.hover",
215
+ "list.background",
216
+ "list.active.background",
217
+ "list.active.border",
218
+ "list.even.background",
219
+ "list.head.background",
220
+ "list.hover.background",
221
+ "muted.background",
222
+ "muted.foreground",
223
+ "popover.background",
224
+ "popover.foreground",
225
+ "primary.background",
226
+ "primary.active.background",
227
+ "primary.foreground",
228
+ "primary.hover.background",
229
+ "progress.bar.background",
230
+ "ring",
231
+ "scrollbar.background",
232
+ "scrollbar.thumb.background",
233
+ "scrollbar.thumb.hover.background",
234
+ "secondary.background",
235
+ "secondary.active.background",
236
+ "secondary.foreground",
237
+ "secondary.hover.background",
238
+ "selection.background",
239
+ "sidebar.background",
240
+ "sidebar.accent.background",
241
+ "sidebar.accent.foreground",
242
+ "sidebar.border",
243
+ "sidebar.foreground",
244
+ "sidebar.primary.background",
245
+ "sidebar.primary.foreground",
246
+ "skeleton.background",
247
+ "slider.background",
248
+ "slider.thumb.background",
249
+ "success.background",
250
+ "success.foreground",
251
+ "success.hover.background",
252
+ "success.active.background",
253
+ "switch.background",
254
+ "switch.thumb.background",
255
+ "tab.background",
256
+ "tab.active.background",
257
+ "tab.active.foreground",
258
+ "tab_bar.background",
259
+ "tab_bar.segmented.background",
260
+ "tab.foreground",
261
+ "table.background",
262
+ "table.active.background",
263
+ "table.active.border",
264
+ "table.even.background",
265
+ "table.head.background",
266
+ "table.head.foreground",
267
+ "table.foot.background",
268
+ "table.foot.foreground",
269
+ "table.hover.background",
270
+ "table.row.border",
271
+ "title_bar.background",
272
+ "title_bar.border",
273
+ "status_bar.background",
274
+ "status_bar.border",
275
+ "warning.background",
276
+ "warning.active.background",
277
+ "warning.hover.background",
278
+ "warning.foreground",
279
+ "overlay",
280
+ "window.border",
281
+ "base.blue",
282
+ "base.blue.light",
283
+ "base.cyan",
284
+ "base.cyan.light",
285
+ "base.green",
286
+ "base.green.light",
287
+ "base.magenta",
288
+ "base.magenta.light",
289
+ "base.red",
290
+ "base.red.light",
291
+ "base.yellow",
292
+ "base.yellow.light",
293
+ ] as const;
294
+ export type ThemeConfigColorName = typeof THEME_CONFIG_COLOR_KEYS[number];
295
+ export type ThemeConfigColors = Readonly<Partial<Record<ThemeConfigColorName, string | null>> & ThemeObject>;
296
+
297
+ export interface ThemeConfig extends ThemeObject {
298
+ readonly name: string;
299
+ readonly mode: ThemeMode;
300
+ readonly is_default?: boolean;
301
+ readonly "font.size"?: number | null;
302
+ readonly "font.family"?: string | null;
303
+ readonly "mono_font.size"?: number | null;
304
+ readonly "mono_font.family"?: string | null;
305
+ readonly radius?: number | null;
306
+ readonly "radius.lg"?: number | null;
307
+ readonly "window.radius"?: number | null;
308
+ readonly shadow?: boolean | null;
309
+ readonly colors: ThemeConfigColors;
310
+ readonly highlight?: ThemeObject | null;
311
+ }
312
+ export interface ThemeToken extends ThemeObject {
313
+ readonly color: string;
314
+ readonly background: ThemeValue;
315
+ }
316
+ /** All serializable native Theme fields, plus its full semantic projection. */
317
+ export interface Theme extends ThemeObject {
318
+ readonly mode: ThemeMode;
319
+ readonly colors: ThemeColors;
320
+ readonly light_theme: ThemeConfig;
321
+ readonly dark_theme: ThemeConfig;
322
+ readonly tokens: Readonly<Record<string, ThemeToken>>;
323
+ readonly semantic_tokens: ThemeObject;
324
+ readonly highlight_theme: ThemeObject;
325
+ readonly font_family: string;
326
+ readonly font_size: number;
327
+ readonly mono_font_family: string;
328
+ readonly mono_font_size: number;
329
+ readonly radius: number;
330
+ readonly radius_lg: number;
331
+ readonly window_radius: number;
332
+ readonly shadow: boolean;
333
+ readonly focus_ring: boolean;
334
+ readonly transparent: string;
335
+ readonly scrollbar_mode: string;
336
+ readonly list: ThemeObject;
337
+ readonly sheet: ThemeObject;
338
+ }
339
+
340
+ function object(value: ThemeValue | undefined): value is ThemeObject {
341
+ return value !== null && typeof value === "object" && !Array.isArray(value);
342
+ }
343
+ const mode = (value: unknown): value is ThemeMode => value === "light" || value === "dark";
344
+ const color = (value: unknown): value is string =>
345
+ typeof value === "string" && value.length <= 128 && CSS.supports("color", value);
346
+
347
+ /** Clone/freeze the complete JSON tree; never project it onto a field whitelist. */
348
+ export function decodeTheme(value: unknown): Readonly<Theme> | null {
349
+ let nodes = 0;
350
+ let characters = 0;
351
+ const ancestors = new Set<object>();
352
+ const clone = (value: unknown, depth: number): ThemeValue => {
353
+ if (++nodes > 50000 || depth > 32) throw new Error("Theme exceeds limits");
354
+ if (value === null || typeof value === "boolean") return value;
355
+ if (typeof value === "number" && Number.isFinite(value)) return value;
356
+ if (typeof value === "string") {
357
+ characters += value.length;
358
+ if (value.length > 65536 || characters > 1024 * 1024) throw new Error("Theme exceeds limits");
359
+ return value;
360
+ }
361
+ if (typeof value !== "object" || ancestors.has(value)) throw new Error("Invalid theme JSON");
362
+ const prototype = Object.getPrototypeOf(value);
363
+ if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
364
+ throw new Error("Invalid theme object");
365
+ ancestors.add(value);
366
+ try {
367
+ if (Array.isArray(value)) {
368
+ if (value.length > 50000 - nodes) throw new Error("Theme exceeds limits");
369
+ return Object.freeze(Array.from(value, item => clone(item, depth + 1)));
370
+ }
371
+ const source = Object.entries(value);
372
+ if (source.length > 50000 - nodes) throw new Error("Theme exceeds limits");
373
+ const entries = source.map(([key, item]) => {
374
+ characters += key.length;
375
+ if (key.length > 256 || characters > 1024 * 1024) throw new Error("Theme exceeds limits");
376
+ return [key, clone(item, depth + 1)] as const;
377
+ });
378
+ return Object.freeze(Object.fromEntries(entries));
379
+ } finally { ancestors.delete(value); }
380
+ };
381
+ try {
382
+ const theme = clone(value, 0);
383
+ if (!object(theme) || !mode(theme.mode)) return null;
384
+ const colors = theme.colors;
385
+ if (!object(colors) || !THEME_COLOR_KEYS.every(key => color(colors[key]))) return null;
386
+ if (!Object.values(colors).every(color)) return null;
387
+ for (const [key, expected] of [["light_theme", "light"], ["dark_theme", "dark"]] as const) {
388
+ const config = theme[key];
389
+ if (!object(config) || config.mode !== expected || typeof config.name !== "string" || !object(config.colors)) return null;
390
+ if (!Object.values(config.colors).every(value => value === null || typeof value === "string")) return null;
391
+ for (const key of ["font.size", "mono_font.size", "radius", "radius.lg", "window.radius"]) {
392
+ const value = config[key];
393
+ if (value !== undefined && value !== null && (typeof value !== "number" || value < 0)) return null;
394
+ }
395
+ for (const key of ["font.family", "mono_font.family"]) {
396
+ if (config[key] !== undefined && config[key] !== null && typeof config[key] !== "string") return null;
397
+ }
398
+ if (config.is_default !== undefined && typeof config.is_default !== "boolean") return null;
399
+ if (config.shadow !== undefined && config.shadow !== null && typeof config.shadow !== "boolean") return null;
400
+ if (config.highlight !== undefined && config.highlight !== null && !object(config.highlight)) return null;
401
+ }
402
+ for (const key of ["font_family", "mono_font_family", "scrollbar_mode"]) {
403
+ if (typeof theme[key] !== "string") return null;
404
+ }
405
+ for (const key of ["font_size", "mono_font_size", "radius", "radius_lg", "window_radius"]) {
406
+ const value = theme[key];
407
+ if (typeof value !== "number" || value < 0) return null;
408
+ }
409
+ if (typeof theme.shadow !== "boolean" || typeof theme.focus_ring !== "boolean" || !color(theme.transparent)) return null;
410
+ for (const key of ["tokens", "semantic_tokens", "highlight_theme", "list", "sheet"]) {
411
+ if (!object(theme[key])) return null;
412
+ }
413
+ for (const token of Object.values(theme.tokens as ThemeObject)) {
414
+ if (!object(token) || !color(token.color) || !("background" in token)) return null;
415
+ }
416
+ return theme as Theme;
417
+ } catch { return null; }
418
+ }