@skaile/skailify-core 0.0.0 → 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.
package/AGENTS.md ADDED
@@ -0,0 +1,164 @@
1
+ <!-- SPDX-License-Identifier: Apache-2.0 -->
2
+
3
+ # AGENTS.md: integrate `@skaile/skailify-core` into an app without a framework
4
+
5
+ You are a coding agent. Follow this file from top to bottom to make an existing app that uses no UI
6
+ framework (plain JavaScript, server-rendered HTML, scripts injected into a page) agent-controllable
7
+ inside a Skaile session. Do every step, run every check, and report what you could not verify.
8
+
9
+ For a React or Angular app, follow the `AGENTS.md` of `@skaile/skailify-react` or
10
+ `@skaile/skailify-angular` instead: they wrap this client.
11
+
12
+ ## What it does
13
+
14
+ The client connects the app to Skaile over the app channel. The app then tells the session agent
15
+ which view is on screen, what state it may read, and which actions it may call; the agent calls them
16
+ as tools, and the platform asks the user first for every action that changes data.
17
+
18
+ The client registers these capabilities itself (declared as `app.<name>`; the agent sees each one as
19
+ `app.<appId>.<name>`):
20
+
21
+ | Name | Kind | Approval | What it does |
22
+ |---|---|---|---|
23
+ | `app.get_view_state` | query | none | The view on screen, the registered state, the actions available now |
24
+ | `app.list_views` | query | none | The app's views (only with the `navigation` option) |
25
+ | `app.navigate` | effect | none | Shows a view and returns its state (only with the `navigation` option) |
26
+
27
+ Everything else is an action the app registers with `registerAction`, with a relative name
28
+ `<sector>.<verb>` (for example `slides.go_to`). Do not write the `app.` prefix yourself: a name that
29
+ starts with it throws.
30
+
31
+ ## What works today, and what does not
32
+
33
+ Read "What works today" in [Getting started](../../docs/getting-started.md) first. For this client:
34
+
35
+ - The app must run embedded in a Skaile preview, declared as an app with `"protocol": true` in the
36
+ workspace's `skaile.preview.json`. Opened on its own, it does not connect.
37
+ - **Writes and consent.** Every action that changes data must be declared with
38
+ `requiresApproval: true`. Queries and harmless view changes need none.
39
+ - Actions exist only while the page is open in the session.
40
+
41
+ ## Prerequisites
42
+
43
+ - The app's id: the `id` of its entry in the workspace's `skaile.preview.json`.
44
+ - With a bundler: `@skaile/workspaces` `^10.14.0` and `zod` `^4.4.0` as well.
45
+ - Without one: nothing else; the bundle carries both.
46
+
47
+ ## Step 1: load the client
48
+
49
+ With a bundler:
50
+
51
+ ```sh
52
+ npm install @skaile/skailify-core @skaile/workspaces zod
53
+ ```
54
+
55
+ ```js
56
+ import { createSkailify, z } from "@skaile/skailify-core";
57
+ ```
58
+
59
+ Without one, add one script tag before the app's own script, with a pinned version, and take the
60
+ exports from the global `Skailify`:
61
+
62
+ ```html
63
+ <script src="https://cdn.jsdelivr.net/npm/@skaile/skailify-core@0.1/dist/skailify.iife.js"></script>
64
+ <script>
65
+ const { createSkailify, z } = Skailify;
66
+ </script>
67
+ ```
68
+
69
+ If the app must not load scripts from another origin, copy `dist/skailify.iife.js` from the npm
70
+ package into the app's static files and load it from there.
71
+
72
+ ## Step 2: connect
73
+
74
+ Create one client per page and start it once:
75
+
76
+ ```js
77
+ const skailify = createSkailify({ appId: "<id in skaile.preview.json>" });
78
+ // … register views, state and actions (steps 3 and 4) …
79
+ void skailify.start();
80
+ ```
81
+
82
+ - `appId` has no safe default: it must equal the app's `id` in `skaile.preview.json`.
83
+ - Registrations made before or after `start()` both work.
84
+ - Optional: `navigation` (step 3), `settleTimeoutMs` (default 3000), `logger`, and `assistant`
85
+ (where Skaile puts its agent button over the app, or `{ kind: "none" }` to leave it out).
86
+
87
+ ## Step 3: expose what the app shows
88
+
89
+ Register the view on screen and keep it current:
90
+
91
+ ```js
92
+ const view = skailify.registerView({ id: "orders", title: "Orders", state: { filter } });
93
+ // when the screen changes:
94
+ view.update({ id: "order/42", title: "Order 42", loading: true });
95
+ // once its data has arrived:
96
+ view.update({ id: "order/42", title: "Order 42" });
97
+ ```
98
+
99
+ - `id` is stable: a route or screen name, with the record key for a detail.
100
+ - `state` is a small JSON-serialisable summary. State that cannot be serialised reaches the agent as
101
+ `{ error: "state is not JSON-serialisable" }` and breaks nothing else.
102
+ - Say `loading: true` while the view fetches what it shows: the agent waits until it is false.
103
+
104
+ Expose the data the agent needs to choose its next call, as ids and labels; `read` is called on every
105
+ read:
106
+
107
+ ```js
108
+ skailify.registerState("orders", () => rows.map((o) => ({ id: o.id, label: o.number, status: o.status })));
109
+ ```
110
+
111
+ If the app has several pages or screens, hand the client its views and how to show one:
112
+
113
+ ```js
114
+ createSkailify({
115
+ appId: "<id>",
116
+ navigation: {
117
+ views: () => [{ id: "orders", title: "Orders" }, { id: "order", menu: false, params: { orderId: "the order" } }],
118
+ navigate: (view, params) => showScreen(view, params),
119
+ current: () => currentScreenId(),
120
+ },
121
+ });
122
+ ```
123
+
124
+ ## Step 4: actions that change data
125
+
126
+ ```js
127
+ skailify.registerAction({
128
+ name: "orders.approve",
129
+ description: "Approve an order by id.",
130
+ input: z.object({ orderId: z.string().describe("Id from get_view_state") }),
131
+ kind: "effect",
132
+ requiresApproval: true,
133
+ handler: async ({ orderId }, ctx) => {
134
+ await approve(orderId); // the same function the app's own button calls
135
+ return { approved: orderId };
136
+ },
137
+ });
138
+ ```
139
+
140
+ - Call the same functions the app's own controls call. Throw an `Error` with a message the agent can
141
+ act on; it reaches the agent as `{ error }`.
142
+ - `ctx.principal` says whose agent turn made the call. It is not for authorisation: the app's own
143
+ login still decides.
144
+ - After an effect the client waits for the next frame and for every view to stop loading, then sends
145
+ the view report and `settled` itself.
146
+
147
+ ## Step 5: verify
148
+
149
+ Run each of these and report exactly which you ran and what happened.
150
+
151
+ 1. This package's tests, in this repository: `bun install`, then in `frontend/core` `bun run test`
152
+ and `bun run type-check`: no test fails.
153
+ 2. The app SDK's conformance checks: `bun run conformance` in `frontend/core`.
154
+ 3. The app works as before when it is opened on its own; `skailify.getSnapshot().status.state` is
155
+ `standalone`.
156
+ 4. In a Skaile session with the app declared `"protocol": true`: ask the agent what the app shows. It
157
+ should call `get_view_state` and describe the view. Ask it to do something behind an action with
158
+ `requiresApproval: true`: an approval card appears, and the app changes only after you approve.
159
+
160
+ ## Known limits
161
+
162
+ - Actions come from the browser: they exist only while the page is open in the session.
163
+ - Settling waits for the next rendered frame and for every registered view to stop loading. Data the
164
+ app fetches without saying `loading: true` is not waited for.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,128 @@
1
+ <!-- SPDX-License-Identifier: Apache-2.0 -->
2
+
1
3
  # @skaile/skailify-core
