@tokamaq/sdk 0.0.0-stage → 0.1.0-dev.2

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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1403 -2
  3. package/build-internals.ts +25 -0
  4. package/caller/agent-launch.ts +60 -0
  5. package/client.tsx +498 -0
  6. package/component-actions.ts +327 -0
  7. package/component-alerts.ts +136 -0
  8. package/component-attributes.ts +340 -0
  9. package/component-parts.tsx +1177 -0
  10. package/core-contracts/auth.ts +40 -0
  11. package/package.json +98 -4
  12. package/quit.ts +58 -0
  13. package/regions.tsx +94 -0
  14. package/screen-search.ts +211 -0
  15. package/send-file.ts +175 -0
  16. package/server-build-internals.ts +11 -0
  17. package/solid.ts +132 -0
  18. package/src/agent-command.ts +48 -0
  19. package/src/agent-info-cache.ts +34 -0
  20. package/src/agent-reports.ts +140 -0
  21. package/src/agent-turn.ts +289 -0
  22. package/src/api.ts +174 -0
  23. package/src/auth.ts +39 -0
  24. package/src/call-error.ts +37 -0
  25. package/src/caller-storage.ts +32 -0
  26. package/src/client.ts +163 -0
  27. package/src/database.ts +322 -0
  28. package/src/functions.ts +726 -0
  29. package/src/hooks.ts +141 -0
  30. package/src/host.ts +211 -0
  31. package/src/http-action.ts +55 -0
  32. package/src/index.ts +126 -0
  33. package/src/internal/contracts.ts +260 -0
  34. package/src/internal/machine-scope.ts +21 -0
  35. package/src/local.ts +22 -0
  36. package/src/plugin.ts +162 -0
  37. package/src/points.ts +79 -0
  38. package/src/protocol.ts +64 -0
  39. package/src/schema.ts +136 -0
  40. package/src/storage.ts +193 -0
  41. package/src/thread-context.ts +42 -0
  42. package/src/transport.ts +421 -0
  43. package/src/triggers.ts +87 -0
  44. package/src/unstable.ts +66 -0
  45. package/src/validators.ts +248 -0
  46. package/src/values.ts +98 -0
  47. package/src/visibility.ts +8 -0
  48. package/src/websocket-transport.ts +91 -0
  49. package/src/window-state-protocol.ts +7 -0
  50. package/src/within.ts +24 -0
  51. package/src/worker-limits.ts +5 -0
  52. package/tools/bundle.mjs +490 -0
  53. package/tools/client-entry.mjs +10 -0
  54. package/tools/contracts.mjs +254 -0
  55. package/tools/http-actions.mjs +42 -0
  56. package/tools/placement.mjs +535 -0
  57. package/tools/plugin-boundary.mjs +592 -0
  58. package/tools/plugin-compiler.mjs +125 -0
  59. package/tools/plugin-descriptor.ts +75 -0
  60. package/tools/points.mjs +340 -0
  61. package/tools/result-validator.mjs +83 -0
  62. package/tools/server-builtins.mjs +82 -0
  63. package/tools/server-files.mjs +34 -0
  64. package/tools/table-references.mjs +98 -0
  65. package/tools/worker.mjs +110 -0
  66. package/tsconfig.plugin-server.json +12 -0
  67. package/tsconfig.plugin-window.json +15 -0
  68. package/view.ts +27 -0
  69. package/web.d.ts +6 -0
  70. package/window-state-host.ts +228 -0
  71. package/window-state.ts +43 -0
