@ashley-shrok/viewmodel-shell 1.5.0 → 1.6.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/agent-skill.md ADDED
@@ -0,0 +1,121 @@
1
+ # ViewModel Shell — agent operating manual
2
+
3
+ This is the protocol manual for an agent driving a ViewModel Shell (VMS) app over its JSON wire. It is operational, not historical. Follow each section as a rule.
4
+
5
+ ## What this is
6
+
7
+ VMS is a server-driven UI framework. The server is a pure function `(state, action) → (newState, view)`. Every response carries the entire UI state as an opaque blob and a fresh `vm` view tree. You do not have to render the tree to drive the API — you can read its node types and dispatch actions directly.
8
+
9
+ ## Endpoints
10
+
11
+ Read the page's `<meta name="viewmodel-shell">` tag for the endpoint pair:
12
+
13
+ ```json
14
+ { "protocol": "viewmodel-shell/1.0", "endpoint": "/api/x", "actionEndpoint": "/api/x/action", "skill": "/.well-known/vms-skill.md" }
15
+ ```
16
+
17
+ - `GET <endpoint>` → returns the initial `{ "ok": true, "vm": <ViewNode tree>, "state": <opaque state blob> }`.
18
+ - `POST <actionEndpoint>` → dispatches an action. See the next section for the body shape.
19
+
20
+ The `skill` field is optional and points at this manual (or an app-specific preamble + this manual).
21
+
22
+ ## Action dispatch shape
23
+
24
+ Two body forms are accepted. Use JSON when you can; multipart only when uploading files.
25
+
26
+ **JSON (recommended for agents).** Set `Content-Type: application/json` and POST a flat envelope:
27
+
28
+ ```json
29
+ { "name": "save-ticket-42", "state": { "...": "round-tripped state blob from the last response" } }
30
+ ```
31
+
32
+ There is no `context` field — per-row / per-tab identity is encoded in the action name itself (e.g. `delete-row-42`, `filter-active`). File uploads are NOT supported in this form.
33
+
34
+ **Multipart (browser-style and file-bearing).** Set `Content-Type: multipart/form-data` and post three kinds of entries:
35
+
36
+ | Field | Value |
37
+ |---|---|
38
+ | `_action` | JSON: `{"name": "<action-name>"}` |
39
+ | `_state` | JSON: the current state blob |
40
+ | any file-input `name` | the binary file content (one entry per file input) |
41
+
42
+ ## The round-trip rule
43
+
44
+ The `state` blob from the last response goes back unchanged on the next dispatch, EXCEPT for the fields the user changed. Input nodes carry a `bind` property whose value is a dotted path inside `state` where the input's value lives (e.g. `"bind": "form.title"`). Before dispatching an action that depends on user input, write your value at the bound path inside the state blob; leave every other field as you received it. The server is authoritative for everything else and may rewrite any field in the response.
45
+
46
+ ## Response envelope
47
+
48
+ Every response carries `ok: boolean`.
49
+
50
+ **Success:**
51
+
52
+ ```json
53
+ { "ok": true, "vm": { /* ViewNode tree */ }, "state": { /* opaque blob */ } }
54
+ ```
55
+
56
+ Optional success-path fields, which may appear alone or alongside `vm`/`state`:
57
+
58
+ | Field | Meaning |
59
+ |---|---|
60
+ | `redirect` | A URL string. Navigate to it (or hand control off). When `redirect` is present, `vm` and `state` may be omitted. |
61
+ | `sideEffects` | An array of side-effect verbs (see next section). Applied in order before any redirect or re-render. |
62
+ | `nextPollIn` | Milliseconds. Schedule a `{"name": "poll"}` dispatch after this delay. |
63
+ | `busy` | Boolean. While `true`, drop user-initiated dispatches. Polls bypass. The next response that omits or sets `false` clears the lock. |
64
+ | `preventUnload` | Boolean. While `true`, treat the page as having unsaved work — warn before navigating away. |
65
+
66
+ **Failure:**
67
+
68
+ ```json
69
+ { "ok": false, "errors": [ { "message": "...", "code": "parse_error", "path": "form.title" } ] }
70
+ ```
71
+
72
+ `errors[].path` and `errors[].code` are optional. HTTP status is 4xx or 5xx on failure. Check `ok` once at the response edge; do not branch on HTTP status.
73
+
74
+ ## Side-effect verbs
75
+
76
+ `sideEffects[]` entries each carry a `type` discriminator. Built-in verbs as of `viewmodel-shell/1.0`:
77
+
78
+ ```json
79
+ { "sideEffects": [
80
+ { "type": "set-local-storage", "key": "auth_jwt", "value": "eyJ..." },
81
+ { "type": "set-session-storage", "key": "draft_id", "value": "42" },
82
+ { "type": "download", "url": "/api/invoices/42/pdf", "filename": "invoice-42.pdf" }
83
+ ] }
84
+ ```
85
+
86
+ | Verb | Effect |
87
+ |---|---|
88
+ | `set-local-storage` | Write `key`/`value` to platform localStorage (or your agent's equivalent persistent store). |
89
+ | `set-session-storage` | Same as above for session-scoped storage. |
90
+ | `download` | Fetch `url` (re-presenting your auth headers — see *Auth*) and save the bytes. Filename precedence: `Content-Disposition` > side-effect `filename` > URL basename > `"download"`. |
91
+
92
+ **Forward-compat rule — silently ignore unknown verbs.** A future minor release may add new verbs. If you see a `type` you do not recognize, skip it; do not error. Honor or ignore per your policy.
93
+
94
+ ## Errors
95
+
96
+ `ok: false` responses always carry `errors[]`. The framework uses a stable code vocabulary at the protocol edge:
97
+
98
+ | Code | Meaning |
99
+ |---|---|
100
+ | `parse_error` | The request body could not be parsed (malformed JSON, missing required field). |
101
+ | `unknown_action` | The `name` in your action envelope does not match any handler in the current tree. |
102
+ | `invalid_tree` | The server built a tree that violates a wire invariant (this is a server bug, not yours). |
103
+ | `uncaught_exception` | The action handler threw. Treat as a 500-class failure. |
104
+
105
+ Stop on `ok: false`. Surface the message to the user. Do not retry blindly — most of these are deterministic.
106
+
107
+ ## Auth
108
+
109
+ The wire does not mandate an auth shape. If the app needs credentials, the app preamble above (or its own README) names them. Common patterns: a `Bearer` token in `Authorization`, a CSRF/anti-forgery token in a custom header, a session cookie. Send the same headers on every request, including polls and downloads. The `download` side-effect re-presents your auth headers when the agent fetches the file.
110
+
111
+ ## Polling
112
+
113
+ If a response carries `nextPollIn: N`, schedule a POST `{ "name": "poll", "state": <last state> }` against the same `actionEndpoint` after `N` milliseconds. The server may continue returning `nextPollIn` until the workflow reaches a terminal state, at which point the field will be absent. Polls run silently — they are not user-initiated.
114
+
115
+ ## Files
116
+
117
+ File uploads use the multipart form above. One form entry per file input, keyed by the input's `name` attribute (from the corresponding node's `name` field in the tree). The file's binary content is the entry's value. JSON-body dispatch cannot carry files; use multipart.
118
+
119
+ ## Versioning
120
+
121
+ This manual applies to protocol token `viewmodel-shell/1.0` — the value of the `protocol` field on the discoverability meta tag. The protocol token tracks the wire shape, NOT the package version: a 1.5.x or 1.6.x package release may still carry protocol `viewmodel-shell/1.0` because the wire has not undergone a breaking change. A future major-version bump (`viewmodel-shell/2.0`) signals a breaking change and invalidates this manual; expect a new skill at the same `/.well-known/vms-skill.md` URL.
package/dist/server.d.ts CHANGED
@@ -68,6 +68,63 @@ export declare const shellSideEffect: {
68
68
  * JSON wire, matching the .NET WhenWritingNull null-omission contract. */
69
69
  download: (url: string, filename?: string) => ShellSideEffect;
70
70
  };