2
4
 
3
- Placeholder. Install the latest version: `npm install @skaile/skailify-core@latest`.
5
+ The framework-free Skailify client. It makes an app agent-controllable inside a Skaile session, over
6
+ the Skailify app channel (`@skaile/workspaces/app-sdk`), without a UI framework: the app creates one
7
+ client with its app id and tells it, in plain function calls, which view is on screen, what state the
8
+ agent may read and which actions it may call.
9
+
10
+ The React and Angular adapters ([`@skaile/skailify-react`](../react/),
11
+ [`@skaile/skailify-angular`](../angular/)) wrap this client; an app that uses one of them does not
12
+ install this package itself. Use it directly in an app without a framework: plain JavaScript, a page
13
+ rendered on the server, a script injected into HTML.
14
+
15
+ It connects only when the app is embedded in Skaile as a preview app with the app channel switched
16
+ on (`"protocol": true`): an iframe whose URL carries `skaileParentOrigin`. Opened on its own, the app
17
+ does not connect, and the registrations cost next to nothing.
18
+
19
+ ## Two ways to load it
20
+
21
+ With a bundler:
22
+
23
+ ```sh
24
+ npm install @skaile/skailify-core @skaile/workspaces zod
25
+ ```
26
+
27
+ ```ts
28
+ import { createSkailify, z } from "@skaile/skailify-core";
29
+ ```
30
+
31
+ Without one, from a single `<script>` tag: `dist/skailify.iife.js` has zod and the app SDK bundled
32
+ in and puts every export of the package on the global `Skailify`. It ships in the npm package, so a
33
+ CDN that serves npm packages serves it too:
34
+
35
+ ```html
36
+ <script src="https://cdn.jsdelivr.net/npm/@skaile/skailify-core@0.1/dist/skailify.iife.js"></script>
37
+ <script>
38
+ const { createSkailify, z } = Skailify;
39
+ </script>
40
+ ```
41
+
42
+ The bundle is about 410 kB minified (about 98 kB gzipped), most of it zod. Pin the version you
43
+ tested; serve a copy from the app's own origin when the app must not load scripts from elsewhere.
44
+
45
+ ## Wiring
46
+
47
+ ```js
48
+ const skailify = createSkailify({ appId: "deck" }); // the id in skaile.preview.json
49
+
50
+ const view = skailify.registerView({ id: "slide-1", title: "Welcome" });
51
+ skailify.registerState("deck", () => ({ slide: current, slides: count }));
52
+ skailify.registerAction({
53
+ name: "slides.go_to", // relative: the agent sees app.<appId>.slides.go_to
54
+ description: "Show a slide of the deck by its number, starting at 1.",
55
+ input: z.object({ slide: z.number().int().min(1) }),
56
+ kind: "effect",
57
+ handler: async ({ slide }) => {
58
+ show(slide); // the same function the app's own buttons call
59
+ return { slide };
60
+ },
61
+ });
62
+
63
+ void skailify.start();
64
+ ```
65
+
66
+ - `registerView` returns `{ update, remove }`: call `update` when the view on screen changes, with
67
+ `loading: true` while it fetches what it shows. Of several registered views the last one added is on
68
+ screen.
69
+ - `registerState` and `registerAction` return the function that removes the registration.
70
+ - `subscribe(listener)` and `getSnapshot()` give the connection status, the capabilities and the
71
+ activity log (the agent's latest calls, newest first) for a panel in the app's own UI.
72
+ - `navigation` (option) hands the client the app's views and how to show one; the agent then gets
73
+ `list_views` and `navigate`. `assistant` (option) places Skaile's agent button over the app, or
74
+ leaves it out with `{ kind: "none" }`; `setAssistant` changes it later.
75
+
76
+ ## Capabilities
77
+
78
+ The agent sees each one as `app.<appId>.<name>`.
79
+
80
+ | Name | Kind | When | What it does |
81
+ |---|---|---|---|
82
+ | `get_view_state` | query | always | The view on screen, the registered state, and the actions available now. Waits until no view is loading |
83
+ | `list_views` | query | with `navigation` | The app's views: id, title, whether they are in the menu, and the parameters they take |
84
+ | `navigate` | effect | with `navigation` | Shows a view with its parameters, waits until it has loaded, and returns the view state |
85
+ | your actions | query / effect | while registered | Whatever `registerAction` declares |
86
+
87
+ Actions that change data are declared with `requiresApproval: true`, and optionally an `effectClass`;
88
+ the platform then asks the user before it forwards the call. After every effect the client waits for
89
+ the next rendered frame and for every view to stop loading, then sends the view report and `settled`.
90
+ A call waits at most `settleTimeoutMs` (default 3 s, at most 10 s).
91
+
92
+ ## For framework adapters
93
+
94
+ Two options exist for an adapter that wraps the client: `rendered`, a promise for "the framework has
95
+ rendered the latest change" that settling waits for before the next frame, and `zone`, which runs the
96
+ connection outside and the handlers inside a framework's async tracking (Angular's zone.js). An app
97
+ without a framework leaves both unset.
98
+
99
+ ## Example
100
+
101
+ [`../_example/plain-html/`](../_example/plain-html/) is a three-slide deck in one HTML file that loads
102
+ the bundle with a `<script>` tag. The tests load the bundle as a page would and drive it through the
103
+ app SDK's fake gateway.
104
+
105
+ ## SDK version
106
+
107
+ Peer `@skaile/workspaces` ^10.14.0 and `zod` ^4.4.0. CI runs the tests and the conformance checks
108
+ against the lowest and the latest `@skaile/workspaces` in that range. The bundle carries the versions
109
+ it was built with.
110
+
111
+ ## Development
112
+
113
+ ```sh
114
+ bun install # at the repository root
115
+ cd frontend/core
116
+ bun run test # builds dist/, then the unit tests and the bundle smoke test
117
+ bun run conformance # the app SDK's conformance checks
118
+ bun run type-check
119
+ ```
120
+
121
+ The React, Angular and vue-tsm adapters resolve `@skaile/skailify-core` to `src/` through their
122
+ `tsconfig.json` `paths`, so their tests and type-checks need no build of this package. Their published
123
+ builds import it (React, Angular) or bundle it in (vue-tsm, which an app serves through its import
124
+ map).
125
+
126
+ ## License
127
+
128
+ Apache-2.0, see [LICENSE](../../LICENSE).