@web-applets/sdk 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +67 -61
- package/dist/core/context.d.ts +2 -2
- package/dist/core/shared.d.ts +9 -5
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +0 -16
- package/dist/utils.js +0 -16
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,29 +1,44 @@
|
|
1
1
|
# Web Applets
|
2
2
|
|
3
|
-
> An open spec & SDK for creating apps that agents can use.
|
3
|
+
> An open spec & SDK for creating web apps that agents can use.
|
4
4
|
|
5
|
-
💌 [Mailing List](https://groups.google.com/a/unternet.co/g/community)
|
5
|
+
👾 [Community Discord](https://discord.gg/2aUvMe8HrC) | 💌 [Mailing List](https://groups.google.com/a/unternet.co/g/community)
|
6
6
|
|
7
|
-
|
7
|
+
[![Mozilla builders logo](docs/assets/builders.png)](https://builders.mozilla.org/)
|
8
|
+
|
9
|
+
Web Applets is a [Mozilla Builders](https://builders.mozilla.org/) project.
|
8
10
|
|
9
|
-
|
11
|
+
## What is it?
|
10
12
|
|
11
|
-
|
13
|
+
**Web Applets is an open specification for building software that both humans and AI can understand and use together.** Instead of forcing AI to operate traditional point-and-click apps built for humans, Web Applets creates a new kind of web software designed for human-AI collaboration.
|
12
14
|
|
13
15
|
![Demo of a web applets chatbot](./docs/assets/applets-chat-demo.gif)
|
14
16
|
|
15
|
-
|
17
|
+
## Why?
|
18
|
+
|
19
|
+
[Unternet](https://unternet.co) is building a new, intelligent user agent that can do things for you across the web. As part of that effort, we needed a way to actuate an embedded web app. You can do this with a computer use model, but for many use cases it's not suitable to point and click around in a virtual browser. Why make a computer talk to another computer via a clumsy web interface when they can just talk directly?
|
20
|
+
|
21
|
+
Web Applets lets you define a simple, computer-readable API for a web app running in a browser, webview, or iframe. You can send it actions as JSON objects, which an LLM can easily create (see [OpenAI's structured JSON endpoint](https://openai.com/index/introducing-structured-outputs-in-the-api/)), and they can update their UI instantly in-place. Plus, you can expose the internal state of the applets to the model so you can do cool stuff like chat to a map.
|
22
|
+
|
23
|
+
We wanted anyone to be able to build these actions into their own third-party applets and distribute them. So, we extended the web & made it available to everyone!
|
24
|
+
|
25
|
+
## Getting started
|
26
|
+
|
27
|
+
Create a new web app using our CLI:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
npx @web-applets/create
|
31
|
+
```
|
32
|
+
|
33
|
+
Inside the generated folder, you'll find a basic web app setup:
|
16
34
|
|
17
|
-
-
|
18
|
-
-
|
19
|
-
-
|
20
|
-
- **Local-First:** Runs in your environment, keeping your data under your control
|
21
|
-
- **Composable:** Applets can work together, sharing context and state
|
22
|
-
- **Open Standard:** Designed for interoperability across clients, not platform lock-in
|
35
|
+
- `public/manifest.json`: A web app manifest, where you can define initial actions, add icons, etc.
|
36
|
+
- `index.html`: Much like a website, this holds the main page for your applet
|
37
|
+
- `src/main.ts`: Declares functions that respond to each action, and a render function that updates the view based on state
|
23
38
|
|
24
|
-
|
39
|
+
> Want to use React? Svelte? Vue? – No problem, just install the dependencies and create an app the way you normally would in a website. So long as you're receiving the action events, it will all just work.
|
25
40
|
|
26
|
-
|
41
|
+
Now let's build out a basic web applet that will say hello when we send it an action:
|
27
42
|
|
28
43
|
`index.html`:
|
29
44
|
|
@@ -37,7 +52,29 @@ Let's say we have a simple website that says hello. It might look something like
|
|
37
52
|
</html>
|
38
53
|
```
|
39
54
|
|
40
|
-
Let's add some Web Applets functionality, so this can respond to a `set_name`
|
55
|
+
Let's add some Web Applets functionality, so this can respond to a `set_name` action:
|
56
|
+
|
57
|
+
`public/manifest.json`:
|
58
|
+
|
59
|
+
```json
|
60
|
+
{
|
61
|
+
// ...
|
62
|
+
"actions": [
|
63
|
+
{
|
64
|
+
"id": "set_name",
|
65
|
+
"description": "Sets the name of the user.",
|
66
|
+
"parameters": {
|
67
|
+
"type": "object",
|
68
|
+
"properties": {
|
69
|
+
"name": {
|
70
|
+
"type": "string"
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
]
|
76
|
+
}
|
77
|
+
```
|
41
78
|
|
42
79
|
`main.js`:
|
43
80
|
|
@@ -47,60 +84,30 @@ import { applets } from '@web-applets/sdk';
|
|
47
84
|
const context = applets.getContext();
|
48
85
|
|
49
86
|
// Define a 'set_name' action, and make it update the shared data object with the new name
|
50
|
-
context.
|
51
|
-
|
52
|
-
name: {
|
53
|
-
type: string,
|
54
|
-
description: 'The name of the person to be greeted.',
|
55
|
-
},
|
56
|
-
},
|
57
|
-
handler: ({ name }) => applet.data = { name };
|
87
|
+
context.addActionHandler('set_name', ({ name }) => {
|
88
|
+
context.data = { name };
|
58
89
|
});
|
59
90
|
|
60
91
|
// Whenever the data is updated, update the view
|
61
92
|
context.ondata = () => {
|
62
|
-
document.getElementById('name')
|
93
|
+
const nameElement = document.getElementById('name');
|
94
|
+
if (nameElement) {
|
95
|
+
nameElement.innerText = context.data.name;
|
96
|
+
}
|
63
97
|
};
|
64
98
|
```
|
65
99
|
|
66
|
-
|
67
|
-
|
68
|
-
To use this applet, we need to load it in our host web app using the SDK. Assuming the applet lives in our public directory, here's what that might look like:
|
69
|
-
|
70
|
-
```js
|
71
|
-
const applet = await applets.load('/helloworld.applet');
|
72
|
-
applet.onstateupdated = (state) => console.log(state);
|
73
|
-
applet.dispatchAction('set_name', { name: 'Web Applets' });
|
74
|
-
// { name: 'Web Applets' }
|
75
|
-
```
|
76
|
-
|
77
|
-
For a live example you can download and play with now, check out the [applets chat demo](https://github.com/unternet-co/applets-chat).
|
78
|
-
|
79
|
-
## Getting started
|
80
|
-
|
81
|
-
Create a new web app with the applets SDK installed. You can do this quickly using our CLI:
|
82
|
-
|
83
|
-
```bash
|
84
|
-
npx @web-applets/create
|
85
|
-
```
|
86
|
-
|
87
|
-
Inside the generated folder, you'll find a basic web app setup:
|
88
|
-
|
89
|
-
- `public/manifest.json`: A web app manifest, useful when publishing your applet, adding icons, etc.
|
90
|
-
- `index.html`: Much like a website, this holds the main page for your applet
|
91
|
-
- `src/main.ts`: Declares functions that respond to each action, and a render function that updates the view based on state
|
92
|
-
|
93
|
-
> Want to use React? Svelte? Vue? – No problem, just install the dependencies and create an app the way you normally would in a website. So long as you're receiving the action events, it will all just work.
|
94
|
-
|
95
|
-
Now if you run `npx @web-applets/inspector`, you should be able to test out your new applet directly. This applet will now work in any environment where the SDK is installed.
|
100
|
+
To test out this applet, first start the dev server with `npm run dev`, and take note of the dev server URL. Then, fire up the Web Applets inspector by running `npx @web-applets/inspector`, and enter the dev URL into the URL bar up the top.
|
96
101
|
|
97
102
|
![A screenshot showing the 'playground' editing UI, with a web applets showing 'Hello, Web Applets'](docs/assets/web-applets-inspector.png)
|
98
103
|
|
104
|
+
You can build this applet, by running `npm run build`, and host it on any static site host. This applet will now work in any environment where the SDK is installed.
|
105
|
+
|
99
106
|
## Integrating Web Applets into your client
|
100
107
|
|
101
|
-
|
108
|
+
In order to run, web applets need to be embedded in an environment that supports the Web Applets protocol. This might look like a browser (email me if you're interested!), or an electron app with `<webview>` tags, or sometthing as simple as a web-based AI chat client using iframes.
|
102
109
|
|
103
|
-
|
110
|
+
First, install & import the applets SDK in your client app:
|
104
111
|
|
105
112
|
```bash
|
106
113
|
npm install @web-applets/sdk
|
@@ -110,12 +117,12 @@ npm install @web-applets/sdk
|
|
110
117
|
import { applets } from '@web-applets/sdk';
|
111
118
|
```
|
112
119
|
|
113
|
-
Now you can import your applets from wherever they're being served from (note – you can also host them anywhere on the web):
|
120
|
+
Now you can import your applets from wherever they're being served from (note – you can also host them locally, or anywhere on the web):
|
114
121
|
|
115
122
|
```js
|
116
|
-
const applet = await applets.load('/
|
123
|
+
const applet = await applets.load('https://applets.unternet.co/maps');
|
117
124
|
applet.ondata = (e) => console.log(e.data);
|
118
|
-
applet.dispatchAction('set_name', { name: 'Web Applets' });
|
125
|
+
applet.dispatchAction('set_name', { name: 'Web Applets' }); // console.log: { name: "Ada Lovelace" }
|
119
126
|
```
|
120
127
|
|
121
128
|
The above applet is actually running headless, but we can get it to display by attaching it to a container. For the loading step, instead run:
|
@@ -129,8 +136,7 @@ const applet = await applets.load(`/helloworld.applet`, container);
|
|
129
136
|
To load pre-existing saved data into an applet, simply set the data property:
|
130
137
|
|
131
138
|
```js
|
132
|
-
applet.data = { name: 'Ada Lovelace' };
|
133
|
-
// console.log: { name: "Ada Lovelace" }
|
139
|
+
applet.data = { name: 'Ada Lovelace' }; // console.log: { name: "Ada Lovelace" }
|
134
140
|
```
|
135
141
|
|
136
142
|
## Feedback & Community
|
package/dist/core/context.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ActionParams, AppletDataEvent, AppletLoadEvent, AppletReadyEvent,
|
1
|
+
import { ActionParams, AppletDataEvent, AppletLoadEvent, AppletReadyEvent, JSONSchema, AppletManifest, AppletAction, AppletMessageRelay } from './shared';
|
2
2
|
export type ActionHandler<T extends ActionParams> = (params: T) => void | Promise<void>;
|
3
3
|
export type ActionHandlerDict = {
|
4
4
|
[key: string]: ActionHandler<any>;
|
@@ -25,7 +25,7 @@ export declare class AppletContext extends EventTarget {
|
|
25
25
|
ondata(event: AppletDataEvent): void;
|
26
26
|
}
|
27
27
|
interface ActionDefinition<T> extends Omit<AppletAction, 'id'> {
|
28
|
-
|
28
|
+
parameters?: JSONSchema;
|
29
29
|
handler?: ActionHandler<T>;
|
30
30
|
}
|
31
31
|
export declare function getContext(): AppletContext;
|
package/dist/core/shared.d.ts
CHANGED
@@ -18,13 +18,17 @@ export interface AppletAction {
|
|
18
18
|
id: string;
|
19
19
|
name?: string;
|
20
20
|
description?: string;
|
21
|
-
|
21
|
+
parameters?: JSONSchema;
|
22
22
|
}
|
23
|
-
export
|
24
|
-
type: string;
|
23
|
+
export interface JSONSchema {
|
24
|
+
type: 'object' | 'string' | 'number' | 'integer' | 'array' | 'boolean' | 'null';
|
25
25
|
description?: string;
|
26
|
-
properties?:
|
27
|
-
|
26
|
+
properties?: {
|
27
|
+
[key: string]: JSONSchema;
|
28
|
+
};
|
29
|
+
required?: string[];
|
30
|
+
additionalProperties?: boolean;
|
31
|
+
}
|
28
32
|
export type ActionParams = Record<string, any>;
|
29
33
|
export declare function loadManifest(pageUrl: string): Promise<AppletManifest>;
|
30
34
|
interface SendMessageOptions {
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
CHANGED
@@ -1,17 +1 @@
|
|
1
|
-
import { AppletAction } from './core/shared';
|
2
1
|
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
|
-
};
|
package/dist/utils.js
CHANGED
@@ -26,19 +26,3 @@ function trimTrailingSlash(url) {
|
|
26
26
|
}
|
27
27
|
return url;
|
28
28
|
}
|
29
|
-
// Creates an OpenAI-compatible schema declaration for an action
|
30
|
-
export function createOpenAISchemaForAction(action) {
|
31
|
-
return {
|
32
|
-
strict: true,
|
33
|
-
name: 'action_schema',
|
34
|
-
schema: {
|
35
|
-
type: 'object',
|
36
|
-
required: Object.keys(action),
|
37
|
-
properties: {
|
38
|
-
id: { type: 'string' },
|
39
|
-
params: action.params,
|
40
|
-
},
|
41
|
-
additionalProperties: false,
|
42
|
-
},
|
43
|
-
};
|
44
|
-
}
|