71
+ /**
72
+ * Mount the canonical VMS agent skill markdown as an HTTP handler.
73
+ *
74
+ * The skill is a self-contained operating manual for the VMS wire protocol
75
+ * (action dispatch shape, state round-trip rules, response envelope vocabulary,
76
+ * side-effect verbs, polling, errors, file uploads). Advertise it to agents
77
+ * driving your app via the `skill` field on the
78
+ * `<meta name="viewmodel-shell">` discoverability tag, pointing at whatever URL
79
+ * you mount this handler at (recommended: `/.well-known/vms-skill.md`).
80
+ *
81
+ * **Canonical source:** `viewmodel-shell/agent-skill.md` (shipped in the npm
82
+ * package's `files` array; the .NET package embeds a byte-identical copy at
83
+ * `viewmodel-shell-dotnet/AgentSkill.md` — see `parity/check-skill.ts`).
84
+ *
85
+ * **Preamble shape.** When `appPreamble` is supplied (non-empty after trim),
86
+ * the served body is:
87
+ *
88
+ * ```
89
+ * ## App-specific notes
90
+ *
91
+ * <preamble verbatim>
92
+ *
93
+ * ---
94
+ *
95
+ * <canonical-skill-body verbatim>
96
+ * ```
97
+ *
98
+ * When omitted (or whitespace-only), the served body is the canonical skill
99
+ * verbatim. The body is computed ONCE at handler-creation time; per-request
100
+ * cost is a single `new Response(body)`. Multiple handlers with different
101
+ * preambles are cheap and fully independent.
102
+ *
103
+ * **Cross-runtime.** Pure Web Fetch API — works in Bun, Deno, Hono, Cloudflare
104
+ * Workers, and Node 18+. The application's router owns method/path routing;
105
+ * the handler accepts any request and unconditionally serves the markdown body
106
+ * with `Content-Type: text/markdown; charset=utf-8`.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * import { createAgentSkillHandler } from "@ashley-shrok/viewmodel-shell/server";
111
+ * const skillHandler = createAgentSkillHandler({
112
+ * appPreamble: "This is the foo app. Auth: Bearer JWT in Authorization.",
113
+ * });
114
+ * Bun.serve({
115
+ * async fetch(req) {
116
+ * const url = new URL(req.url);
117
+ * if (url.pathname === "/.well-known/vms-skill.md" && req.method === "GET") {
118
+ * return skillHandler(req);
119
+ * }
120
+ * // ... your other routes
121
+ * },
122
+ * });
123
+ * ```
124
+ */
125
+ export declare function createAgentSkillHandler(opts?: {
126
+ appPreamble?: string;
127
+ }): (req: Request) => Response;
71
128
  /**
72
129
  * Thrown by an action handler to signal a malformed/invalid request. The
73
130
  * createAction wrapper catches this and returns a 400 with the error
package/dist/server.js CHANGED
@@ -8,6 +8,21 @@
8
8
  // Re-export the ViewNode hierarchy and wire types so a backend can import
9
9
  // everything it needs from one place.
10
10
  export * from "./index.js";
11
+ // ─── Canonical agent skill load (1.6.0 / 1.5.0) ──────────────────────────────
12
+ // Loaded once at module init so createAgentSkillHandler can bake the body at
13
+ // handler-creation time (no per-request fs work). The path resolution works for
14
+ // BOTH layouts:
15
+ // - Source/dev (vitest): src/server.ts → ../agent-skill.md
16
+ // - Published tarball: dist/server.js → ../agent-skill.md (file is in
17
+ // package root, listed in package.json `files`)
18
+ // If `agent-skill.md` is missing at module init the import throws — the
19
+ // fail-loud rule (see AGENTS.md capability-seam doc) applies: a silent 404
20
+ // from the skill endpoint would defeat the purpose.
21
+ import { readFileSync as __vmsReadFileSync } from "node:fs";
22
+ import { fileURLToPath as __vmsFileURLToPath } from "node:url";
23
+ import { dirname as __vmsDirname, join as __vmsJoin } from "node:path";
24
+ const __vmsAgentSkillDir = __vmsDirname(__vmsFileURLToPath(import.meta.url));
25
+ const AGENT_SKILL_MARKDOWN = __vmsReadFileSync(__vmsJoin(__vmsAgentSkillDir, "..", "agent-skill.md"), "utf8");
11
26
  /**
12
27
  * Walk a ViewNode tree and assert that every dispatch-bearing action name names
13
28
  * exactly one operation. Two occurrences are considered "the same operation"
@@ -376,6 +391,71 @@ export const shellSideEffect = {
376
391
  * JSON wire, matching the .NET WhenWritingNull null-omission contract. */
