@native-sdk/cli 0.4.3 → 0.5.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.
Files changed (100) hide show
  1. package/build/app.zig +308 -72
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +336 -7
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/core/SKILL.md +3 -0
  28. package/skill-data/native-ui/SKILL.md +30 -0
  29. package/skill-data/ts-core/SKILL.md +493 -0
  30. package/skill-data/zig/SKILL.md +208 -0
  31. package/skills/native-sdk/SKILL.md +1 -1
  32. package/src/app_runner/root.zig +75 -9
  33. package/src/app_runner/ts_core_main.zig +231 -0
  34. package/src/platform/linux/gtk_host.c +226 -21
  35. package/src/platform/windows/webview2_host.cpp +16 -10
  36. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  37. package/src/primitives/app_manifest/root.zig +13 -0
  38. package/src/primitives/app_manifest/types.zig +14 -0
  39. package/src/primitives/app_manifest/validation.zig +18 -0
  40. package/src/primitives/app_manifest/web_layer.zig +366 -0
  41. package/src/primitives/canvas/commands.zig +14 -7
  42. package/src/primitives/canvas/lazy_tls.zig +73 -0
  43. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  44. package/src/primitives/canvas/root.zig +6 -0
  45. package/src/primitives/canvas/tests.zig +1 -0
  46. package/src/primitives/canvas/text_atlas.zig +40 -32
  47. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  48. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  49. package/src/primitives/canvas/text_spans.zig +39 -26
  50. package/src/primitives/canvas/tokens.zig +54 -0
  51. package/src/primitives/canvas/ui.zig +125 -0
  52. package/src/primitives/canvas/ui_markup.zig +22 -3
  53. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  54. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  55. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  56. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  57. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  58. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  60. package/src/primitives/canvas/ui_schema.zig +8 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  62. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  63. package/src/primitives/canvas/widget_render.zig +25 -15
  64. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  65. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  66. package/src/root.zig +7 -0
  67. package/src/runtime/api.zig +7 -0
  68. package/src/runtime/bridge_responses.zig +2 -0
  69. package/src/runtime/builtin_bridge.zig +6 -0
  70. package/src/runtime/canvas_frame.zig +57 -36
  71. package/src/runtime/canvas_images.zig +9 -3
  72. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  73. package/src/runtime/canvas_widget_runtime.zig +7 -4
  74. package/src/runtime/effects.zig +490 -2
  75. package/src/runtime/effects_host_tests.zig +594 -0
  76. package/src/runtime/flow.zig +8 -0
  77. package/src/runtime/platform_bridge_tests.zig +1 -0
  78. package/src/runtime/root.zig +18 -0
  79. package/src/runtime/session_replay.zig +7 -1
  80. package/src/runtime/session_tests.zig +35 -8
  81. package/src/runtime/tests.zig +2 -0
  82. package/src/runtime/ts_core_host.zig +1505 -0
  83. package/src/runtime/ts_core_host_tests.zig +1408 -0
  84. package/src/runtime/ts_ui_app.zig +469 -0
  85. package/src/runtime/ui_app.zig +58 -0
  86. package/src/runtime/view_canvas.zig +24 -17
  87. package/src/runtime/view_widget_tree.zig +2 -2
  88. package/src/runtime/web_layer_gate_tests.zig +242 -0
  89. package/src/runtime/window_storage.zig +5 -0
  90. package/src/runtime/window_view_runtime.zig +1 -0
  91. package/src/tooling/buildgraph.zig +7 -1
  92. package/src/tooling/codesign.zig +212 -105
  93. package/src/tooling/doctor.zig +78 -0
  94. package/src/tooling/manifest.zig +314 -1
  95. package/src/tooling/package.zig +722 -18
  96. package/src/tooling/raw_manifest.zig +16 -0
  97. package/src/tooling/root.zig +1 -0
  98. package/src/tooling/templates.zig +911 -88
  99. package/src/tooling/ts_core.zig +486 -0
  100. package/src/tooling/verbs.zig +22 -0
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env node
2
+ // @native-sdk/core: transpile an app-core subset TypeScript module to Zig.
3
+ //
4
+ // native-core <entry.ts> -o <out.zig> [--frame-cap <bytes>] [--heap-cap <bytes>]
5
+ //
6
+ // --frame-cap / --heap-cap set the emitted core's rt kernel capacities (the
7
+ // frame arena and the per-space model heap) as comptime constants; omitted,
8
+ // the rt defaults apply.
9
+ //
10
+ // Exit codes: 0 emitted; 1 subset/type errors (teaching diagnostics on
11
+ // stderr); 2 usage.
12
+
13
+ import { transpileFile, formatDiagnostic, type TranspileOptions } from "./transpile.ts";
14
+ import fs from "node:fs";
15
+
16
+ function parseByteCount(flag: string, raw: string | undefined): number | null {
17
+ const v = raw === undefined ? NaN : Number(raw);
18
+ if (!Number.isSafeInteger(v) || v <= 0) {
19
+ console.error(`${flag} needs a positive integer byte count, got ${raw ?? "<missing>"}`);
20
+ return null;
21
+ }
22
+ return v;
23
+ }
24
+
25
+ function main(argv: string[]): number {
26
+ const args = argv.slice(2);
27
+ let entry: string | null = null;
28
+ let out: string | null = null;
29
+ let frameCap: number | undefined;
30
+ let heapCap: number | undefined;
31
+ for (let i = 0; i < args.length; i++) {
32
+ if (args[i] === "-o" || args[i] === "--out") {
33
+ out = args[++i] ?? null;
34
+ } else if (args[i] === "--frame-cap") {
35
+ const v = parseByteCount("--frame-cap", args[++i]);
36
+ if (v === null) return 2;
37
+ frameCap = v;
38
+ } else if (args[i] === "--heap-cap") {
39
+ const v = parseByteCount("--heap-cap", args[++i]);
40
+ if (v === null) return 2;
41
+ heapCap = v;
42
+ } else if (!args[i].startsWith("-")) {
43
+ entry = args[i];
44
+ } else {
45
+ console.error(`unknown flag ${args[i]}`);
46
+ return 2;
47
+ }
48
+ }
49
+ if (!entry) {
50
+ console.error("usage: native-core <entry.ts> -o <out.zig> [--frame-cap <bytes>] [--heap-cap <bytes>]");
51
+ return 2;
52
+ }
53
+ const options: TranspileOptions = { frameCap, heapCap };
54
+ const result = transpileFile(entry, options);
55
+ for (const e of result.typeErrors) console.error(e);
56
+ for (const d of result.diagnostics) console.error(formatDiagnostic(d));
57
+ // Teaching notices (NS1028): printed, never failing the build.
58
+ for (const w of result.warnings) console.error(formatDiagnostic(w, "warning"));
59
+ if (!result.ok || result.zig === null) return 1;
60
+ if (out) {
61
+ fs.writeFileSync(out, result.zig);
62
+ } else {
63
+ process.stdout.write(result.zig);
64
+ }
65
+ return 0;
66
+ }
67
+
68
+ process.exit(main(process.argv));
@@ -0,0 +1,291 @@
1
+ #!/usr/bin/env node
2
+ // The core-logic dev harness: run a TypeScript app core under node with a
3
+ // virtual host — the fastest behavioral loop for update/effects work. This
4
+ // is NOT a renderer: no window, no markup, no pixels (`native dev` runs the
5
+ // real app). What it gives you is the dispatch cycle at conversation speed:
6
+ //
7
+ // node devhost.ts src/core.ts # interactive (NDJSON on stdin)
8
+ // node devhost.ts src/core.ts --script msgs.ndjson
9
+ //
10
+ // Input, one JSON object per line:
11
+ // {"kind":"add"} dispatch a Msg (payload fields ride
12
+ // along; {"$bytes":"text"} values
13
+ // become Uint8Array)
14
+ // {"advance": 500} advance the virtual clock 500ms,
15
+ // firing due Sub timers and Cmd.delay
16
+ // one-shots in time order
17
+ //
18
+ // Transcript, one line per fact:
19
+ // model <json> the committed model after a dispatch
20
+ // (Uint8Array fields as {"$bytes":...})
21
+ // cmd <op> <details> an effect the dispatch requested
22
+ // sub arm|re-arm|cancel <key> subscription reconciliation by key
23
+ // fire <key> -> <kind> @ <ms> a virtual timer fired (dispatched)
24
+ //
25
+ // Virtual-host semantics match the run-fidelity harness (node = native):
26
+ // - Cmd.now dispatches its arm immediately at the current virtual time;
27
+ // - Sub.timer reconciles by key after every commit (new key or changed
28
+ // interval arms, missing key cancels), each fire dispatching the named
29
+ // arm with the virtual time;
30
+ // - Cmd.delay arms a one-shot; re-issuing a live key re-arms from now
31
+ // (the debounce discipline), Cmd.cancel drops it;
32
+ // - timer/now/delay arms carry exactly one number payload field (pinned
33
+ // by tsc), so the harness constructs them shape-directed without
34
+ // needing the field's name.
35
+ // Every other effect (files, fetch, clipboard, spawn, audio, host commands)
36
+ // is printed as `cmd ...` and NOT performed — feed its result back yourself
37
+ // as an ordinary Msg line. That is the point: results are plain messages,
38
+ // and the loop stays deterministic.
39
+
40
+ import fs from "node:fs";
41
+ import path from "node:path";
42
+ import readline from "node:readline";
43
+ import { register } from "node:module";
44
+ import { pathToFileURL } from "node:url";
45
+ import { installTextMethods } from "./text_polyfill.ts";
46
+
47
+ interface Cmdish {
48
+ readonly op: string;
49
+ readonly [field: string]: unknown;
50
+ }
51
+
52
+ function usage(): never {
53
+ console.error("usage: devhost.ts <core.ts> [--script <msgs.ndjson>]");
54
+ console.error("core-logic loop only (update/effects under a virtual host) - not a renderer;");
55
+ console.error("run the real app with `native dev`.");
56
+ process.exit(2);
57
+ }
58
+
59
+ const args = process.argv.slice(2);
60
+ let entry: string | null = null;
61
+ let script: string | null = null;
62
+ for (let i = 0; i < args.length; i++) {
63
+ if (args[i] === "--script") script = args[++i] ?? null;
64
+ else if (args[i] === "--help" || args[i] === "-h") usage();
65
+ else if (!args[i].startsWith("-")) entry = args[i];
66
+ else usage();
67
+ }
68
+ if (!entry) usage();
69
+
70
+ // The resolver hook maps "@native-sdk/core" onto this package's own SDK
71
+ // module (app trees carry no node_modules for bare resolution to find),
72
+ // and the byte-text methods (s.toUpperCase(), s.split(sep), ...) install
73
+ // on Uint8Array.prototype before the core loads — the same tables the
74
+ // native rt helpers use, so node runs are byte-identical by construction.
75
+ installTextMethods();
76
+ register(new URL("./devhost_resolver.mjs", import.meta.url));
77
+
78
+ const mod = await import(pathToFileURL(path.resolve(entry)).href);
79
+ if (typeof mod.initialModel !== "function" || typeof mod.update !== "function") {
80
+ console.error(`${entry} is not an app core: it must export initialModel() and update(model, msg)`);
81
+ process.exit(1);
82
+ }
83
+
84
+ // ---------------------------------------------------------- transcript i/o
85
+
86
+ const decoder = new TextDecoder();
87
+ const encoder = new TextEncoder();
88
+
89
+ function jsonable(value: unknown): unknown {
90
+ if (value instanceof Uint8Array) return { $bytes: decoder.decode(value) };
91
+ if (Array.isArray(value)) return value.map(jsonable);
92
+ if (value !== null && typeof value === "object") {
93
+ const out: Record<string, unknown> = {};
94
+ for (const [k, v] of Object.entries(value)) out[k] = jsonable(v);
95
+ return out;
96
+ }
97
+ return value;
98
+ }
99
+
100
+ function revive(value: unknown): unknown {
101
+ if (Array.isArray(value)) return value.map(revive);
102
+ if (value !== null && typeof value === "object") {
103
+ const record = value as Record<string, unknown>;
104
+ if (typeof record.$bytes === "string" && Object.keys(record).length === 1) {
105
+ return encoder.encode(record.$bytes);
106
+ }
107
+ const out: Record<string, unknown> = {};
108
+ for (const [k, v] of Object.entries(record)) out[k] = revive(v);
109
+ return out;
110
+ }
111
+ return value;
112
+ }
113
+
114
+ function say(line: string): void {
115
+ process.stdout.write(line + "\n");
116
+ }
117
+
118
+ // --------------------------------------------------------- the virtual host
119
+
120
+ let now = 0;
121
+ /// Armed repeating timers (Sub.timer), by key.
122
+ const timers = new Map<string, { everyMs: number; msgKind: string; nextAt: number }>();
123
+ /// Armed one-shots (Cmd.delay), by key.
124
+ const delays = new Map<string, { msgKind: string; at: number }>();
125
+
126
+ /// Timer/now/delay arms carry exactly one number payload field (tsc pins
127
+ /// the shape), so a proxy that answers every non-kind read with the
128
+ /// timestamp IS the arm value — no field name needed.
129
+ function timestampMsg(kind: string, atMs: number): unknown {
130
+ return new Proxy(
131
+ { kind },
132
+ {
133
+ get: (target, prop) => (prop === "kind" ? kind : atMs),
134
+ has: () => true,
135
+ },
136
+ );
137
+ }
138
+
139
+ function performCmd(cmd: Cmdish): void {
140
+ switch (cmd.op) {
141
+ case "none":
142
+ return;
143
+ case "batch":
144
+ for (const inner of cmd.cmds as Cmdish[]) performCmd(inner);
145
+ return;
146
+ case "now":
147
+ say(`cmd now -> ${cmd.msgKind} @ ${now}`);
148
+ dispatch(timestampMsg(cmd.msgKind as string, now));
149
+ return;
150
+ case "delay": {
151
+ const key = cmd.key as string;
152
+ const rearmed = delays.has(key);
153
+ delays.set(key, { msgKind: cmd.msgKind as string, at: now + (cmd.afterMs as number) });
154
+ say(`cmd delay ${rearmed ? "re-arm" : "arm"} ${key} +${cmd.afterMs}ms -> ${cmd.msgKind}`);
155
+ return;
156
+ }
157
+ case "cancel": {
158
+ const key = cmd.key as string;
159
+ if (delays.delete(key)) {
160
+ say(`cmd cancel ${key} (delay dropped)`);
161
+ } else {
162
+ say(`cmd cancel ${key} (not performed here - a live request or named op drops silently; a live spawn ends loudly, err arm "cancelled")`);
163
+ }
164
+ return;
165
+ }
166
+ default: {
167
+ // Every other effect is the host's job; print the request and let
168
+ // the author feed the result back as an ordinary Msg line.
169
+ const details = Object.entries(cmd)
170
+ .filter(([k]) => k !== "op")
171
+ .map(([k, v]) => `${k}=${JSON.stringify(jsonable(v))}`)
172
+ .join(" ");
173
+ say(`cmd ${cmd.op} ${details}`.trimEnd() + " (not performed - feed the result back as a Msg line)");
174
+ return;
175
+ }
176
+ }
177
+ }
178
+
179
+ function reconcileSubs(): void {
180
+ if (typeof mod.subscriptions !== "function") return;
181
+ const declared = new Map<string, { everyMs: number; msgKind: string }>();
182
+ const collect = (sub: Cmdish): void => {
183
+ if (sub.op === "timer") {
184
+ declared.set(sub.key as string, { everyMs: sub.everyMs as number, msgKind: sub.msgKind as string });
185
+ } else if (sub.op === "batch") {
186
+ for (const inner of sub.subs as Cmdish[]) collect(inner);
187
+ }
188
+ };
189
+ collect(mod.subscriptions(model) as Cmdish);
190
+ for (const [key, spec] of declared) {
191
+ const active = timers.get(key);
192
+ if (!active) {
193
+ timers.set(key, { ...spec, nextAt: now + spec.everyMs });
194
+ say(`sub arm ${key} every ${spec.everyMs}ms -> ${spec.msgKind}`);
195
+ } else if (active.everyMs !== spec.everyMs || active.msgKind !== spec.msgKind) {
196
+ timers.set(key, { ...spec, nextAt: now + spec.everyMs });
197
+ say(`sub re-arm ${key} every ${spec.everyMs}ms -> ${spec.msgKind}`);
198
+ }
199
+ }
200
+ for (const key of [...timers.keys()]) {
201
+ if (!declared.has(key)) {
202
+ timers.delete(key);
203
+ say(`sub cancel ${key}`);
204
+ }
205
+ }
206
+ }
207
+
208
+ function advance(ms: number): void {
209
+ const deadline = now + ms;
210
+ for (;;) {
211
+ // The earliest due fire wins; ties go to timers in arm order.
212
+ let dueAt = Infinity;
213
+ let fire: (() => void) | null = null;
214
+ for (const [key, timer] of timers) {
215
+ if (timer.nextAt <= deadline && timer.nextAt < dueAt) {
216
+ dueAt = timer.nextAt;
217
+ fire = () => {
218
+ timer.nextAt += timer.everyMs;
219
+ say(`fire ${key} -> ${timer.msgKind} @ ${now}`);
220
+ dispatch(timestampMsg(timer.msgKind, now));
221
+ };
222
+ }
223
+ }
224
+ for (const [key, delay] of delays) {
225
+ if (delay.at <= deadline && delay.at < dueAt) {
226
+ dueAt = delay.at;
227
+ fire = () => {
228
+ delays.delete(key);
229
+ say(`fire ${key} -> ${delay.msgKind} @ ${now}`);
230
+ dispatch(timestampMsg(delay.msgKind, now));
231
+ };
232
+ }
233
+ }
234
+ if (!fire) break;
235
+ now = dueAt;
236
+ fire();
237
+ }
238
+ now = deadline;
239
+ }
240
+
241
+ // ------------------------------------------------------------ the dispatch
242
+
243
+ let model: unknown = null;
244
+ boot();
245
+
246
+ function boot(): void {
247
+ const booted = mod.initialModel();
248
+ const [first, cmd] = Array.isArray(booted) ? booted : [booted, null];
249
+ say(`model ${JSON.stringify(jsonable(first))}`);
250
+ model = first;
251
+ if (cmd) performCmd(cmd as Cmdish);
252
+ reconcileSubs();
253
+ }
254
+
255
+ function dispatch(msg: unknown): void {
256
+ const result = mod.update(model, msg);
257
+ const [next, cmd] = Array.isArray(result) ? result : [result, null];
258
+ model = next;
259
+ say(`model ${JSON.stringify(jsonable(model))}`);
260
+ if (cmd) performCmd(cmd as Cmdish);
261
+ reconcileSubs();
262
+ }
263
+
264
+ function handleLine(raw: string): void {
265
+ const line = raw.trim();
266
+ if (line.length === 0 || line.startsWith("#")) return;
267
+ let parsed: unknown;
268
+ try {
269
+ parsed = JSON.parse(line);
270
+ } catch {
271
+ console.error(`not JSON: ${line}`);
272
+ return;
273
+ }
274
+ const record = parsed as Record<string, unknown>;
275
+ if (typeof record.advance === "number") {
276
+ advance(record.advance);
277
+ return;
278
+ }
279
+ if (typeof record.kind !== "string") {
280
+ console.error(`neither a Msg ({"kind": ...}) nor {"advance": ms}: ${line}`);
281
+ return;
282
+ }
283
+ dispatch(revive(parsed));
284
+ }
285
+
286
+ if (script) {
287
+ for (const line of fs.readFileSync(script, "utf8").split("\n")) handleLine(line);
288
+ } else {
289
+ const rl = readline.createInterface({ input: process.stdin });
290
+ rl.on("line", handleLine);
291
+ }
@@ -0,0 +1,22 @@
1
+ // Module-resolution hook for the devhost: maps the "@native-sdk/core"
2
+ // specifier onto this package's own SDK module (sdk/core.ts, the file the
3
+ // package's exports map names), and "@native-sdk/core/<lib>" onto the SDK
4
+ // library module beside it (sdk/<lib>.ts), so a core under `node` imports
5
+ // exactly the modules the transpiler types it against. The hook
6
+ // short-circuits BEFORE default resolution on purpose: app trees carry a
7
+ // node_modules/@native-sdk/core copy as editor surface (same content,
8
+ // materialized by the CLI pre-publish, npm-installed after), but node's
9
+ // type stripping refuses .ts files under node_modules, so the devhost
10
+ // always imports the SDK checkout's module directly. Relative imports
11
+ // inside the core's src/ are real files node resolves itself.
12
+
13
+ export async function resolve(specifier, context, nextResolve) {
14
+ if (specifier === "@native-sdk/core") {
15
+ return { shortCircuit: true, url: new URL("../sdk/core.ts", import.meta.url).href };
16
+ }
17
+ if (specifier.startsWith("@native-sdk/core/")) {
18
+ const lib = specifier.slice("@native-sdk/core/".length);
19
+ return { shortCircuit: true, url: new URL(`../sdk/${lib}.ts`, import.meta.url).href };
20
+ }
21
+ return nextResolve(specifier, context);
22
+ }