@web-applets/sdk 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +69 -99
- package/dist/components/applet-frame.d.ts +19 -0
- package/dist/components/applet-frame.js +93 -0
- package/dist/core/applet.d.ts +21 -0
- package/dist/core/applet.js +117 -0
- package/dist/core/context.d.ts +32 -0
- package/dist/core/context.js +138 -0
- package/dist/core/host.d.ts +31 -0
- package/dist/core/host.js +134 -0
- package/dist/core/shared.d.ts +121 -0
- package/dist/core/shared.js +171 -0
- package/dist/index.d.ts +9 -5
- package/dist/index.js +9 -6
- package/dist/lib/utils.d.ts +17 -0
- package/dist/lib/utils.js +37 -0
- package/dist/utils.d.ts +3 -4
- package/dist/utils.js +10 -30
- package/package.json +1 -1
- package/dist/client.d.ts +0 -32
- package/dist/client.js +0 -190
- package/dist/context.d.ts +0 -27
- package/dist/context.js +0 -136
- package/dist/types.d.ts +0 -73
- package/dist/types.js +0 -21
- package/dist/web-component/index.d.ts +0 -1
- package/dist/web-component/index.js +0 -52
- package/dist/web-components/applet-frame.d.ts +0 -1
- package/dist/web-components/applet-frame.js +0 -52
@@ -0,0 +1,31 @@
|
|
1
|
+
import { AppletAction, AppletMessage, ActionParams, AppletManifest, AppletDataEvent, AppletResizeEvent, AppletActionsEvent, AppletMessageRelay } from './shared';
|
2
|
+
interface LoadOpts {
|
3
|
+
unsafe?: boolean;
|
4
|
+
}
|
5
|
+
declare function load(url: string, container?: HTMLIFrameElement, opts?: LoadOpts): Promise<Applet>;
|
6
|
+
interface AppletOptions {
|
7
|
+
manifest: AppletManifest;
|
8
|
+
container: HTMLIFrameElement;
|
9
|
+
}
|
10
|
+
declare class Applet<T = any> extends EventTarget {
|
11
|
+
#private;
|
12
|
+
messageRelay: AppletMessageRelay;
|
13
|
+
url: string;
|
14
|
+
actions: AppletAction[];
|
15
|
+
container: HTMLIFrameElement;
|
16
|
+
type: string;
|
17
|
+
constructor(options: AppletOptions);
|
18
|
+
initializeListeners(): void;
|
19
|
+
get data(): T;
|
20
|
+
set data(data: T);
|
21
|
+
get manifest(): AppletManifest;
|
22
|
+
onresize(event: AppletResizeEvent): void;
|
23
|
+
onactions(event: AppletActionsEvent): void;
|
24
|
+
ondata(event: AppletDataEvent): void;
|
25
|
+
disconnect(): void;
|
26
|
+
dispatchAction(actionId: string, params: ActionParams): Promise<AppletMessage>;
|
27
|
+
}
|
28
|
+
export declare const applets: {
|
29
|
+
load: typeof load;
|
30
|
+
};
|
31
|
+
export { Applet };
|
@@ -0,0 +1,134 @@
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6
|
+
};
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
+
};
|
12
|
+
var _Applet_manifest, _Applet_data;
|
13
|
+
import { AppletMessage, AppletDataMessage, AppletInitMessage, AppletDataEvent, AppletResizeEvent, AppletActionsEvent, AppletMessageRelay, } from './shared';
|
14
|
+
import { parseUrl } from '../lib/utils';
|
15
|
+
// Container for initializing applets without an explicit container
|
16
|
+
const hiddenContainer = document.createElement('iframe');
|
17
|
+
hiddenContainer.style.display = 'none';
|
18
|
+
document.body.appendChild(hiddenContainer);
|
19
|
+
const defaultOpts = {
|
20
|
+
unsafe: false,
|
21
|
+
};
|
22
|
+
// Load an applet object from a URL
|
23
|
+
async function load(url, container, opts) {
|
24
|
+
const _opts = Object.assign(defaultOpts, opts ?? {});
|
25
|
+
if (!container)
|
26
|
+
container = hiddenContainer;
|
27
|
+
url = parseUrl(url);
|
28
|
+
const manifest = await loadManifest(`${url}`);
|
29
|
+
// If unsafe enabled, allow same origin sandbox
|
30
|
+
// This is required for e.g. YouTube embeds
|
31
|
+
if (_opts.unsafe && manifest.unsafe) {
|
32
|
+
container.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
container.setAttribute('sandbox', 'allow-scripts allow-forms');
|
36
|
+
}
|
37
|
+
// Load the applet
|
38
|
+
const applet = new Applet({
|
39
|
+
manifest,
|
40
|
+
container,
|
41
|
+
});
|
42
|
+
return new Promise((resolve) => {
|
43
|
+
applet.messageRelay.on('ready', () => {
|
44
|
+
resolve(applet);
|
45
|
+
});
|
46
|
+
});
|
47
|
+
}
|
48
|
+
class Applet extends EventTarget {
|
49
|
+
constructor(options) {
|
50
|
+
super();
|
51
|
+
this.actions = [];
|
52
|
+
_Applet_manifest.set(this, void 0);
|
53
|
+
this.type = 'host';
|
54
|
+
_Applet_data.set(this, void 0);
|
55
|
+
this.container = options.container;
|
56
|
+
this.container.src = options.manifest.start_url;
|
57
|
+
this.messageRelay = new AppletMessageRelay(this.container.contentWindow);
|
58
|
+
__classPrivateFieldSet(this, _Applet_manifest, options.manifest, "f");
|
59
|
+
this.initializeListeners();
|
60
|
+
this.messageRelay.on('ready', () => {
|
61
|
+
this.messageRelay.send(new AppletInitMessage({ manifest: options.manifest }));
|
62
|
+
});
|
63
|
+
}
|
64
|
+
initializeListeners() {
|
65
|
+
this.messageRelay.on('data', (message) => {
|
66
|
+
__classPrivateFieldSet(this, _Applet_data, message.data, "f");
|
67
|
+
const dataEvent = new AppletDataEvent({ data: message.data });
|
68
|
+
if (typeof this.ondata === 'function')
|
69
|
+
this.ondata(dataEvent);
|
70
|
+
this.dispatchEvent(dataEvent);
|
71
|
+
});
|
72
|
+
this.messageRelay.on('resize', (message) => {
|
73
|
+
const resizeEvent = new AppletResizeEvent({
|
74
|
+
dimensions: message.dimensions,
|
75
|
+
});
|
76
|
+
if (typeof this.onresize === 'function')
|
77
|
+
this.onresize(resizeEvent);
|
78
|
+
this.dispatchEvent(resizeEvent);
|
79
|
+
});
|
80
|
+
this.messageRelay.on('actions', (message) => {
|
81
|
+
this.actions = message.actions;
|
82
|
+
const actionsEvent = new AppletActionsEvent({ actions: message.actions });
|
83
|
+
if (typeof this.onactions === 'function')
|
84
|
+
this.onactions(actionsEvent);
|
85
|
+
this.dispatchEvent(actionsEvent);
|
86
|
+
});
|
87
|
+
}
|
88
|
+
get data() {
|
89
|
+
return __classPrivateFieldGet(this, _Applet_data, "f");
|
90
|
+
}
|
91
|
+
set data(data) {
|
92
|
+
__classPrivateFieldSet(this, _Applet_data, data, "f");
|
93
|
+
this.messageRelay.send(new AppletDataMessage({ data }));
|
94
|
+
}
|
95
|
+
get manifest() {
|
96
|
+
return __classPrivateFieldGet(this, _Applet_manifest, "f");
|
97
|
+
}
|
98
|
+
onresize(event) { }
|
99
|
+
onactions(event) { }
|
100
|
+
ondata(event) { }
|
101
|
+
disconnect() {
|
102
|
+
this.container.src = 'about:blank';
|
103
|
+
}
|
104
|
+
async dispatchAction(actionId, params) {
|
105
|
+
const actionMessage = new AppletMessage('action', {
|
106
|
+
actionId,
|
107
|
+
params,
|
108
|
+
});
|
109
|
+
return await this.messageRelay.send(actionMessage);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
_Applet_manifest = new WeakMap(), _Applet_data = new WeakMap();
|
113
|
+
// Loads a manifest and parses the JSON
|
114
|
+
async function loadManifest(baseUrl) {
|
115
|
+
baseUrl = parseUrl(baseUrl);
|
116
|
+
let manifest;
|
117
|
+
try {
|
118
|
+
const request = await fetch(`${baseUrl}/manifest.json`);
|
119
|
+
manifest = await request.json();
|
120
|
+
// TODO: Add verification this is a valid manifest
|
121
|
+
}
|
122
|
+
catch (e) {
|
123
|
+
console.error(e.message);
|
124
|
+
}
|
125
|
+
manifest.start_url = manifest.start_url
|
126
|
+
? parseUrl(manifest.start_url, baseUrl)
|
127
|
+
: baseUrl;
|
128
|
+
return manifest;
|
129
|
+
}
|
130
|
+
// Exports
|
131
|
+
export const applets = {
|
132
|
+
load,
|
133
|
+
};
|
134
|
+
export { Applet };
|
@@ -0,0 +1,121 @@
|
|
1
|
+
export interface AppletManifest {
|
2
|
+
name?: string;
|
3
|
+
short_name?: string;
|
4
|
+
icons: AppletIcons;
|
5
|
+
description?: string;
|
6
|
+
icon?: string;
|
7
|
+
display?: string;
|
8
|
+
start_url?: string;
|
9
|
+
unsafe?: boolean;
|
10
|
+
actions?: AppletAction[];
|
11
|
+
}
|
12
|
+
export interface AppletIcons {
|
13
|
+
src: string;
|
14
|
+
purpose?: string;
|
15
|
+
sizes?: string;
|
16
|
+
type?: string;
|
17
|
+
}
|
18
|
+
export interface AppletAction {
|
19
|
+
id: string;
|
20
|
+
name?: string;
|
21
|
+
description?: string;
|
22
|
+
params?: JSONSchemaProperties;
|
23
|
+
}
|
24
|
+
export type JSONSchemaProperties = Record<string, {
|
25
|
+
type: string;
|
26
|
+
description?: string;
|
27
|
+
properties?: JSONSchemaProperties;
|
28
|
+
}>;
|
29
|
+
export type ActionParams = Record<string, any>;
|
30
|
+
export declare function loadManifest(pageUrl: string): Promise<AppletManifest>;
|
31
|
+
interface SendMessageOptions {
|
32
|
+
resolves: boolean;
|
33
|
+
}
|
34
|
+
export declare class AppletMessageRelay {
|
35
|
+
target: Window;
|
36
|
+
constructor(target: Window);
|
37
|
+
send(message: AppletMessage, options?: SendMessageOptions): Promise<AppletMessage>;
|
38
|
+
on(messageType: AppletMessageType, callback: AppletMessageCallback): Promise<void>;
|
39
|
+
}
|
40
|
+
export declare class AppletMessage {
|
41
|
+
type: AppletMessageType;
|
42
|
+
id: string;
|
43
|
+
timeStamp: number;
|
44
|
+
[key: string]: any;
|
45
|
+
constructor(type: AppletMessageType, values?: {
|
46
|
+
[key: string]: any;
|
47
|
+
});
|
48
|
+
toJson(): {
|
49
|
+
[k: string]: any;
|
50
|
+
};
|
51
|
+
}
|
52
|
+
export declare class AppletResolveMessage extends AppletMessage {
|
53
|
+
messageId: string;
|
54
|
+
constructor({ id }: {
|
55
|
+
id: string;
|
56
|
+
});
|
57
|
+
}
|
58
|
+
export declare class AppletActionsMessage extends AppletMessage {
|
59
|
+
actions: AppletAction[];
|
60
|
+
constructor({ actions }: {
|
61
|
+
actions: AppletAction[];
|
62
|
+
});
|
63
|
+
}
|
64
|
+
export declare class AppletDataMessage<T = any> extends AppletMessage {
|
65
|
+
data: T;
|
66
|
+
constructor({ data }: {
|
67
|
+
data: T;
|
68
|
+
});
|
69
|
+
}
|
70
|
+
export declare class AppletReadyMessage extends AppletMessage {
|
71
|
+
constructor();
|
72
|
+
}
|
73
|
+
export declare class AppletResizeMessage extends AppletMessage {
|
74
|
+
dimensions: {
|
75
|
+
height: number;
|
76
|
+
width: number;
|
77
|
+
};
|
78
|
+
constructor({ dimensions, }: {
|
79
|
+
dimensions: AppletResizeMessage['dimensions'];
|
80
|
+
});
|
81
|
+
}
|
82
|
+
interface AppletActionMessageOptions {
|
83
|
+
actionId: string;
|
84
|
+
params: any;
|
85
|
+
}
|
86
|
+
export declare class AppletActionMessage extends AppletMessage {
|
87
|
+
actionId: string;
|
88
|
+
params: any;
|
89
|
+
constructor({ actionId, params }: AppletActionMessageOptions);
|
90
|
+
}
|
91
|
+
export declare class AppletInitMessage extends AppletMessage {
|
92
|
+
constructor();
|
93
|
+
}
|
94
|
+
export type AppletMessageType = 'action' | 'actions' | 'data' | 'init' | 'ready' | 'resolve' | 'resize';
|
95
|
+
export type AppletMessageCallback = (message: AppletMessage) => Promise<void> | void;
|
96
|
+
export declare class AppletDataEvent extends Event {
|
97
|
+
data: any;
|
98
|
+
constructor({ data }: {
|
99
|
+
data: any;
|
100
|
+
});
|
101
|
+
}
|
102
|
+
export declare class AppletReadyEvent extends Event {
|
103
|
+
constructor();
|
104
|
+
}
|
105
|
+
export declare class AppletLoadEvent extends Event {
|
106
|
+
constructor();
|
107
|
+
}
|
108
|
+
export declare class AppletActionsEvent extends Event {
|
109
|
+
actions: AppletAction[];
|
110
|
+
constructor({ actions }: {
|
111
|
+
actions: AppletAction[];
|
112
|
+
});
|
113
|
+
}
|
114
|
+
export interface AppletResizeEventOpts {
|
115
|
+
dimensions: AppletResizeMessage['dimensions'];
|
116
|
+
}
|
117
|
+
export declare class AppletResizeEvent extends Event {
|
118
|
+
dimensions: AppletResizeMessage['dimensions'];
|
119
|
+
constructor({ dimensions }: AppletResizeEventOpts);
|
120
|
+
}
|
121
|
+
export {};
|
@@ -0,0 +1,171 @@
|
|
1
|
+
/* Manifest & action definitions */
|
2
|
+
import { parseUrl } from '../utils';
|
3
|
+
export async function loadManifest(pageUrl) {
|
4
|
+
pageUrl = parseUrl(pageUrl);
|
5
|
+
let manifest;
|
6
|
+
try {
|
7
|
+
const pageRequest = await fetch(pageUrl);
|
8
|
+
const html = await pageRequest.text();
|
9
|
+
const parser = new DOMParser();
|
10
|
+
const doc = parser.parseFromString(html, 'text/html');
|
11
|
+
const linkElem = doc.querySelector('link[rel="manifest"]');
|
12
|
+
const manifestUrl = parseUrl(linkElem.href);
|
13
|
+
const manifestRequest = await fetch(manifestUrl);
|
14
|
+
manifest = await manifestRequest.json();
|
15
|
+
// TODO: Add verification this is a valid manifest
|
16
|
+
}
|
17
|
+
catch (e) {
|
18
|
+
return;
|
19
|
+
}
|
20
|
+
return manifest;
|
21
|
+
}
|
22
|
+
export class AppletMessageRelay {
|
23
|
+
constructor(target) {
|
24
|
+
this.target = target;
|
25
|
+
}
|
26
|
+
async send(message, options) {
|
27
|
+
this.target.postMessage(message.toJson(), '*');
|
28
|
+
if (options && options.resolves === false)
|
29
|
+
return;
|
30
|
+
// Wait for a resolve message to be sent back before completing await
|
31
|
+
return new Promise((resolve) => {
|
32
|
+
const listener = (messageEvent) => {
|
33
|
+
const responseMessage = new AppletMessage(messageEvent.data.type, messageEvent.data);
|
34
|
+
if (responseMessage.type === 'resolve' &&
|
35
|
+
responseMessage.id === message.id) {
|
36
|
+
window.removeEventListener('message', listener);
|
37
|
+
resolve(responseMessage);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
window.addEventListener('message', listener);
|
41
|
+
});
|
42
|
+
}
|
43
|
+
async on(messageType, callback) {
|
44
|
+
const listener = async (messageEvent) => {
|
45
|
+
if (messageEvent.source === window.self)
|
46
|
+
return;
|
47
|
+
if (messageEvent.data.type !== messageType)
|
48
|
+
return;
|
49
|
+
const message = new AppletMessage(messageEvent.data.type, messageEvent.data);
|
50
|
+
// Wait for the callback to complete, then send a 'resolve' event
|
51
|
+
// with the message ID.
|
52
|
+
await callback(message);
|
53
|
+
const resolveMessage = new AppletResolveMessage({ id: message.id });
|
54
|
+
this.send(resolveMessage, { resolves: false });
|
55
|
+
};
|
56
|
+
window.addEventListener('message', listener);
|
57
|
+
// TODO: Return something that I can then call .off or .removeListener, implement the
|
58
|
+
// rest of that event class
|
59
|
+
}
|
60
|
+
}
|
61
|
+
/* Messages */
|
62
|
+
export class AppletMessage {
|
63
|
+
constructor(type, values) {
|
64
|
+
this.timeStamp = Date.now();
|
65
|
+
this.type = type;
|
66
|
+
this.id = crypto.randomUUID();
|
67
|
+
if (values)
|
68
|
+
Object.assign(this, values);
|
69
|
+
}
|
70
|
+
toJson() {
|
71
|
+
return Object.fromEntries(Object.entries(this).filter(([_, value]) => {
|
72
|
+
try {
|
73
|
+
JSON.stringify(value);
|
74
|
+
return true;
|
75
|
+
}
|
76
|
+
catch {
|
77
|
+
return false;
|
78
|
+
}
|
79
|
+
}));
|
80
|
+
}
|
81
|
+
}
|
82
|
+
export class AppletResolveMessage extends AppletMessage {
|
83
|
+
constructor({ id }) {
|
84
|
+
super('resolve');
|
85
|
+
this.id = id;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
export class AppletActionsMessage extends AppletMessage {
|
89
|
+
constructor({ actions }) {
|
90
|
+
super('actions');
|
91
|
+
this.actions = actions;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
export class AppletDataMessage extends AppletMessage {
|
95
|
+
constructor({ data }) {
|
96
|
+
super('data');
|
97
|
+
this.data = data;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
export class AppletReadyMessage extends AppletMessage {
|
101
|
+
constructor() {
|
102
|
+
super('ready');
|
103
|
+
}
|
104
|
+
}
|
105
|
+
export class AppletResizeMessage extends AppletMessage {
|
106
|
+
constructor({ dimensions, }) {
|
107
|
+
super('resize');
|
108
|
+
this.dimensions = dimensions;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
export class AppletActionMessage extends AppletMessage {
|
112
|
+
constructor({ actionId, params }) {
|
113
|
+
super('action');
|
114
|
+
this.actionId = actionId;
|
115
|
+
this.params = params;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
export class AppletInitMessage extends AppletMessage {
|
119
|
+
constructor() {
|
120
|
+
super('init');
|
121
|
+
}
|
122
|
+
}
|
123
|
+
/* Events */
|
124
|
+
export class AppletDataEvent extends Event {
|
125
|
+
constructor({ data }) {
|
126
|
+
super('data', {
|
127
|
+
bubbles: false,
|
128
|
+
cancelable: false,
|
129
|
+
composed: false,
|
130
|
+
});
|
131
|
+
this.data = data;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
export class AppletReadyEvent extends Event {
|
135
|
+
constructor() {
|
136
|
+
super('ready', {
|
137
|
+
bubbles: false,
|
138
|
+
cancelable: false,
|
139
|
+
composed: false,
|
140
|
+
});
|
141
|
+
}
|
142
|
+
}
|
143
|
+
export class AppletLoadEvent extends Event {
|
144
|
+
constructor() {
|
145
|
+
super('load', {
|
146
|
+
bubbles: false,
|
147
|
+
cancelable: false,
|
148
|
+
composed: false,
|
149
|
+
});
|
150
|
+
}
|
151
|
+
}
|
152
|
+
export class AppletActionsEvent extends Event {
|
153
|
+
constructor({ actions }) {
|
154
|
+
super('actions', {
|
155
|
+
bubbles: false,
|
156
|
+
cancelable: false,
|
157
|
+
composed: false,
|
158
|
+
});
|
159
|
+
this.actions = actions;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
export class AppletResizeEvent extends Event {
|
163
|
+
constructor({ dimensions }) {
|
164
|
+
super('resize', {
|
165
|
+
bubbles: false,
|
166
|
+
cancelable: false,
|
167
|
+
composed: false,
|
168
|
+
});
|
169
|
+
this.dimensions = dimensions;
|
170
|
+
}
|
171
|
+
}
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
export * from './
|
2
|
-
export * from './
|
3
|
-
export *
|
4
|
-
|
5
|
-
|
1
|
+
export * from './core/shared';
|
2
|
+
export * from './core/applet';
|
3
|
+
export * from './core/context';
|
4
|
+
import { load } from './core/applet';
|
5
|
+
import { getContext } from './core/context';
|
6
|
+
export declare const applets: {
|
7
|
+
load: typeof load;
|
8
|
+
getContext: typeof getContext;
|
9
|
+
};
|
package/dist/index.js
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
export * from './
|
2
|
-
export * from './
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
export
|
1
|
+
export * from './core/shared';
|
2
|
+
export * from './core/applet';
|
3
|
+
export * from './core/context';
|
4
|
+
import { load } from './core/applet';
|
5
|
+
import { getContext } from './core/context';
|
6
|
+
export const applets = {
|
7
|
+
load,
|
8
|
+
getContext,
|
9
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { AppletAction } from '../core/shared';
|
2
|
+
export declare function parseUrl(url: string, base?: string): string;
|
3
|
+
export declare function createOpenAISchemaForAction(action: AppletAction): {
|
4
|
+
strict: boolean;
|
5
|
+
name: string;
|
6
|
+
schema: {
|
7
|
+
type: string;
|
8
|
+
required: string[];
|
9
|
+
properties: {
|
10
|
+
id: {
|
11
|
+
type: string;
|
12
|
+
};
|
13
|
+
params: import("../core/shared").JSONSchemaProperties;
|
14
|
+
};
|
15
|
+
additionalProperties: boolean;
|
16
|
+
};
|
17
|
+
};
|
@@ -0,0 +1,37 @@
|
|
1
|
+
// Adds http/https to URLs, and prepends with window location if relative
|
2
|
+
export function parseUrl(url, base) {
|
3
|
+
if (['http', 'https'].includes(url.split('://')[0])) {
|
4
|
+
return url;
|
5
|
+
}
|
6
|
+
let path = trimSlashes(url);
|
7
|
+
url = `${base || window.location.origin}/${path}`;
|
8
|
+
return url;
|
9
|
+
}
|
10
|
+
function trimSlashes(str) {
|
11
|
+
return str.replace(/^\/+|\/+$/g, '');
|
12
|
+
}
|
13
|
+
export function createOpenAISchemaForAction(action) {
|
14
|
+
return {
|
15
|
+
strict: true,
|
16
|
+
name: 'action_schema',
|
17
|
+
schema: {
|
18
|
+
type: 'object',
|
19
|
+
required: Object.keys(action),
|
20
|
+
properties: {
|
21
|
+
id: { type: 'string' },
|
22
|
+
params: action.params,
|
23
|
+
},
|
24
|
+
additionalProperties: false,
|
25
|
+
},
|
26
|
+
};
|
27
|
+
}
|
28
|
+
// export async function loadAppletManifest(url: string): Promise<AppletManifest> {
|
29
|
+
// url = parseUrl(url);
|
30
|
+
// const request = await fetch(`${url}/manifest.json`);
|
31
|
+
// const appletManifest = await request.json();
|
32
|
+
// if (appletManifest.type !== 'applet') {
|
33
|
+
// throw new Error("URL doesn't point to a valid applet manifest.");
|
34
|
+
// }
|
35
|
+
// appletManifest.entrypoint = parseUrl(appletManifest.entrypoint, url);
|
36
|
+
// return appletManifest;
|
37
|
+
// }
|
package/dist/utils.d.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import { AppletAction
|
2
|
-
export declare function
|
3
|
-
export declare function loadAppletManifest(url: string): Promise<AppletManifest>;
|
1
|
+
import { AppletAction } from './core/shared';
|
2
|
+
export declare function parseUrl(url: string, base?: string): string;
|
4
3
|
export declare function createOpenAISchemaForAction(action: AppletAction): {
|
5
4
|
strict: boolean;
|
6
5
|
name: string;
|
@@ -11,7 +10,7 @@ export declare function createOpenAISchemaForAction(action: AppletAction): {
|
|
11
10
|
id: {
|
12
11
|
type: string;
|
13
12
|
};
|
14
|
-
params: import("./
|
13
|
+
params: import("./core/shared").JSONSchemaProperties;
|
15
14
|
};
|
16
15
|
additionalProperties: boolean;
|
17
16
|
};
|
package/dist/utils.js
CHANGED
@@ -1,36 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
if (path.startsWith('/'))
|
7
|
-
path = path.slice(1);
|
8
|
-
if (path.endsWith('/'))
|
9
|
-
path = path.slice(0, -1);
|
10
|
-
url = `${base || window.location.origin}/${path}`;
|
11
|
-
return url;
|
12
|
-
}
|
13
|
-
export async function getAppletsList(url) {
|
14
|
-
url = parseUrl(url);
|
15
|
-
try {
|
16
|
-
const request = await fetch(`${url}/manifest.json`);
|
17
|
-
const appManifest = await request.json();
|
18
|
-
return appManifest.applets ? appManifest.applets : [];
|
19
|
-
}
|
20
|
-
catch {
|
21
|
-
return [];
|
22
|
-
}
|
1
|
+
// Adds http/https to URLs, and prepends with window location if relative
|
2
|
+
export function parseUrl(url, base) {
|
3
|
+
if (url)
|
4
|
+
url = URL.parse(url, base ?? window.location.href).href;
|
5
|
+
return trimTrailingSlash(url);
|
23
6
|
}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
const appletManifest = await request.json();
|
28
|
-
if (appletManifest.type !== 'applet') {
|
29
|
-
throw new Error("URL doesn't point to a valid applet manifest.");
|
7
|
+
function trimTrailingSlash(url) {
|
8
|
+
if (url.endsWith('/')) {
|
9
|
+
return url.slice(0, -1);
|
30
10
|
}
|
31
|
-
|
32
|
-
return appletManifest;
|
11
|
+
return url;
|
33
12
|
}
|
13
|
+
// Creates an OpenAI-compatible schema declaration for an action
|
34
14
|
export function createOpenAISchemaForAction(action) {
|
35
15
|
return {
|
36
16
|
strict: true,
|
package/package.json
CHANGED
package/dist/client.d.ts
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
import { AppletAction, AppletHeader, AppletMessage, ActionParams, AppletManifest, AppletMessageType, AppletMessageCallback, AppletManifestDict } from './types';
|
2
|
-
export declare function list(url: string): Promise<AppletManifestDict>;
|
3
|
-
interface AppletOpts {
|
4
|
-
headless?: boolean;
|
5
|
-
unsafe?: boolean;
|
6
|
-
}
|
7
|
-
export declare function load(url: string, container?: HTMLIFrameElement, opts?: AppletOpts): Promise<Applet>;
|
8
|
-
export declare class Applet<T = unknown> extends EventTarget {
|
9
|
-
#private;
|
10
|
-
actions: AppletAction[];
|
11
|
-
manifest: AppletManifest;
|
12
|
-
container: HTMLIFrameElement;
|
13
|
-
constructor();
|
14
|
-
get state(): T;
|
15
|
-
set state(state: T);
|
16
|
-
toJson(): {
|
17
|
-
[k: string]: any;
|
18
|
-
};
|
19
|
-
resizeContainer(dimensions: {
|
20
|
-
height: number;
|
21
|
-
width: number;
|
22
|
-
}): void;
|
23
|
-
onstateupdated(event: CustomEvent): void;
|
24
|
-
disconnect(): void;
|
25
|
-
dispatchAction(actionId: string, params: ActionParams): Promise<AppletMessage<any>>;
|
26
|
-
send(message: AppletMessage): Promise<AppletMessage<any>>;
|
27
|
-
on(messageType: AppletMessageType, callback: AppletMessageCallback): Promise<void>;
|
28
|
-
}
|
29
|
-
export declare function loadManifest(url: string): Promise<AppletManifest>;
|
30
|
-
export declare function getHeaders(url: string): Promise<AppletHeader[]>;
|
31
|
-
export declare function getManifests(url: string): Promise<any[]>;
|
32
|
-
export {};
|