@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,327 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { AttributeFilters } from "./screen-search";
3
+
4
+ const componentPartBrand: unique symbol = Symbol("tokamaq.component.part");
5
+ declare const renderFunctionBrand: unique symbol;
6
+ declare const originalElementBrand: unique symbol;
7
+ declare const elementAttributesBrand: unique symbol;
8
+ declare const nativeAttributesBrand: unique symbol;
9
+ const componentActionBrand: unique symbol = Symbol("tokamaq.component.action");
10
+
11
+ export interface ComponentPart<Target extends string, Props extends object, Attributes extends object = {}> {
12
+ readonly [elementAttributesBrand]?: Attributes;
13
+ readonly [componentPartBrand]: (props: Props) => void;
14
+ readonly target: Target;
15
+ }
16
+
17
+ export interface RenderFunction<Props extends object> {
18
+ readonly [renderFunctionBrand]: (props: Props) => void;
19
+ readonly target: string;
20
+ readonly renderTarget?: string;
21
+ }
22
+
23
+ export type ComponentActionKind =
24
+ | "before"
25
+ | "after"
26
+ | "prepend"
27
+ | "append"
28
+ | "layer"
29
+ | "hide"
30
+ | "attach"
31
+ | "attrs";
32
+
33
+ export interface ComponentActionOptions<P extends object = any> {
34
+ order?: number;
35
+ /**
36
+ * Live condition, read with the props of the owning component, so one
37
+ * action can show for some instances only (one message, one thread).
38
+ */
39
+ when?: (props: P) => boolean;
40
+ }
41
+
42
+ type Part<P extends object> = ComponentPart<string, P>;
43
+ type Addition<P extends object> = (props: P) => JSX.Element;
44
+
45
+ type ReservedAttribute =
46
+ | "data-part"
47
+ | "data-part-ignore"
48
+ | "id"
49
+ | "ref"
50
+ | "children"
51
+ | "innerHTML"
52
+ | "textContent"
53
+ | "outerHTML"
54
+ | "innerText";
55
+ type ForbiddenAttribute = ReservedAttribute | `prop:${string}` | `attr:${string}`;
56
+ type EventNamespace = `on:${string}` | `oncapture:${string}`;
57
+
58
+ export function eventNamespace(key: string) {
59
+ return /^(?:on|oncapture):/i.test(key);
60
+ }
61
+
62
+ export function reservedAttribute(key: string) {
63
+ return (
64
+ /^(?:prop:|attr:)/i.test(key) ||
65
+ /^(?:data-part|data-part-ignore|id|ref|children|innerhtml|textcontent|outerhtml|innertext)$/i.test(key)
66
+ );
67
+ }
68
+
69
+ type IntrinsicAttributes<Tag extends string> = Tag extends keyof JSX.IntrinsicElements
70
+ ? JSX.IntrinsicElements[Tag]
71
+ : JSX.IntrinsicElements["div"];
72
+
73
+ export type ElementAttributes<Tag extends string> = { readonly [nativeAttributesBrand]: true } & {
74
+ [K in keyof IntrinsicAttributes<Tag>]: K extends `aria-${string}`
75
+ ? string extends IntrinsicAttributes<Tag>[K]
76
+ ? IntrinsicAttributes<Tag>[K]
77
+ : JSX.EnumeratedPseudoBoolean extends IntrinsicAttributes<Tag>[K]
78
+ ? IntrinsicAttributes<Tag>[K] | boolean
79
+ : IntrinsicAttributes<Tag>[K]
80
+ : IntrinsicAttributes<Tag>[K];
81
+ };
82
+
83
+ type CallableKeys<A> = {
84
+ [K in keyof A]-?: Extract<A[K], (...args: never[]) => unknown> extends never ? never : K;
85
+ }[keyof A];
86
+ type NativeEvents<A> = typeof nativeAttributesBrand extends keyof A
87
+ ? EventNamespace | Lowercase<Extract<CallableKeys<A>, string>>
88
+ : never;
89
+ type MutableAttributes<A extends object> = {
90
+ [
91
+ K in keyof A as K extends string
92
+ ? Lowercase<K> extends Lowercase<ForbiddenAttribute | NativeEvents<A>>
93
+ ? never
94
+ : Extract<A[K], (...args: never[]) => unknown> extends never
95
+ ? K
96
+ : never
97
+ : never
98
+ ]?: K extends "class" ? string : A[K];
99
+ };
100
+ type Forbidden<F extends string = ForbiddenAttribute> = { [K in F]?: never };
101
+ type OriginalProps<A extends object> = MutableAttributes<A> &
102
+ Forbidden<Exclude<ForbiddenAttribute, "children"> | NativeEvents<A>> & { readonly children?: JSX.Element };
103
+
104
+ export interface OriginalElement<A extends object = {}> {
105
+ readonly [originalElementBrand]?: true;
106
+ (props: OriginalProps<A>): JSX.Element;
107
+ Children(): JSX.Element;
108
+ }
109
+
110
+ type Layer<P extends object, A extends object = {}> = (props: P, Original: OriginalElement<A>) => JSX.Element;
111
+ type Attributes<P extends object, A extends object = {}> = (
112
+ props: P,
113
+ current: Readonly<MutableAttributes<A>>,
114
+ ) => MutableAttributes<A>;
115
+
116
+ export type ComponentAction<K extends ComponentActionKind = ComponentActionKind> = Readonly<{
117
+ [componentActionBrand]: K;
118
+ target: string;
119
+ layer?: K extends "layer" ? Layer<any, any> : Addition<any>;
120
+ attributes?: Attributes<any, any>;
121
+ when?: (props: any) => boolean;
122
+ }>;
123
+ export function isComponentAction(value: unknown): value is ComponentAction {
124
+ return !!value && typeof value === "object" && componentActionBrand in value;
125
+ }
126
+
127
+ export function defineComponentAction<K extends ComponentActionKind, P extends object>(
128
+ kind: K,
129
+ target: { readonly target: string },
130
+ layer: ComponentAction<K>["layer"],
131
+ options?: ComponentActionOptions<P>,
132
+ attributes?: Attributes<any, any>,
133
+ ): ComponentAction<K> {
134
+ return Object.freeze({
135
+ [componentActionBrand]: kind,
136
+ target: target.target,
137
+ layer,
138
+ attributes,
139
+ when: options?.when,
140
+ });
141
+ }
142
+
143
+ function insert<K extends "before" | "after" | "prepend" | "append">(kind: K) {
144
+ return <P extends object>(
145
+ target: Part<P>,
146
+ ui: Addition<NoInfer<P>>,
147
+ options?: ComponentActionOptions<NoInfer<P>>,
148
+ ) => defineComponentAction(kind, target, ui, options);
149
+ }
150
+
151
+ export const before = insert("before");
152
+ export const after = insert("after");
153
+ export const prepend = insert("prepend");
154
+ export const append = insert("append");
155
+
156
+ export function attach<P extends object>(
157
+ target: RenderFunction<P>,
158
+ fn: (props: NoInfer<P>) => JSX.Element | void,
159
+ options?: ComponentActionOptions<NoInfer<P>>,
160
+ ) {
161
+ return defineComponentAction(
162
+ "attach",
163
+ { target: target.renderTarget ?? target.target },
164
+ fn as Addition<P>, // A void callback returns undefined, which Solid can render.
165
+ options,
166
+ );
167
+ }
168
+
169
+ export function hide<P extends object>(target: Part<P>, options?: ComponentActionOptions<NoInfer<P>>) {
170
+ return defineComponentAction("hide", target, undefined, options);
171
+ }
172
+
173
+ export function layer<P extends object, A extends object>(
174
+ target: ComponentPart<string, P, A>,
175
+ draw: Layer<NoInfer<P>, NoInfer<A>>,
176
+ options?: ComponentActionOptions<NoInfer<P>>,
177
+ ) {
178
+ return defineComponentAction("layer", target, draw as Layer<any, any>, options);
179
+ }
180
+
181
+ export function attrs<P extends object, A extends object, Values extends object>(
182
+ target: ComponentPart<string, P, A>,
183
+ read: (
184
+ props: NoInfer<P>,
185
+ current: Readonly<MutableAttributes<NoInfer<A>>>,
186
+ ) => Values &
187
+ MutableAttributes<NoInfer<A>> &
188
+ (string extends keyof NoInfer<Values>
189
+ ? unknown
190
+ : Forbidden<ForbiddenAttribute | NativeEvents<A>> &
191
+ UnknownKeys<NoInfer<Values>, MutableAttributes<NoInfer<A>>>),
192
+ options?: ComponentActionOptions<NoInfer<P>>,
193
+ ) {
194
+ return defineComponentAction("attrs", target, undefined, options, read as Attributes<any, any>);
195
+ }
196
+
197
+ export function componentActionKind<K extends ComponentActionKind>(action: ComponentAction<K>): K {
198
+ return action[componentActionBrand];
199
+ }
200
+
201
+ type SearchOptions = AttributeFilters & { in?: string; nth?: number };
202
+ type ElementType = object | string;
203
+ type ScreenMatch = readonly [
204
+ ancestors: string,
205
+ props: object,
206
+ filters?: AttributeFilters,
207
+ attributes?: ElementType,
208
+ ];
209
+ type Keys<T> = T extends unknown ? keyof T : never;
210
+ type Values<T, K extends PropertyKey> = T extends Record<K, unknown> ? T[K] : never;
211
+ type Available<T> = { [K in Keys<T>]?: Values<T, K> };
212
+ type Options<Value> = Value extends readonly ScreenMatch[]
213
+ ? OptionsFromAttributes<NonNullable<Value[number][2]>>
214
+ : {};
215
+ type OptionsFromAttributes<T> = Available<OmitEach<T, "aria">> &
216
+ ([Values<T, "aria">] extends [never] ? {} : { aria?: Available<Values<T, "aria">> });
217
+ type OmitEach<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
218
+
219
+ type AllowedFilters<Value> = { in?: string; nth?: number } & Options<Value>;
220
+ type UnknownKeys<Given, Allowed> = Record<Exclude<keyof Given, keyof Allowed>, never>;
221
+ type CheckFilters<Given, Allowed> = UnknownKeys<Given, Allowed> &
222
+ (Given extends { aria: infer A }
223
+ ? Allowed extends { aria?: infer B }
224
+ ? { aria: UnknownKeys<A, NonNullable<B>> }
225
+ : never
226
+ : unknown);
227
+
228
+ type Fits<Match extends ScreenMatch, Filters> = Filters extends { in: infer Scope extends string }
229
+ ? Scope extends Match[0]
230
+ ? FitsAttributes<Match, Filters>
231
+ : false
232
+ : FitsAttributes<Match, Filters>;
233
+ type FitsAttributes<Match extends ScreenMatch, Filters> =
234
+ (Match[2] extends AttributeFilters ? Match[2] : {}) extends Pick<
235
+ Filters,
236
+ Exclude<keyof Filters, "in" | "nth">
237
+ >
238
+ ? true
239
+ : false;
240
+ type Narrow<
241
+ Matches extends readonly ScreenMatch[],
242
+ Filters,
243
+ Kept extends ScreenMatch[] = [],
244
+ > = Matches extends readonly [infer First extends ScreenMatch, ...infer Rest extends ScreenMatch[]]
245
+ ? Narrow<Rest, Filters, Fits<First, Filters> extends true ? [...Kept, First] : Kept>
246
+ : Kept;
247
+ type AtPosition<
248
+ Matches extends readonly ScreenMatch[],
249
+ Nth extends number,
250
+ Field extends 1 | 3,
251
+ Before extends unknown[] = [unknown],
252
+ > = Matches extends readonly [infer First extends ScreenMatch, ...infer Rest extends ScreenMatch[]]
253
+ ? Before["length"] extends Nth
254
+ ? First[Field]
255
+ : AtPosition<Rest, Nth, Field, [...Before, unknown]>
256
+ : never;
257
+ type MatchField<Matches extends readonly ScreenMatch[], Filters, Field extends 1 | 3> = Filters extends {
258
+ nth: infer N extends number;
259
+ }
260
+ ? number extends N
261
+ ? Matches[number][Field]
262
+ : AtPosition<Matches, N, Field>
263
+ : Matches[number][Field];
264
+ type SearchField<Value extends object, Filters, Field extends 1 | 3> = Value extends readonly ScreenMatch[]
265
+ ? MatchField<
266
+ Extract<keyof Filters, "in" | keyof AttributeFilters> extends never ? Value : Narrow<Value, Filters>,
267
+ Filters,
268
+ Field
269
+ >
270
+ : Field extends 1
271
+ ? Value
272
+ : undefined;
273
+
274
+ type ComponentAt<Module, Name extends string> = Name extends keyof Module
275
+ ? Module[Name]
276
+ : Name extends `${infer Key extends keyof Module & string}.${infer Rest}`
277
+ ? ComponentAt<Module[Key], Rest>
278
+ : never;
279
+
280
+ /** Generated declarations share a module reference for imported component props. */
281
+ export type ComponentAttributes<Module, Names extends string> = {
282
+ [Name in Names]: ComponentAt<Module, Name> extends (props: infer Props, ...rest: any) => unknown
283
+ ? Extract<NonNullable<Props>, object>
284
+ : {};
285
+ };
286
+
287
+ type SearchAttributes<Name extends string, Type, Defaults> = Type extends string
288
+ ? ElementAttributes<Type>
289
+ : Type extends object
290
+ ? Type
291
+ : [Defaults] extends [never]
292
+ ? {}
293
+ : Name extends keyof Defaults
294
+ ? SearchAttributes<Name, Defaults[Name], never>
295
+ : ElementAttributes<Name>;
296
+
297
+ type ElementTarget<Names, Name extends string, Filters, Defaults> = Name extends keyof Names
298
+ ? ComponentPart<
299
+ string,
300
+ SearchField<Extract<Names[Name], object>, Filters, 1>,
301
+ SearchAttributes<Name, SearchField<Extract<Names[Name], object>, Filters, 3>, Defaults>
302
+ >
303
+ : unknown;
304
+ type FunctionTarget<Functions, Name, Filters> =
305
+ Extract<keyof Filters, keyof AttributeFilters> extends never
306
+ ? Name extends keyof Functions
307
+ ? SearchField<Extract<Functions[Name], object>, Filters, 1> extends infer P extends object
308
+ ? [P] extends [never]
309
+ ? unknown
310
+ : RenderFunction<P>
311
+ : never
312
+ : unknown
313
+ : unknown;
314
+ type FindFilters<Names, Name> = AllowedFilters<Name extends keyof Names ? Names[Name] : {}>;
315
+
316
+ /** Separate match lists let a name carry both element and render function props. */
317
+ export type ScreenFind<
318
+ Names extends Record<string, object>,
319
+ Functions extends Record<string, object> = {},
320
+ Defaults extends object = never,
321
+ > = <Name extends (keyof Names | keyof Functions) & string, const Filters extends SearchOptions = {}>(
322
+ name: Name,
323
+ filters?: Filters &
324
+ FindFilters<Names, NoInfer<Name>> &
325
+ CheckFilters<Filters, FindFilters<Names, NoInfer<Name>>>,
326
+ ) => { readonly target: string } & ElementTarget<Names, Name, Filters, Defaults> &
327
+ FunctionTarget<Functions, Name, Filters>;
@@ -0,0 +1,136 @@
1
+ import { onCleanup } from "solid-js";
2
+ import type { AttributeWarning } from "./component-attributes";
3
+ import type { LoadedPartAction } from "./component-parts";
4
+ import { screenElementName } from "./screen-search";
5
+
6
+ type WarningKey = readonly [kind: string, plugin: string, target: string];
7
+ type Alert = { plugins: readonly string[]; messages: Map<string, number>; sticky: boolean };
8
+
9
+ export function componentAlerts(
10
+ notify: (messages: readonly string[]) => void,
11
+ attributeWarning: (warning: AttributeWarning) => void,
12
+ writeWarning: (message: string) => void,
13
+ ) {
14
+ const active = new Map<string, Alert>();
15
+ const logged = new Map<string, Pick<Alert, "plugins" | "sticky">>();
16
+ let skipped: ReadonlySet<string> = new Set();
17
+ const stale = new Map<string, string>();
18
+ let last: string[] = [];
19
+ let pending = false;
20
+ let closed = false;
21
+ onCleanup(() => (closed = true));
22
+
23
+ function changed() {
24
+ if (pending || closed) return;
25
+ pending = true;
26
+ // Several mounted rows and layer stages can change in the same turn.
27
+ queueMicrotask(() => {
28
+ pending = false;
29
+ if (closed) return;
30
+ const next = [
31
+ ...new Set([
32
+ ...skipped,
33
+ ...stale.values(),
34
+ ...[...active.values()].map(({ messages }) => [...messages.keys()].sort()[0]!),
35
+ ]),
36
+ ].sort();
37
+ if (next.length === last.length && next.every((message, i) => message === last[i])) return;
38
+ last = next;
39
+ notify(next);
40
+ });
41
+ }
42
+
43
+ function log(
44
+ key: string,
45
+ plugins: readonly string[],
46
+ message: string,
47
+ sticky = false,
48
+ detail?: AttributeWarning,
49
+ ) {
50
+ if (logged.has(key)) return;
51
+ logged.set(key, { plugins, sticky });
52
+ writeWarning(message);
53
+ if (detail) attributeWarning(detail);
54
+ }
55
+
56
+ function record(key: string, plugins: readonly string[], message: string, sticky = false) {
57
+ let alert = active.get(key);
58
+ if (!alert) {
59
+ alert = { plugins, messages: new Map(), sticky };
60
+ active.set(key, alert);
61
+ } else if (sticky) return;
62
+
63
+ const messages = alert.messages;
64
+ const count = messages.get(message) ?? 0;
65
+ messages.set(message, count + 1);
66
+ if (!count) changed();
67
+ if (sticky) return;
68
+ return () => {
69
+ const count = messages.get(message)!;
70
+ if (count > 1) messages.set(message, count - 1);
71
+ else {
72
+ messages.delete(message);
73
+ if (active.get(key) !== alert) return;
74
+ if (!messages.size) active.delete(key);
75
+ changed();
76
+ }
77
+ };
78
+ }
79
+
80
+ return {
81
+ warn(key: WarningKey, message: string, detail?: AttributeWarning) {
82
+ const plugins = detail?.otherPlugin ? [key[1], detail.otherPlugin] : [key[1]];
83
+ const id = JSON.stringify(key);
84
+ const sticky = detail?.kind === "attr-error";
85
+ if (detail) {
86
+ const release = record(
87
+ id,
88
+ plugins,
89
+ message.replace(detail.target, () => screenElementName(detail.target)),
90
+ sticky,
91
+ );
92
+ if (release) onCleanup(release);
93
+ }
94
+ log(id, plugins, message, sticky, detail);
95
+ },
96
+ failure(item: LoadedPartAction, cause: unknown, consoleMessage: string) {
97
+ const id = JSON.stringify(["error", item.plugin, item.id]);
98
+ const error = cause instanceof Error ? cause.message : String(cause);
99
+ const message = `${item.plugin} (${item.id}) failed on ${screenElementName(item.action.target)}: ${error}`;
100
+ record(id, [item.plugin], message, true);
101
+ log(id, [item.plugin], consoleMessage, true);
102
+ },
103
+ interference(kind: string, cause: LoadedPartAction, affected: LoadedPartAction, message: string) {
104
+ const id = JSON.stringify([kind, cause.plugin, affected.plugin, affected.action.target]);
105
+ const plugins = [cause.plugin, affected.plugin];
106
+ const release = record(id, plugins, message)!;
107
+ log(id, plugins, message);
108
+ return release;
109
+ },
110
+ mismatch(plugin: string, target: string) {
111
+ const key = JSON.stringify([plugin, target]);
112
+ if (stale.has(key)) return;
113
+ const message = `Plugin "${plugin}": targets a different element at ${target}; skipping this addition`;
114
+ stale.set(key, message);
115
+ writeWarning(message);
116
+ changed();
117
+ },
118
+ skipped(messages: string[]) {
119
+ const next = new Set(messages);
120
+ for (const message of next) if (!skipped.has(message)) writeWarning(message);
121
+ skipped = next;
122
+ changed();
123
+ },
124
+ clear(plugin: string, includeFailures = false) {
125
+ let forgotten = false;
126
+ for (const [key, alert] of active)
127
+ if ((!alert.sticky || includeFailures) && alert.plugins.includes(plugin))
128
+ forgotten = active.delete(key) || forgotten;
129
+ for (const key of stale.keys())
130
+ if (JSON.parse(key)[0] === plugin) forgotten = stale.delete(key) || forgotten;
131
+ for (const [key, { plugins, sticky }] of logged)
132
+ if ((!sticky || includeFailures) && plugins.includes(plugin)) logged.delete(key);
133
+ if (forgotten) changed();
134
+ },
135
+ };
136
+ }