@happyvertical/smrt-web 0.42.6 → 0.43.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.
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Register every collection's generated tool descriptors with WebMCP.
3
+ *
4
+ * @returns a disposer that deregisters all tools this call registered. On a
5
+ * browser without WebMCP the call is a no-op and the disposer is inert.
6
+ */
7
+ export declare function registerWebMcpTools(definitions: SmrtWebCollectionDefinition[], options?: RegisterWebMcpToolsOptions): () => void;
8
+
9
+ export declare interface RegisterWebMcpToolsOptions {
10
+ /** REST base path for the fetchers (default `/api/v1`). */
11
+ basePath?: string;
12
+ /** Injectable fetch (tests / SSR-safe wrappers). */
13
+ fetchFn?: typeof fetch;
14
+ /** Shared smrt-web cache handle used by page collections. */
15
+ client?: SmrtWebClient;
16
+ /** Optional cache scope matching the page collection's scope. */
17
+ scope?: string;
18
+ /**
19
+ * Override how a definition's CRUD fetchers are built. Defaults to
20
+ * {@link createDefinitionFetchers}; the primary seam for testing `execute`
21
+ * without a live server.
22
+ */
23
+ resolveFetchers?: (definition: SmrtWebCollectionDefinition) => SmrtCrudFetchers;
24
+ /** Predicate to include/exclude individual tools (e.g. reads-only surfaces). */
25
+ filter?: (definition: SmrtWebCollectionDefinition, descriptor: NonNullable<SmrtWebCollectionDefinition['toolDescriptors']>[number]) => boolean;
26
+ }
27
+
28
+ /**
29
+ * The per-collection CRUD surface of the generated REST client
30
+ * (`createClient(basePath).<collection>` from `@happyvertical/smrt-virt-client`).
31
+ *
32
+ * Return types are `unknown` on purpose: generated fetchers resolve with
33
+ * whatever the server sent, so this package normalizes and validates payloads
34
+ * centrally — see {@link unwrapListResult} / {@link unwrapItemResult}.
35
+ */
36
+ declare interface SmrtCrudFetchers {
37
+ list(params?: Record<string, unknown>): Promise<unknown>;
38
+ get?(id: string): Promise<unknown>;
39
+ create(data: Record<string, unknown>): Promise<unknown>;
40
+ update?(id: string, data: Record<string, unknown>): Promise<unknown>;
41
+ delete?(id: string): Promise<unknown>;
42
+ /** Invoke a generated custom action route. */
43
+ custom?(action: string, args: Record<string, unknown>, route?: SmrtWebToolRouteDescriptor): Promise<unknown>;
44
+ }
45
+
46
+ /**
47
+ * Opaque handle to the shared client cache / request-dedup layer. Create one
48
+ * with {@link createSmrtWebClient} and pass the SAME instance to every
49
+ * collection that should share a cache and deduplicate in-flight requests.
50
+ *
51
+ * The engine (currently a TanStack Query client) is intentionally hidden behind
52
+ * this brand so it stays swappable — do not depend on its concrete shape.
53
+ */
54
+ declare interface SmrtWebClient {
55
+ /** Phantom brand — this handle wraps the hidden client-cache engine. */
56
+ readonly __smrtWebClient: 'SmrtWebClient';
57
+ }
58
+
59
+ declare interface SmrtWebCollectionDefinition<TData extends object = object> {
60
+ /** REST collection name (e.g. `products`). */
61
+ name: string;
62
+ /**
63
+ * Canonical qualified model identity (e.g.
64
+ * `@happyvertical/smrt-products:Product`). Generated definitions always
65
+ * provide it; optional here for source compatibility with manual literals
66
+ * authored before policy-aware web collections.
67
+ */
68
+ objectRef?: string;
69
+ /** Source class name (e.g. `Product`). */
70
+ className: string;
71
+ /** Path under the API base path (e.g. `/products`). */
72
+ endpoint: string;
73
+ /** Primary key field name (`id` for SmrtObject). */
74
+ idField: string;
75
+ /** CRUD + custom actions exposed by the api decorator config. */
76
+ actions: string[];
77
+ /**
78
+ * WebMCP/MCP tool descriptors for the exposed actions (#1812). Optional so
79
+ * hand-built definitions (older codegen, tests) still satisfy the type; a
80
+ * missing value means "no WebMCP tools to register".
81
+ */
82
+ toolDescriptors?: WebToolDescriptor[];
83
+ /** Persisted field metadata keyed by field name. */
84
+ fields: Record<string, SmrtWebFieldDefinition>;
85
+ /**
86
+ * Manifest-derived relationship edges to sibling REST collections. Drives
87
+ * relationship-derived cache invalidation: a settled mutation on this
88
+ * collection invalidates the caches of the collections these edges name.
89
+ * Optional so hand-built definitions (older codegen, tests) still satisfy the
90
+ * type; a missing value means "no derived edges".
91
+ */
92
+ relationships?: SmrtWebRelationship[];
93
+ /** Phantom row-type carrier — never present at runtime. */
94
+ _row?: TData;
95
+ }
96
+
97
+ /**
98
+ * Field metadata emitted per column by the `@happyvertical/smrt-virt-web`
99
+ * virtual module (generated from the package manifest).
100
+ */
101
+ declare interface SmrtWebFieldDefinition {
102
+ type: SmrtWebFieldType;
103
+ required?: boolean;
104
+ /** Whether the public field contract explicitly permits `null`. */
105
+ nullable?: boolean;
106
+ default?: unknown;
107
+ /** Developer-authored `@field({ description })` (#2046) — end-user help seed. */
108
+ description?: string;
109
+ /** Static `@field({ ui })` hints (#2046). */
110
+ ui?: SmrtWebFieldUIHints;
111
+ }
112
+
113
+ /**
114
+ * The field types core's web-collection emission actually produces —
115
+ * relationship pseudo-columns (`oneToMany`/`manyToMany`) and STI `meta`
116
+ * internals never reach an emitted definition. Textual mirror of core's
117
+ * `WebFieldType` (this package is deliberately smrt-dependency-free, so it
118
+ * cannot import the source type); the four co-managed sites are core's
119
+ * runtime type, the ambient virt-web d.ts, the physical `@smrt/web` d.ts,
120
+ * and this mirror.
121
+ */
122
+ declare type SmrtWebFieldType = 'text' | 'decimal' | 'boolean' | 'integer' | 'datetime' | 'json' | 'foreignKey' | 'crossPackageRef';
123
+
124
+ /** Static `@field({ ui })` hints (#2046) — the field-policy rail seed. */
125
+ declare interface SmrtWebFieldUIHints {
126
+ basic?: boolean;
127
+ group?: string;
128
+ order?: number;
129
+ locked?: boolean;
130
+ }
131
+
132
+ /**
133
+ * A manifest-derived edge from this collection to a sibling REST collection,
134
+ * emitted by the `@happyvertical/smrt-virt-web` virtual module. When a mutation
135
+ * on this collection settles, the caches of the collections named by these
136
+ * edges are invalidated (relationship-derived invalidation, #1761), so a
137
+ * dependent view refetches without any hand-wired cache key.
138
+ *
139
+ * SMRT-owned data — no client-engine (`@tanstack/*`) type appears here, so it
140
+ * stays inside the engine-absorption boundary.
141
+ */
142
+ declare interface SmrtWebRelationship {
143
+ /** The declaring field carrying the relationship (e.g. `groupId`, `items`). */
144
+ field: string;
145
+ /** The relationship kind, mirroring the manifest field type. */
146
+ kind: SmrtWebRelationshipKind;
147
+ /** REST collection name the edge resolves to (e.g. `ad_groups`). */
148
+ relatedCollection: string;
149
+ }
150
+
151
+ /** The relationship kinds a generated web collection edge can describe. */
152
+ declare type SmrtWebRelationshipKind = 'foreignKey' | 'crossPackageRef' | 'oneToMany' | 'manyToMany';
153
+
154
+ declare interface SmrtWebToolRouteDescriptor {
155
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
156
+ scope: 'item' | 'collection';
157
+ /** Route segments below the collection endpoint; dynamic segments use `[x]`. */
158
+ path: string[];
159
+ /** Transport names rewritten by the tool schema (e.g. `actionId` → `id`). */
160
+ parameterAliases?: Record<string, string>;
161
+ /** The generated method accepts one `options` bag as its sole argument. */
162
+ optionsBag?: boolean;
163
+ }
164
+
165
+ /**
166
+ * One generated collection definition: everything needed to construct a client
167
+ * collection over the generated REST surface. The `_row` property is a phantom
168
+ * type carrier threaded through codegen — it never exists at runtime, it only
169
+ * lets factories infer the row type from a definition.
170
+ */
171
+ /**
172
+ * One WebMCP / MCP tool descriptor for a collection action (#1812). Emitted by
173
+ * the core web-collections codegen as PLAIN DATA (this package has no smrt
174
+ * dependency), shaped to match Chrome's `document.modelContext.registerTool`
175
+ * input — see https://developer.chrome.com/docs/ai/webmcp. Consumed by
176
+ * {@link registerWebMcpTools} in `./webmcp`.
177
+ */
178
+ declare interface WebToolDescriptor {
179
+ /** The action this tool performs (`list` | `get` | … | a custom method name). */
180
+ action: string;
181
+ /** Tool id, `${className.toLowerCase()}_${action}` (e.g. `product_list`). */
182
+ name: string;
183
+ description: string;
184
+ /** JSON Schema for the tool's arguments. */
185
+ inputSchema: Record<string, unknown>;
186
+ /** True for non-mutating reads → WebMCP `annotations.readOnlyHint`. */
187
+ readOnly: boolean;
188
+ /** Generated custom-route transport metadata. */
189
+ route?: SmrtWebToolRouteDescriptor;
190
+ }
191
+
192
+ export { }
package/dist/webmcp.js ADDED
@@ -0,0 +1,2 @@
1
+ import { u as registerWebMcpTools } from "./chunks/src-CDdW9uYx.js";
2
+ export { registerWebMcpTools };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@happyvertical/smrt-web",
3
- "version": "0.42.6",
3
+ "version": "0.43.0",
4
4
  "description": "SMRT browser client data runtime: typed collection factory wrapping the client-data engine over generated REST clients",
5
5
  "author": "HappyVertical",
6
6
  "type": "module",
@@ -16,6 +16,10 @@
16
16
  ".": {
17
17
  "types": "./dist/index.d.ts",
18
18
  "import": "./dist/index.js"
19
+ },
20
+ "./webmcp": {
21
+ "types": "./dist/webmcp.d.ts",
22
+ "import": "./dist/webmcp.js"
19
23
  }
20
24
  },
21
25
  "dependencies": {