377
392
  download: (url, filename) => ({ type: "download", url, ...(filename != null ? { filename } : {}) }),
378
393
  };
394
+ // ─── Canonical agent skill mount helper (1.6.0 / 1.5.0) ──────────────────────
395
+ /**
396
+ * Mount the canonical VMS agent skill markdown as an HTTP handler.
397
+ *
398
+ * The skill is a self-contained operating manual for the VMS wire protocol
399
+ * (action dispatch shape, state round-trip rules, response envelope vocabulary,
400
+ * side-effect verbs, polling, errors, file uploads). Advertise it to agents
401
+ * driving your app via the `skill` field on the
402
+ * `<meta name="viewmodel-shell">` discoverability tag, pointing at whatever URL
403
+ * you mount this handler at (recommended: `/.well-known/vms-skill.md`).
404
+ *
405
+ * **Canonical source:** `viewmodel-shell/agent-skill.md` (shipped in the npm
406
+ * package's `files` array; the .NET package embeds a byte-identical copy at
407
+ * `viewmodel-shell-dotnet/AgentSkill.md` — see `parity/check-skill.ts`).
408
+ *
409
+ * **Preamble shape.** When `appPreamble` is supplied (non-empty after trim),
410
+ * the served body is:
411
+ *
412
+ * ```
413
+ * ## App-specific notes
414
+ *
415
+ * <preamble verbatim>
416
+ *
417
+ * ---
418
+ *
419
+ * <canonical-skill-body verbatim>
420
+ * ```
421
+ *
422
+ * When omitted (or whitespace-only), the served body is the canonical skill
423
+ * verbatim. The body is computed ONCE at handler-creation time; per-request
424
+ * cost is a single `new Response(body)`. Multiple handlers with different
425
+ * preambles are cheap and fully independent.
426
+ *
427
+ * **Cross-runtime.** Pure Web Fetch API — works in Bun, Deno, Hono, Cloudflare
428
+ * Workers, and Node 18+. The application's router owns method/path routing;
429
+ * the handler accepts any request and unconditionally serves the markdown body
430
+ * with `Content-Type: text/markdown; charset=utf-8`.
431
+ *
432
+ * @example
433
+ * ```ts
434
+ * import { createAgentSkillHandler } from "@ashley-shrok/viewmodel-shell/server";
435
+ * const skillHandler = createAgentSkillHandler({
436
+ * appPreamble: "This is the foo app. Auth: Bearer JWT in Authorization.",
437
+ * });
438
+ * Bun.serve({
439
+ * async fetch(req) {
440
+ * const url = new URL(req.url);
441
+ * if (url.pathname === "/.well-known/vms-skill.md" && req.method === "GET") {
442
+ * return skillHandler(req);
443
+ * }
444
+ * // ... your other routes
445
+ * },
446
+ * });
447
+ * ```
448
+ */
449
+ export function createAgentSkillHandler(opts = {}) {
450
+ const preamble = opts.appPreamble?.trim() ?? "";
451
+ const body = preamble.length === 0
452
+ ? AGENT_SKILL_MARKDOWN
453
+ : `## App-specific notes\n\n${preamble}\n\n---\n\n${AGENT_SKILL_MARKDOWN}`;
454
+ return (_req) => new Response(body, {
455
+ status: 200,
456
+ headers: { "Content-Type": "text/markdown; charset=utf-8" },
457
+ });
458
+ }
379
459
  // ─── Action handler factory ──────────────────────────────────────────────────
380
460
  /**
381
461
  * Thrown by an action handler to signal a malformed/invalid request. The
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ashley-shrok/viewmodel-shell",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "A server-driven UI framework where the wire format is structured enough that agents can build full-stack apps without ever opening a browser and all UI tests are pure unit tests with no browser runtime. Server returns a JSON tree of typed nodes; a thin TypeScript adapter renders it to DOM. Backend-agnostic — a .NET reference backend ships with the repo, but any language can produce the JSON contract.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,6 +21,7 @@
21
21
  "files": [
22
22
  "dist",
23
23
  "styles",
24
+ "agent-skill.md",
24
25
  "README.md",
25
26
  "LICENSE"
26
27
  ],