@@ -0,0 +1,340 @@
1
+ import { assign, className, DOMWithState } from "@solidjs/web";
2
+ import { createRenderEffect, createSignal, onCleanup } from "solid-js";
3
+ import { eventNamespace, reservedAttribute } from "./component-actions";
4
+ import type { LoadedPartAction, PartRegistry } from "./component-parts";
5
+
6
+ type Values = Record<string, unknown>;
7
+ type NativeAttributes = { key(key: string): string; event(key: string): boolean };
8
+ export type AttributeLayer = { item: LoadedPartAction; values: Values };
9
+ export type AttributeLayers = () => readonly AttributeLayer[];
10
+ export const noAttributeLayers: AttributeLayers = () => [];
11
+ const empty = Object.freeze(Object.create(null) as Values);
12
+ const exposed = { enumerable: true, configurable: true };
13
+
14
+ export type AttributeWarning = {
15
+ kind: "attr-conflict" | "attr-covered" | "attr-invalid" | "attr-error" | "attr-legacy";
16
+ plugin: string;
17
+ target: string;
18
+ otherPlugin?: string;
19
+ attribute?: string;
20
+ };
21
+
22
+ export function warnAttribute(
23
+ registry: PartRegistry,
24
+ warning: AttributeWarning,
25
+ message: string,
26
+ exportId?: string,
27
+ ) {
28
+ const { kind, plugin, target, attribute, otherPlugin } = warning;
29
+ // Once per attribute where one is named, so fixing the first bad key does
30
+ // not hide the next one.
31
+ const key =
32
+ exportId ?? (otherPlugin || attribute ? JSON.stringify([target, attribute, otherPlugin]) : target);
33
+ registry.warn([kind === "attr-error" ? "error" : kind, plugin, key], message, warning);
34
+ }
35
+
36
+ export function installationOrder(a: LoadedPartAction, b: LoadedPartAction) {
37
+ return (
38
+ (a.installOrder ?? Infinity) - (b.installOrder ?? Infinity) ||
39
+ a.order - b.order ||
40
+ a.plugin.localeCompare(b.plugin) ||
41
+ a.id.localeCompare(b.id)
42
+ );
43
+ }
44
+
45
+ /** Return only add-on values. Unchanged properties still read from the author. */
46
+ export function attributeValues(
47
+ registry: PartRegistry,
48
+ props: object,
49
+ owner: Values,
50
+ actions: readonly LoadedPartAction[],
51
+ layers?: readonly AttributeLayer[],
52
+ native?: NativeAttributes,
53
+ ): Values {
54
+ if (!actions.length && !layers?.length) return empty;
55
+ const changes = Object.create(null) as Values;
56
+ const written = new Map<string, string>();
57
+
58
+ const forbidden = (item: LoadedPartAction, key: string) =>
59
+ reservedAttribute(key) || native?.event(key) || item.callableAttributes?.has(key);
60
+ const name = (key: string) => native?.key(key) ?? key;
61
+
62
+ function apply(item: LoadedPartAction, values: Values, reads?: Set<string>) {
63
+ // Children are drawn by the layer stage. Reading that getter here would
64
+ // render a second copy of the children in the attribute effect.
65
+ const keys = Object.keys(values).filter((key) => reads || key !== "children");
66
+ const entries = keys.map((raw) => {
67
+ const blocked = forbidden(item, raw);
68
+ return { raw, key: name(raw), blocked, value: blocked ? undefined : values[raw] };
69
+ });
70
+ for (const { raw, key, blocked, value } of entries) {
71
+ if (blocked || typeof value === "function" || (key !== "class" && typeof owner[key] === "function")) {
72
+ warnAttribute(
73
+ registry,
74
+ { kind: "attr-invalid", plugin: item.plugin, target: item.action.target, attribute: raw },
75
+ `Plugin "${item.plugin}" cannot change attribute "${raw}" on ${item.action.target}`,
76
+ );
77
+ continue;
78
+ }
79
+ if (value === undefined) continue;
80
+ const previous = written.get(key);
81
+ if (key === "class") {
82
+ if (typeof value === "string" && value) {
83
+ const base = changes.class ?? owner.class;
84
+ // Solid accepts nested arrays of classes, including object forms.
85
+ changes.class =
86
+ typeof base === "string" && base ? `${base} ${value}` : base ? [base, value] : value;
87
+ }
88
+ continue;
89
+ }
90
+ if (reads && previous && previous !== item.plugin && !reads.has(key)) {
91
+ warnAttribute(
92
+ registry,
93
+ {
94
+ kind: "attr-conflict",
95
+ plugin: item.plugin,
96
+ target: item.action.target,
97
+ otherPlugin: previous,
98
+ attribute: key,
99
+ },
100
+ `Plugin "${item.plugin}" overwrote "${key}" from plugin "${previous}" on ${item.action.target} without reading current.${key}`,
101
+ );
102
+ }
103
+ written.set(key, item.plugin);
104
+ changes[key] = value;
105
+ }
106
+ }
107
+
108
+ function safely(item: LoadedPartAction, run: () => void) {
109
+ try {
110
+ run();
111
+ } catch (cause) {
112
+ warnAttribute(
113
+ registry,
114
+ { kind: "attr-error", plugin: item.plugin, target: item.action.target },
115
+ // The same wording as every other failing action, so the alert list reads as one.
116
+ `${item.plugin} (${item.id}) failed on ${item.action.target}: ${cause instanceof Error ? cause.message : String(cause)}`,
117
+ item.id,
118
+ );
119
+ }
120
+ }
121
+
122
+ for (const item of actions) {
123
+ if (!item.enabled(props)) continue;
124
+ const reads = new Set<string>();
125
+ const current = new Proxy(Object.create(null) as Values, {
126
+ get(_, key) {
127
+ if (typeof key !== "string" || forbidden(item, key)) return;
128
+ key = name(key);
129
+ reads.add(key);
130
+ return Object.hasOwn(changes, key) ? changes[key] : owner[key];
131
+ },
132
+ has(_, key) {
133
+ if (typeof key !== "string" || forbidden(item, key)) return false;
134
+ key = name(key);
135
+ reads.add(key);
136
+ return key in changes || key in owner;
137
+ },
138
+ ownKeys: () =>
139
+ [...new Set([...Object.keys(owner), ...Object.keys(changes)])].filter((key) => !forbidden(item, key)),
140
+ getOwnPropertyDescriptor(_, key) {
141
+ if (typeof key !== "string" || forbidden(item, key)) return;
142
+ key = name(key);
143
+ reads.add(key);
144
+ if (Object.hasOwn(changes, key) || Object.hasOwn(owner, key)) return exposed;
145
+ },
146
+ });
147
+ safely(item, () => apply(item, item.action.attributes!(props, current), reads));
148
+ }
149
+ for (const { item, values } of layers ?? []) safely(item, () => apply(item, values));
150
+ return changes;
151
+ }
152
+
153
+ const html = "http://www.w3.org/1999/xhtml";
154
+ const booleanAttributes = new Set(
155
+ (
156
+ "allowfullscreen alpha async autofocus autoplay checked compact controls credentialless declare default defer " +
157
+ "disablepictureinpicture disableremoteplayback disabled formnovalidate hidden inert ismap itemscope loop " +
158
+ "multiple muted nohref nomodule noresize noshade novalidate nowrap open playsinline readonly required reversed " +
159
+ "scoped seamless selected shadowrootclonable shadowrootcustomelementregistry shadowrootdelegatesfocus shadowrootserializable " +
160
+ "truespeed typemustmatch webkitdirectory"
161
+ ).split(" "),
162
+ );
163
+
164
+ /** Owner bindings keep writing to the original node. While an overlay is
165
+ * active, save those writes separately so disabling it reveals the latest
166
+ * owner state, including Solid's incremental class and style operations. */
167
+ export function bindNativeAttributes(
168
+ element: Element,
169
+ read: (owner: Values, native: NativeAttributes) => Values,
170
+ ) {
171
+ // Mirror attributes only; cloning a custom element would run its constructor.
172
+ const base = element.ownerDocument.createElementNS(
173
+ element.namespaceURI,
174
+ element.namespaceURI === html ? "div" : "g",
175
+ );
176
+ for (const attr of Array.from(element.attributes))
177
+ base.setAttributeNS(attr.namespaceURI, attr.name, attr.value);
178
+ const stateful = DOMWithState[element.nodeName] ?? {};
179
+ const baseline = new Map<string, unknown>();
180
+ const [revision, revise] = createSignal(0, { ownedWrite: true });
181
+ const restore: (() => void)[] = [];
182
+ let applied: Values = empty;
183
+ let applying = false;
184
+ const normalize = (key: string) => (element.namespaceURI === html ? key.toLowerCase() : key);
185
+ const covered = (key: string) => Object.keys(applied).some((name) => normalize(name) === key);
186
+ const dirty = () => revise((prev) => prev + 1);
187
+
188
+ function patch(on: object, key: string, descriptor: PropertyDescriptor) {
189
+ const previous = Object.getOwnPropertyDescriptor(on, key);
190
+ Object.defineProperty(on, key, { ...descriptor, configurable: true });
191
+ restore.push(() => {
192
+ if (previous) Object.defineProperty(on, key, previous);
193
+ else Reflect.deleteProperty(on, key);
194
+ });
195
+ }
196
+
197
+ function mutate<T extends object, K extends keyof T & string>(
198
+ target: T,
199
+ mirror: T,
200
+ method: K,
201
+ keyOf: (...args: any[]) => string,
202
+ ) {
203
+ const write = target[method] as (...args: unknown[]) => unknown;
204
+ patch(target, method, {
205
+ value: function (this: T, ...args: unknown[]) {
206
+ if (applying || this !== target) return write.apply(this, args);
207
+ const key = normalize(keyOf(...args));
208
+ const before = base.getAttribute(key);
209
+ const result = write.apply(mirror, args);
210
+ if (!covered(key)) write.apply(this, args);
211
+ if (base.getAttribute(key) !== before) dirty();
212
+ return result;
213
+ },
214
+ writable: true,
215
+ });
216
+ }
217
+
218
+ mutate(element, base, "setAttribute", (key: string) => key);
219
+ mutate(element, base, "removeAttribute", (key: string) => key);
220
+ mutate(element, base, "toggleAttribute", (key: string) => key);
221
+ mutate(element, base, "setAttributeNS", (_ns: string, key: string) => key);
222
+ mutate(
223
+ element,
224
+ base,
225
+ "removeAttributeNS",
226
+ (ns: string, local: string) => base.getAttributeNodeNS(ns, local)?.name ?? local,
227
+ );
228
+
229
+ for (const method of ["add", "remove", "replace", "toggle"] as const)
230
+ mutate(element.classList, base.classList, method, () => "class");
231
+
232
+ function setter(on: object, key: string, write: (value: unknown, set: (value: unknown) => void) => void) {
233
+ let prototype: object | null = on;
234
+ let descriptor: PropertyDescriptor | undefined;
235
+ while (prototype && !(descriptor = Object.getOwnPropertyDescriptor(prototype, key)))
236
+ prototype = Object.getPrototypeOf(prototype) as object | null;
237
+ if (!descriptor?.set || !descriptor.get) return;
238
+ const set = descriptor.set.bind(on);
239
+ patch(on, key, {
240
+ get: descriptor.get.bind(on),
241
+ set: (value) => (applying ? set(value) : write(value, set)),
242
+ });
243
+ }
244
+
245
+ if ("style" in element && "style" in base) {
246
+ const style = (element as HTMLElement).style;
247
+ const ownerStyle = (base as HTMLElement).style;
248
+ for (const method of ["setProperty", "removeProperty"] as const)
249
+ mutate(style, ownerStyle, method, () => "style");
250
+ setter(style, "cssText", (value, set) => {
251
+ ownerStyle.cssText = String(value);
252
+ if (!covered("style")) set(value);
253
+ dirty();
254
+ });
255
+ }
256
+ setter(element, "className", (value, set) => {
257
+ base.setAttribute("class", String(value));
258
+ if (!covered("class")) set(value);
259
+ dirty();
260
+ });
261
+ for (const key of Object.keys(stateful)) {
262
+ baseline.set(key, Reflect.get(element, key));
263
+ setter(element, key, (value, set) => {
264
+ if (!covered(normalize(key))) set(value);
265
+ if (!Object.is(baseline.get(key), value)) {
266
+ baseline.set(key, value);
267
+ dirty();
268
+ }
269
+ });
270
+ }
271
+
272
+ const native: NativeAttributes = {
273
+ key: (key) => (key === "className" ? "class" : key),
274
+ event: (key) => eventNamespace(key) || (/^on/i.test(key) && key.toLowerCase() in element),
275
+ };
276
+ const owner = new Proxy(Object.create(null) as Values, {
277
+ get(_, key) {
278
+ if (typeof key !== "string") return;
279
+ if (baseline.has(key)) return baseline.get(key);
280
+ const value = base.getAttribute(key);
281
+ return element.namespaceURI === html && booleanAttributes.has(key.toLowerCase())
282
+ ? value !== null
283
+ : (value ?? undefined);
284
+ },
285
+ has: (_, key) => typeof key === "string" && (baseline.has(key) || base.hasAttribute(key)),
286
+ ownKeys: () => [...new Set([...base.getAttributeNames(), ...baseline.keys()])],
287
+ getOwnPropertyDescriptor: (_, key) =>
288
+ typeof key === "string" && (baseline.has(key) || base.hasAttribute(key)) ? exposed : undefined,
289
+ });
290
+
291
+ function paint(next: Values) {
292
+ applying = true;
293
+ try {
294
+ for (const key of Object.keys(applied)) {
295
+ if (Object.hasOwn(next, key)) continue;
296
+ if (baseline.has(key)) Reflect.set(element, key, baseline.get(key));
297
+ else {
298
+ const attribute = base.getAttributeNode(key);
299
+ if (attribute?.namespaceURI)
300
+ element.setAttributeNS(attribute.namespaceURI, attribute.name, attribute.value);
301
+ else if (attribute) element.setAttribute(key, attribute.value);
302
+ else element.removeAttribute(key);
303
+ }
304
+ }
305
+ for (const [key, value] of Object.entries(next)) {
306
+ if (key === "class" || key === "style") {
307
+ // Normalize with Solid without sharing its owner binding caches.
308
+ const scratch = element.ownerDocument.createElement("div");
309
+ if (key === "class") className(scratch, value as Parameters<typeof className>[1]);
310
+ else assign(scratch, { style: value }, true);
311
+ const attr = scratch.getAttribute(key);
312
+ if (attr === null) element.removeAttribute(key);
313
+ else element.setAttribute(key, attr);
314
+ } else if (typeof value === "boolean" && key.startsWith("aria-")) {
315
+ element.setAttribute(key, String(value));
316
+ } else if (/^on/i.test(key)) {
317
+ // Solid reserves camel-case on* names even when the DOM has no such event.
318
+ if (value === null || value === undefined || value === false) element.removeAttribute(key);
319
+ else element.setAttribute(key, value === true ? "" : String(value));
320
+ } else assign(element, { [key]: value }, true);
321
+ }
322
+ } finally {
323
+ applying = false;
324
+ applied = next;
325
+ }
326
+ }
327
+
328
+ onCleanup(() => {
329
+ paint(empty);
330
+ for (const undo of restore.reverse()) undo();
331
+ });
332
+ createRenderEffect(
333
+ () => {
334
+ revision();
335
+ return read(owner, native);
336
+ },
337
+ paint,
338
+ { sync: true, transparent: true },
339
+ );
340
+ }