@pancake-apps/web 0.0.0-snapshot-20260125200133

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.
@@ -0,0 +1,674 @@
1
+ import { AppsSdkBridge, AppsSdkStore } from './chunk-5NYJ2IVD.js';
2
+ import { McpAppsBridge, McpAppsStore, initialize } from './chunk-YGGRUIUG.js';
3
+
4
+ // src/unified/detection.ts
5
+ function isAppsSdkAvailable() {
6
+ return typeof window !== "undefined" && window.openai != null;
7
+ }
8
+ function isMcpAppsAvailable() {
9
+ if (typeof window === "undefined") return false;
10
+ if (window.__PANCAKE_FORCE_MCP__) return true;
11
+ try {
12
+ return window.self !== window.top;
13
+ } catch {
14
+ return true;
15
+ }
16
+ }
17
+ function detectPlatform() {
18
+ if (isAppsSdkAvailable()) {
19
+ return "apps-sdk";
20
+ }
21
+ return "mcp-apps";
22
+ }
23
+ var cachedPlatform = null;
24
+ function getPlatform() {
25
+ if (cachedPlatform === null) {
26
+ cachedPlatform = detectPlatform();
27
+ }
28
+ return cachedPlatform;
29
+ }
30
+ function resetPlatformCache() {
31
+ cachedPlatform = null;
32
+ }
33
+
34
+ // src/unified/defaults.ts
35
+ var SSR_DEFAULTS = {
36
+ theme: "light",
37
+ locale: "en-US",
38
+ displayMode: "inline",
39
+ maxHeight: 400,
40
+ safeArea: { top: 0, right: 0, bottom: 0, left: 0 },
41
+ containerSize: { width: 0, height: 400 },
42
+ containerWidth: 0,
43
+ containerHeight: 400,
44
+ platform: "web",
45
+ deviceType: "desktop",
46
+ canHover: true,
47
+ hasCoarsePointer: false,
48
+ inputCapabilities: { canHover: true, hasCoarsePointer: false },
49
+ toolInput: void 0,
50
+ toolInputPartial: void 0,
51
+ toolOutput: void 0,
52
+ toolResponseMetadata: void 0,
53
+ viewState: void 0
54
+ };
55
+
56
+ // src/unified/base-adaptor.ts
57
+ var BaseAdaptor = class {
58
+ /**
59
+ * Shared implementation for getting SSR snapshot values
60
+ */
61
+ getServerSnapshotForKey(key) {
62
+ return SSR_DEFAULTS[key];
63
+ }
64
+ /**
65
+ * Shared implementation for getting snapshot values
66
+ * Maps keys to the appropriate getter methods
67
+ */
68
+ getSnapshotForKey(key) {
69
+ switch (key) {
70
+ case "theme":
71
+ return this.getTheme();
72
+ case "locale":
73
+ return this.getLocale();
74
+ case "displayMode":
75
+ return this.getDisplayMode();
76
+ case "maxHeight":
77
+ return this.getMaxHeight();
78
+ case "safeArea":
79
+ return this.getSafeArea();
80
+ case "containerSize":
81
+ return this.getContainerSize();
82
+ case "platform":
83
+ return this.getPlatform();
84
+ case "deviceType":
85
+ return this.getDeviceType();
86
+ case "containerWidth":
87
+ return this.getContainerSize().width;
88
+ case "containerHeight":
89
+ return this.getContainerSize().height;
90
+ case "inputCapabilities":
91
+ return this.getInputCapabilities();
92
+ case "canHover":
93
+ return this.getInputCapabilities().canHover;
94
+ case "hasCoarsePointer":
95
+ return this.getInputCapabilities().hasCoarsePointer;
96
+ case "toolInput":
97
+ return this.getToolInput();
98
+ case "toolOutput":
99
+ return this.getToolOutput();
100
+ case "toolResponseMetadata":
101
+ return this.getToolResponseMetadata();
102
+ case "viewState":
103
+ return this.getViewState();
104
+ case "toolInputPartial":
105
+ return this.getToolInputPartial();
106
+ default:
107
+ return void 0;
108
+ }
109
+ }
110
+ };
111
+
112
+ // src/unified/apps-sdk-adaptor.ts
113
+ function mapDisplayMode(mode) {
114
+ return mode;
115
+ }
116
+ function deriveDeviceType(userAgent) {
117
+ if (!userAgent) return "desktop";
118
+ const ua = userAgent.toLowerCase();
119
+ if (/mobile|iphone|android.*mobile/.test(ua)) return "mobile";
120
+ if (/tablet|ipad|android(?!.*mobile)/.test(ua)) return "tablet";
121
+ return "desktop";
122
+ }
123
+ function derivePlatform(userAgent) {
124
+ if (!userAgent) return "web";
125
+ const ua = userAgent.toLowerCase();
126
+ if (/iphone|ipad|ipod/.test(ua)) return "ios";
127
+ if (/android/.test(ua)) return "android";
128
+ if (/macintosh|mac os/.test(ua)) return "macos";
129
+ if (/windows/.test(ua)) return "windows";
130
+ return "web";
131
+ }
132
+ var AppsSdkAdaptor = class extends BaseAdaptor {
133
+ platform = "apps-sdk";
134
+ bridge;
135
+ store;
136
+ externalStoreCache = /* @__PURE__ */ new Map();
137
+ constructor() {
138
+ super();
139
+ this.bridge = AppsSdkBridge.getInstance();
140
+ this.store = AppsSdkStore.getInstance();
141
+ }
142
+ // ============================================
143
+ // External Store Access
144
+ // ============================================
145
+ getExternalStore(key) {
146
+ let cached = this.externalStoreCache.get(key);
147
+ if (!cached) {
148
+ cached = {
149
+ subscribe: (callback) => {
150
+ const appsSdkKey = this.mapKeyToAppsSdk(key);
151
+ if (appsSdkKey) {
152
+ return this.store.subscribe(appsSdkKey, callback);
153
+ }
154
+ return this.subscribeForComputedKey(key, callback);
155
+ },
156
+ getSnapshot: () => this.getSnapshotForKey(key),
157
+ getServerSnapshot: () => this.getServerSnapshotForKey(key)
158
+ };
159
+ this.externalStoreCache.set(key, cached);
160
+ }
161
+ return cached;
162
+ }
163
+ mapKeyToAppsSdk(key) {
164
+ const mapping = {
165
+ theme: "theme",
166
+ locale: "locale",
167
+ displayMode: "displayMode",
168
+ maxHeight: "maxHeight",
169
+ safeArea: "safeArea",
170
+ toolInput: "toolInput",
171
+ toolOutput: "toolOutput",
172
+ toolResponseMetadata: "toolResponseMetadata",
173
+ viewState: "widgetState"
174
+ };
175
+ return mapping[key] ?? null;
176
+ }
177
+ subscribeForComputedKey(key, callback) {
178
+ if (key === "platform" || key === "deviceType" || key === "canHover" || key === "hasCoarsePointer") {
179
+ return this.store.subscribe("userAgent", callback);
180
+ }
181
+ if (key === "containerWidth" || key === "containerHeight") {
182
+ return this.store.subscribe("maxHeight", callback);
183
+ }
184
+ return () => {
185
+ };
186
+ }
187
+ // ============================================
188
+ // Direct Getters
189
+ // ============================================
190
+ getTheme() {
191
+ return this.store.getSnapshot("theme") ?? "light";
192
+ }
193
+ getLocale() {
194
+ return this.store.getSnapshot("locale") ?? "en-US";
195
+ }
196
+ getDisplayMode() {
197
+ const mode = this.store.getSnapshot("displayMode") ?? "inline";
198
+ return mapDisplayMode(mode);
199
+ }
200
+ getMaxHeight() {
201
+ return this.store.getSnapshot("maxHeight") ?? 400;
202
+ }
203
+ getSafeArea() {
204
+ return this.store.getSnapshot("safeArea") ?? { top: 0, right: 0, bottom: 0, left: 0 };
205
+ }
206
+ getPlatform() {
207
+ const userAgent = this.store.getSnapshot("userAgent");
208
+ return derivePlatform(userAgent);
209
+ }
210
+ getDeviceType() {
211
+ const userAgent = this.store.getSnapshot("userAgent");
212
+ return deriveDeviceType(userAgent);
213
+ }
214
+ getContainerSize() {
215
+ const maxHeight = this.getMaxHeight();
216
+ return {
217
+ width: typeof window !== "undefined" ? window.innerWidth : 0,
218
+ height: maxHeight
219
+ };
220
+ }
221
+ getInputCapabilities() {
222
+ const deviceType = this.getDeviceType();
223
+ const isMobile = deviceType === "mobile" || deviceType === "tablet";
224
+ return {
225
+ canHover: !isMobile,
226
+ hasCoarsePointer: isMobile
227
+ };
228
+ }
229
+ getToolInput() {
230
+ return this.store.getSnapshot("toolInput");
231
+ }
232
+ getToolOutput() {
233
+ return this.store.getSnapshot("toolOutput");
234
+ }
235
+ getToolResponseMetadata() {
236
+ return this.store.getSnapshot("toolResponseMetadata");
237
+ }
238
+ getViewState() {
239
+ return this.store.getSnapshot("widgetState");
240
+ }
241
+ getToolInputPartial() {
242
+ return void 0;
243
+ }
244
+ // ============================================
245
+ // Actions
246
+ // ============================================
247
+ async callTool(name, args) {
248
+ return this.bridge.callTool(name, args);
249
+ }
250
+ sendFollowUpMessage(message) {
251
+ this.bridge.sendFollowUpMessage(message);
252
+ }
253
+ openExternal(url) {
254
+ this.bridge.openExternal(url);
255
+ }
256
+ requestDisplayMode(mode) {
257
+ this.bridge.requestDisplayMode(mode);
258
+ }
259
+ setViewState(state) {
260
+ this.bridge.setWidgetState(state);
261
+ }
262
+ // ============================================
263
+ // Feature Detection
264
+ // ============================================
265
+ isSupported(feature) {
266
+ const supported = [
267
+ "uploadFile",
268
+ "getFileDownloadUrl",
269
+ "requestModal",
270
+ "requestClose",
271
+ "setOpenInAppUrl",
272
+ "notifyIntrinsicHeight",
273
+ "sizeReporting",
274
+ "callTool",
275
+ "sendMessage",
276
+ "openLink",
277
+ "requestDisplayMode"
278
+ ];
279
+ return supported.includes(feature);
280
+ }
281
+ // ============================================
282
+ // MCP-Specific Stubs (with console warnings)
283
+ // ============================================
284
+ async readResource(_uri) {
285
+ console.warn("[Pancake] readResource is not supported on Apps SDK");
286
+ throw new Error("readResource is not supported on Apps SDK");
287
+ }
288
+ async updateModelContext(_params) {
289
+ console.warn("[Pancake] updateModelContext is not supported on Apps SDK");
290
+ }
291
+ log(level, data, logger) {
292
+ const loggerPrefix = logger ? `[${logger}]` : "[Pancake]";
293
+ switch (level) {
294
+ case "debug":
295
+ console.debug(loggerPrefix, data);
296
+ break;
297
+ case "info":
298
+ console.info(loggerPrefix, data);
299
+ break;
300
+ case "warning":
301
+ console.warn(loggerPrefix, data);
302
+ break;
303
+ case "error":
304
+ console.error(loggerPrefix, data);
305
+ break;
306
+ }
307
+ }
308
+ async ping() {
309
+ return Promise.resolve();
310
+ }
311
+ onTeardown(_handler) {
312
+ console.warn("[Pancake] onTeardown is not supported on Apps SDK");
313
+ return () => {
314
+ };
315
+ }
316
+ notifySizeChanged(_width, height) {
317
+ this.bridge.notifyIntrinsicHeight?.(height);
318
+ }
319
+ setupAutoSizeReporting(element) {
320
+ if (typeof ResizeObserver === "undefined" || !this.bridge.notifyIntrinsicHeight) {
321
+ return () => {
322
+ };
323
+ }
324
+ const observer = new ResizeObserver((entries) => {
325
+ const entry = entries[0];
326
+ if (entry) {
327
+ this.bridge.notifyIntrinsicHeight?.(entry.contentRect.height);
328
+ }
329
+ });
330
+ observer.observe(element);
331
+ return () => {
332
+ observer.disconnect();
333
+ };
334
+ }
335
+ // ============================================
336
+ // Initialization
337
+ // ============================================
338
+ async ensureReady() {
339
+ return Promise.resolve();
340
+ }
341
+ };
342
+
343
+ // src/unified/mcp-apps-adaptor.ts
344
+ function mapPlatform(platform, userAgent) {
345
+ if (userAgent) {
346
+ const ua = userAgent.toLowerCase();
347
+ if (/iphone|ipad|ipod/.test(ua)) return "ios";
348
+ if (/android/.test(ua)) return "android";
349
+ if (/macintosh|mac os/.test(ua)) return "macos";
350
+ if (/windows/.test(ua)) return "windows";
351
+ }
352
+ switch (platform) {
353
+ case "mobile":
354
+ return "web";
355
+ case "desktop":
356
+ return "web";
357
+ case "web":
358
+ default:
359
+ return "web";
360
+ }
361
+ }
362
+ function deriveDeviceType2(platform) {
363
+ switch (platform) {
364
+ case "mobile":
365
+ return "mobile";
366
+ case "desktop":
367
+ case "web":
368
+ default:
369
+ return "desktop";
370
+ }
371
+ }
372
+ var McpAppsAdaptor = class extends BaseAdaptor {
373
+ platform = "mcp-apps";
374
+ bridge;
375
+ store;
376
+ initPromise = null;
377
+ viewStateKey = "pancake-view-state:default";
378
+ viewStateSubscribers = /* @__PURE__ */ new Set();
379
+ externalStoreCache = /* @__PURE__ */ new Map();
380
+ constructor() {
381
+ super();
382
+ this.bridge = McpAppsBridge.getInstance();
383
+ this.store = McpAppsStore.getInstance();
384
+ this.setupStorageListener();
385
+ }
386
+ /**
387
+ * Setup localStorage listener for viewState cross-tab synchronization
388
+ */
389
+ setupStorageListener() {
390
+ if (typeof window === "undefined") return;
391
+ window.addEventListener("storage", (event) => {
392
+ if (event.key === this.viewStateKey) {
393
+ this.notifyViewStateSubscribers();
394
+ }
395
+ });
396
+ }
397
+ /**
398
+ * Notify all viewState subscribers of changes
399
+ */
400
+ notifyViewStateSubscribers() {
401
+ this.viewStateSubscribers.forEach((callback) => callback());
402
+ }
403
+ /**
404
+ * Get the localStorage key for view state, derived from toolInfo
405
+ */
406
+ getViewStateKey() {
407
+ const hostContext = this.store.getSnapshot("hostContext");
408
+ const toolName = hostContext?.toolInfo?.tool?.name;
409
+ if (toolName && this.viewStateKey === "pancake-view-state:default") {
410
+ this.viewStateKey = `pancake-view-state:${toolName}`;
411
+ }
412
+ return this.viewStateKey;
413
+ }
414
+ // ============================================
415
+ // External Store Access
416
+ // ============================================
417
+ getExternalStore(key) {
418
+ let cached = this.externalStoreCache.get(key);
419
+ if (!cached) {
420
+ cached = {
421
+ subscribe: (callback) => {
422
+ return this.subscribeForKey(key, callback);
423
+ },
424
+ getSnapshot: () => this.getSnapshotForKey(key),
425
+ getServerSnapshot: () => this.getServerSnapshotForKey(key)
426
+ };
427
+ this.externalStoreCache.set(key, cached);
428
+ }
429
+ return cached;
430
+ }
431
+ subscribeForKey(key, callback) {
432
+ const hostContextKeys = [
433
+ "theme",
434
+ "locale",
435
+ "displayMode",
436
+ "maxHeight",
437
+ "safeArea",
438
+ "containerSize",
439
+ "platform",
440
+ "deviceType",
441
+ "containerWidth",
442
+ "containerHeight",
443
+ "inputCapabilities",
444
+ "canHover",
445
+ "hasCoarsePointer"
446
+ ];
447
+ if (hostContextKeys.includes(key)) {
448
+ return this.store.subscribe("hostContext", callback);
449
+ }
450
+ if (key === "toolInput") {
451
+ return this.store.subscribe("toolInput", callback);
452
+ }
453
+ if (key === "toolOutput") {
454
+ return this.store.subscribe("toolResult", callback);
455
+ }
456
+ if (key === "toolResponseMetadata") {
457
+ return this.store.subscribe("toolResult", callback);
458
+ }
459
+ if (key === "toolInputPartial") {
460
+ return this.store.subscribe("toolInputPartial", callback);
461
+ }
462
+ if (key === "viewState") {
463
+ this.viewStateSubscribers.add(callback);
464
+ return () => {
465
+ this.viewStateSubscribers.delete(callback);
466
+ };
467
+ }
468
+ return () => {
469
+ };
470
+ }
471
+ // ============================================
472
+ // Direct Getters
473
+ // ============================================
474
+ getTheme() {
475
+ const hostContext = this.store.getSnapshot("hostContext");
476
+ return hostContext.theme ?? "light";
477
+ }
478
+ getLocale() {
479
+ const hostContext = this.store.getSnapshot("hostContext");
480
+ return hostContext.locale ?? "en-US";
481
+ }
482
+ getDisplayMode() {
483
+ const hostContext = this.store.getSnapshot("hostContext");
484
+ return hostContext.displayMode ?? "inline";
485
+ }
486
+ getMaxHeight() {
487
+ const hostContext = this.store.getSnapshot("hostContext");
488
+ const dims = hostContext.containerDimensions;
489
+ return dims?.maxHeight ?? dims?.height ?? 400;
490
+ }
491
+ getSafeArea() {
492
+ const hostContext = this.store.getSnapshot("hostContext");
493
+ return hostContext.safeAreaInsets ?? { top: 0, right: 0, bottom: 0, left: 0 };
494
+ }
495
+ getPlatform() {
496
+ const hostContext = this.store.getSnapshot("hostContext");
497
+ return mapPlatform(hostContext.platform, hostContext.userAgent);
498
+ }
499
+ getDeviceType() {
500
+ const hostContext = this.store.getSnapshot("hostContext");
501
+ return deriveDeviceType2(hostContext.platform);
502
+ }
503
+ getContainerSize() {
504
+ const hostContext = this.store.getSnapshot("hostContext");
505
+ const dims = hostContext.containerDimensions;
506
+ return {
507
+ width: dims?.maxWidth ?? dims?.width ?? (typeof window !== "undefined" ? window.innerWidth : 0),
508
+ height: dims?.maxHeight ?? dims?.height ?? 400
509
+ };
510
+ }
511
+ getInputCapabilities() {
512
+ const hostContext = this.store.getSnapshot("hostContext");
513
+ const caps = hostContext.deviceCapabilities;
514
+ return {
515
+ canHover: caps?.hover ?? true,
516
+ hasCoarsePointer: caps?.touch ?? false
517
+ };
518
+ }
519
+ getToolInput() {
520
+ return this.store.getSnapshot("toolInput");
521
+ }
522
+ getToolOutput() {
523
+ const result = this.store.getSnapshot("toolResult");
524
+ return result?.structuredContent;
525
+ }
526
+ getToolResponseMetadata() {
527
+ const result = this.store.getSnapshot("toolResult");
528
+ return result?._meta;
529
+ }
530
+ getViewState() {
531
+ if (typeof window === "undefined") return void 0;
532
+ try {
533
+ const key = this.getViewStateKey();
534
+ const stored = localStorage.getItem(key);
535
+ return stored ? JSON.parse(stored) : void 0;
536
+ } catch {
537
+ return void 0;
538
+ }
539
+ }
540
+ getToolInputPartial() {
541
+ return this.store.getSnapshot("toolInputPartial");
542
+ }
543
+ // ============================================
544
+ // Actions
545
+ // ============================================
546
+ async callTool(name, args) {
547
+ await this.ensureReady();
548
+ return this.bridge.callTool(name, args);
549
+ }
550
+ sendFollowUpMessage(message) {
551
+ this.ensureReady().then(() => {
552
+ this.bridge.sendMessage(message);
553
+ }).catch((error) => {
554
+ console.warn("[pancake] sendFollowUpMessage failed:", error);
555
+ });
556
+ }
557
+ openExternal(url) {
558
+ this.ensureReady().then(() => {
559
+ this.bridge.openLink(url);
560
+ }).catch((error) => {
561
+ console.warn("[pancake] openExternal failed:", error);
562
+ });
563
+ }
564
+ requestDisplayMode(mode) {
565
+ this.ensureReady().then(() => {
566
+ this.bridge.requestDisplayMode(mode);
567
+ }).catch((error) => {
568
+ console.warn("[pancake] requestDisplayMode failed:", error);
569
+ });
570
+ }
571
+ setViewState(state) {
572
+ if (typeof window !== "undefined") {
573
+ try {
574
+ const key = this.getViewStateKey();
575
+ localStorage.setItem(key, JSON.stringify(state));
576
+ this.notifyViewStateSubscribers();
577
+ } catch {
578
+ }
579
+ }
580
+ this.ensureReady().then(() => {
581
+ this.bridge.updateModelContext({
582
+ structuredContent: { __viewState: state }
583
+ });
584
+ }).catch((error) => {
585
+ console.warn("[pancake] setViewState updateModelContext failed:", error);
586
+ });
587
+ }
588
+ // ============================================
589
+ // MCP-Specific Actions
590
+ // ============================================
591
+ async readResource(uri) {
592
+ await this.ensureReady();
593
+ return this.bridge.readResource(uri);
594
+ }
595
+ async updateModelContext(params) {
596
+ await this.ensureReady();
597
+ return this.bridge.updateModelContext(params);
598
+ }
599
+ log(level, data, logger) {
600
+ this.bridge.log(level, data, logger);
601
+ }
602
+ async ping() {
603
+ await this.ensureReady();
604
+ return this.bridge.ping();
605
+ }
606
+ onTeardown(handler) {
607
+ return this.bridge.onTeardown(handler);
608
+ }
609
+ notifySizeChanged(width, height) {
610
+ this.bridge.notifySizeChanged(width, height);
611
+ }
612
+ setupAutoSizeReporting(element) {
613
+ return this.bridge.setupAutoSizeReporting(element);
614
+ }
615
+ // ============================================
616
+ // Feature Detection
617
+ // ============================================
618
+ isSupported(feature) {
619
+ const supported = [
620
+ "readResource",
621
+ "updateModelContext",
622
+ "timezone",
623
+ "cssVariables",
624
+ "toolInfo",
625
+ "logging",
626
+ "ping",
627
+ "onTeardown",
628
+ "callTool",
629
+ "sendMessage",
630
+ "openLink",
631
+ "requestDisplayMode",
632
+ "sizeReporting"
633
+ ];
634
+ return supported.includes(feature);
635
+ }
636
+ // ============================================
637
+ // Initialization
638
+ // ============================================
639
+ async initialize() {
640
+ if (!this.initPromise) {
641
+ this.initPromise = initialize().then(() => {
642
+ });
643
+ }
644
+ return this.initPromise;
645
+ }
646
+ async ensureReady() {
647
+ if (!this.bridge.isInitialized) {
648
+ await this.initialize();
649
+ }
650
+ }
651
+ };
652
+
653
+ // src/unified/adaptor.ts
654
+ var cachedAdaptor = null;
655
+ function getAdaptor() {
656
+ if (cachedAdaptor) {
657
+ return cachedAdaptor;
658
+ }
659
+ const platform = getPlatform();
660
+ if (platform === "apps-sdk") {
661
+ cachedAdaptor = new AppsSdkAdaptor();
662
+ } else {
663
+ cachedAdaptor = new McpAppsAdaptor();
664
+ }
665
+ return cachedAdaptor;
666
+ }
667
+ function resetAdaptor() {
668
+ cachedAdaptor = null;
669
+ resetPlatformCache();
670
+ }
671
+
672
+ export { SSR_DEFAULTS, getAdaptor, getPlatform, isAppsSdkAvailable, isMcpAppsAvailable, resetAdaptor };
673
+ //# sourceMappingURL=chunk-ZYBPDIEG.js.map
674
+ //# sourceMappingURL=chunk-ZYBPDIEG.js.map