@native-sdk/cli 0.8.4 → 0.9.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 (146) hide show
  1. package/build/app.zig +859 -62
  2. package/build.zig +543 -13
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +2 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +365 -2
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +149 -0
  19. package/packages/core/sdk/core.ts +529 -12
  20. package/packages/core/sdk/events.d.ts +36 -0
  21. package/packages/core/sdk/events.ts +57 -0
  22. package/packages/core/src/checker.ts +468 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1847 -16
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +82 -16
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/references/project-anatomy.md +1 -1
  38. package/skill-data/native-ui/SKILL.md +47 -0
  39. package/skill-data/ts-core/SKILL.md +80 -31
  40. package/skill-data/ts-services/SKILL.md +126 -0
  41. package/skill-data/ts-services/references/service-surface.md +603 -0
  42. package/skill-data/zig/SKILL.md +1 -1
  43. package/skills/native-sdk/SKILL.md +1 -1
  44. package/src/app_runner/no_migrations.zig +3 -0
  45. package/src/app_runner/no_services.zig +20 -0
  46. package/src/app_runner/root.zig +104 -4
  47. package/src/app_runner/ts_core_main.zig +249 -8
  48. package/src/automation/snapshot.zig +23 -5
  49. package/src/embed/app_exports.zig +11 -1
  50. package/src/embed/c_api.zig +48 -2
  51. package/src/embed/conversions.zig +4 -5
  52. package/src/embed/host.zig +109 -1
  53. package/src/embed/root.zig +7 -0
  54. package/src/embed/tests.zig +213 -0
  55. package/src/embed/types.zig +51 -0
  56. package/src/embed/ui_host.zig +119 -5
  57. package/src/platform/android/NativeSdkActivity.java +131 -0
  58. package/src/platform/android/android_host.c +182 -1
  59. package/src/platform/android/native_sdk_app.h +17 -0
  60. package/src/platform/ios/native_sdk_app.h +17 -0
  61. package/src/platform/ios/uikit_host.m +115 -0
  62. package/src/platform/linux/cef_host.cpp +1 -1
  63. package/src/platform/linux/gtk_host.c +89 -17
  64. package/src/platform/linux/gtk_host.h +3 -0
  65. package/src/platform/linux/root.zig +40 -18
  66. package/src/platform/macos/appkit_host.h +16 -3
  67. package/src/platform/macos/appkit_host.m +536 -36
  68. package/src/platform/macos/cef_host.mm +248 -36
  69. package/src/platform/macos/root.zig +244 -34
  70. package/src/platform/null_platform.zig +223 -19
  71. package/src/platform/null_platform_tests.zig +74 -0
  72. package/src/platform/root.zig +9 -0
  73. package/src/platform/types.zig +179 -15
  74. package/src/platform/windows/root.zig +187 -21
  75. package/src/platform/windows/webview2_host.cpp +293 -46
  76. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  77. package/src/primitives/app_manifest/root.zig +4 -0
  78. package/src/primitives/app_manifest/tests.zig +3 -0
  79. package/src/primitives/app_manifest/types.zig +32 -0
  80. package/src/primitives/app_manifest/validation.zig +19 -1
  81. package/src/root.zig +46 -0
  82. package/src/runtime/api.zig +11 -0
  83. package/src/runtime/automation_snapshot.zig +4 -0
  84. package/src/runtime/bridge_permission_tests.zig +23 -2
  85. package/src/runtime/bridge_responses.zig +3 -1
  86. package/src/runtime/canvas_frame.zig +18 -11
  87. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  88. package/src/runtime/clock.zig +13 -1
  89. package/src/runtime/core.zig +133 -1
  90. package/src/runtime/credentials_store.zig +115 -0
  91. package/src/runtime/effects.zig +2295 -113
  92. package/src/runtime/effects_credentials_tests.zig +332 -0
  93. package/src/runtime/effects_host_tests.zig +208 -1
  94. package/src/runtime/effects_relational_tests.zig +314 -0
  95. package/src/runtime/effects_store_tests.zig +211 -0
  96. package/src/runtime/flow.zig +14 -1
  97. package/src/runtime/gpu_surface_events.zig +6 -1
  98. package/src/runtime/native_command_event_tests.zig +42 -0
  99. package/src/runtime/persist_store.zig +666 -0
  100. package/src/runtime/record_store.zig +788 -0
  101. package/src/runtime/relational_store.zig +854 -0
  102. package/src/runtime/root.zig +49 -0
  103. package/src/runtime/runtime_event_tests.zig +70 -0
  104. package/src/runtime/service_host.zig +711 -0
  105. package/src/runtime/service_pool.zig +1314 -0
  106. package/src/runtime/session_blobs.zig +1 -1
  107. package/src/runtime/session_journal.zig +102 -0
  108. package/src/runtime/session_record.zig +177 -0
  109. package/src/runtime/session_replay.zig +109 -4
  110. package/src/runtime/shell_layout.zig +1 -0
  111. package/src/runtime/shell_layout_tests.zig +120 -0
  112. package/src/runtime/sqlite_engine.zig +474 -0
  113. package/src/runtime/state.zig +6 -0
  114. package/src/runtime/system_services.zig +49 -10
  115. package/src/runtime/tests.zig +4 -0
  116. package/src/runtime/ts_core_host.zig +788 -86
  117. package/src/runtime/ts_core_host_tests.zig +231 -11
  118. package/src/runtime/ts_ui_app.zig +499 -1
  119. package/src/runtime/ui_app.zig +142 -16
  120. package/src/runtime/ui_app_tests.zig +155 -5
  121. package/src/runtime/validation.zig +22 -0
  122. package/src/runtime/view.zig +9 -0
  123. package/src/runtime/window_storage.zig +1 -0
  124. package/src/runtime/window_view_runtime.zig +2 -3
  125. package/src/runtime/window_views.zig +19 -0
  126. package/src/tooling/android.zig +21 -16
  127. package/src/tooling/db.zig +160 -0
  128. package/src/tooling/ios.zig +7 -1
  129. package/src/tooling/manifest.zig +214 -2
  130. package/src/tooling/package.zig +116 -0
  131. package/src/tooling/raw_manifest.zig +30 -0
  132. package/src/tooling/root.zig +1 -0
  133. package/src/tooling/templates.zig +161 -4
  134. package/src/tooling/ts_core.zig +283 -17
  135. package/src/tooling/verbs.zig +7 -0
  136. package/third_party/sqlite/README.md +13 -0
  137. package/third_party/sqlite/sqlite3.c +269649 -0
  138. package/third_party/sqlite/sqlite3.h +14349 -0
  139. package/tools/corewire/core_abi.zig +14 -2
  140. package/tools/corewire/emit.zig +98 -29
  141. package/tools/corewire/emit_facade.zig +378 -23
  142. package/tools/corewire/emit_profile.zig +6 -2
  143. package/tools/corewire/emit_service.zig +924 -0
  144. package/tools/corewire/main.zig +122 -0
  145. package/tools/corewire/service_contract.zig +250 -0
  146. package/tools/corewire/sidecar.zig +26 -15
@@ -34,7 +34,8 @@
34
34
  // Cmd.cancel(key) drop the in-flight keyed effect — request,
35
35
  // readFile/writeFile/fetch/clipboardRead, or
36
36
  // delay — SILENTLY (no terminal arm dispatch).
37
- // Aimed at a live spawn or streaming fetch it
37
+ // Aimed at a live spawn, streaming fetch, or
38
+ // streaming service it
38
39
  // stays LOUD: the err arm runs with
39
40
  // "cancelled" — ending a stream is observable
40
41
  // Cmd.batch([a, b, ...]) several commands from one dispatch
@@ -75,6 +76,18 @@
75
76
  // Cmd.showNotification({ title, subtitle?, body? })
76
77
  // fire-and-forget desktop notification; the OS
77
78
  // remains authoritative over final delivery
79
+ // Cmd.openExternalUrl(url) open an allowed HTTP(S) URL in the system
80
+ // browser; denied/invalid URLs fail closed
81
+ // Cmd.revealPath(path) reveal a path in the system file manager;
82
+ // invalid/unavailable requests fail closed
83
+ // Cmd.credentials.set(key, secret, route)
84
+ // Cmd.credentials.get(key, route)
85
+ // Cmd.credentials.delete(key, route)
86
+ // routed access to Keychain / Secret Service /
87
+ // Credential Manager
88
+ // Cmd.formatLocalTime(ms, style, route)
89
+ // format an epoch timestamp through the host's
90
+ // current locale and time zone
78
91
  // Cmd.delay(key, ms, "fired") a keyed ONE-SHOT timer: dispatches the named
79
92
  // arm once after `ms`, with the fire time (ms)
80
93
  // as its single number payload; re-issuing a
@@ -229,6 +242,10 @@
229
242
  // "Open" consequence; also restores a
230
243
  // minimized window. An unknown label is a
231
244
  // no-op.
245
+ // Cmd.hideWindow(label) order a live window out while retaining
246
+ // its views; showWindow is the inverse.
247
+ // Cmd.setDockPresence(visible) show or remove the app from the macOS
248
+ // Dock/app switcher.
232
249
  // Cmd.quitApp() graceful terminate, the tray "Quit"
233
250
  // consequence: the host quits through the
234
251
  // SAME shutdown path a last-window close
@@ -262,22 +279,101 @@
262
279
  // import "@native-sdk/core", so tsc-in-the-editor and `native check` agree.
263
280
  /// <reference path="./bytes_text_methods.d.ts" />
264
281
 
265
- /// The text intrinsic: turn a string literal or template into bytes. The
266
- /// transpiler recognizes calls BY IDENTITY (this import, renames honored)
267
- /// and folds them at compile time — a literal argument becomes rodata, a
268
- /// template becomes frame-arena bytes via bufPrint — so no string ever
269
- /// exists at native runtime. Under node this body runs as-is, byte for
270
- /// byte the same result. Arguments must be literals or templates; dynamic
271
- /// text lives in the model as Uint8Array from the start.
282
+ /// The ASCII text intrinsic: turn a string literal or template whose code
283
+ /// units are all 0x00..0x7f into bytes. The transpiler recognizes calls BY
284
+ /// IDENTITY (this import, renames honored) and folds them at compile time —
285
+ /// a literal argument becomes rodata, a template becomes frame-arena bytes
286
+ /// via bufPrint — so no string ever exists at native runtime. Under node
287
+ /// this body runs as-is, byte for byte the same result. Use `utf8Bytes` for
288
+ /// user-visible text that may contain Unicode. Arguments must be literals
289
+ /// or templates; dynamic text lives in the model as Uint8Array from the
290
+ /// start.
272
291
  export function asciiBytes(s: string): Uint8Array {
292
+ for (let i = 0; i < s.length; i++) {
293
+ if (s.charCodeAt(i) > 0x7f) {
294
+ throw new RangeError("asciiBytes accepts ASCII only; use utf8Bytes for Unicode text");
295
+ }
296
+ }
273
297
  const out = new Uint8Array(s.length);
274
298
  for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i);
275
299
  return out;
276
300
  }
277
301
 
302
+ /// The UTF-8 text intrinsic. Like `asciiBytes`, literals become rodata and
303
+ /// templates become frame-arena bytes in a native core; under node this
304
+ /// implementation performs the same encoding directly. Invalid lone UTF-16
305
+ /// surrogates encode as U+FFFD, matching the platform TextEncoder contract.
306
+ export function utf8Bytes(s: string): Uint8Array {
307
+ let byteLength = 0;
308
+ for (let i = 0; i < s.length; i++) {
309
+ const code = s.charCodeAt(i);
310
+ if (code <= 0x7f) {
311
+ byteLength += 1;
312
+ } else if (code <= 0x7ff) {
313
+ byteLength += 2;
314
+ } else if (code >= 0xd800 && code <= 0xdbff && i + 1 < s.length) {
315
+ const next = s.charCodeAt(i + 1);
316
+ if (next >= 0xdc00 && next <= 0xdfff) {
317
+ byteLength += 4;
318
+ i += 1;
319
+ } else {
320
+ byteLength += 3;
321
+ }
322
+ } else {
323
+ byteLength += 3;
324
+ }
325
+ }
326
+
327
+ const out = new Uint8Array(byteLength);
328
+ let at = 0;
329
+ for (let i = 0; i < s.length; i++) {
330
+ let code = s.charCodeAt(i);
331
+ if (code >= 0xd800 && code <= 0xdbff && i + 1 < s.length) {
332
+ const next = s.charCodeAt(i + 1);
333
+ if (next >= 0xdc00 && next <= 0xdfff) {
334
+ code = 0x10000 + ((code - 0xd800) << 10) + (next - 0xdc00);
335
+ i += 1;
336
+ } else {
337
+ code = 0xfffd;
338
+ }
339
+ } else if (code >= 0xd800 && code <= 0xdfff) {
340
+ code = 0xfffd;
341
+ }
342
+
343
+ if (code <= 0x7f) {
344
+ out[at] = code;
345
+ at += 1;
346
+ } else if (code <= 0x7ff) {
347
+ out[at] = 0xc0 | (code >> 6);
348
+ out[at + 1] = 0x80 | (code & 0x3f);
349
+ at += 2;
350
+ } else if (code <= 0xffff) {
351
+ out[at] = 0xe0 | (code >> 12);
352
+ out[at + 1] = 0x80 | ((code >> 6) & 0x3f);
353
+ out[at + 2] = 0x80 | (code & 0x3f);
354
+ at += 3;
355
+ } else {
356
+ out[at] = 0xf0 | (code >> 18);
357
+ out[at + 1] = 0x80 | ((code >> 12) & 0x3f);
358
+ out[at + 2] = 0x80 | ((code >> 6) & 0x3f);
359
+ out[at + 3] = 0x80 | (code & 0x3f);
360
+ at += 4;
361
+ }
362
+ }
363
+ return out;
364
+ }
365
+
278
366
  /// Every app Msg is a discriminated union on a string `kind` tag.
279
367
  export type Msgish = { readonly kind: string };
280
368
 
369
+ /** Cooperative cancellation capability supplied by generated service hosts. */
370
+ export interface ServiceCancellation {
371
+ /** True after Cmd.cancel or the operation deadline requests cancellation. */
372
+ readonly cancelled: () => boolean;
373
+ /** Throw a boundary-tagged cancellation error when cancellation was requested. */
374
+ readonly throwIfCancelled: () => void;
375
+ }
376
+
281
377
  /// The Msg arms `Cmd.now` may target: arms whose payload is exactly one
282
378
  /// number-typed field (the runtime dispatches the arm with the timestamp in
283
379
  /// that field). Anything else is unrepresentable — the runtime has only a
@@ -305,6 +401,38 @@ export type BytesKind<M extends Msgish> = M extends Msgish
305
401
  }[Exclude<keyof M, "kind">]
306
402
  : never;
307
403
 
404
+ /// The Msg arms a generated typed service client may target: exactly one
405
+ /// payload field whose type is the operation's declared result shape.
406
+ export type ServiceKind<M extends Msgish, P> = M extends Msgish
407
+ ? {
408
+ [K in Exclude<keyof M, "kind">]-?: M[K] extends P
409
+ ? P extends M[K]
410
+ ? [Exclude<keyof M, "kind">] extends [K]
411
+ ? M["kind"]
412
+ : never
413
+ : never
414
+ : never;
415
+ }[Exclude<keyof M, "kind">]
416
+ : never;
417
+
418
+ /// Routing for a generated typed service call. Success carries the
419
+ /// operation's declared record/value shape; failures remain UTF-8 bytes so
420
+ /// transport, timeout, cancellation, and service `{ kind, message }` errors
421
+ /// share one stable error channel.
422
+ export interface ServiceRoute<M extends Msgish, P> {
423
+ readonly key?: string;
424
+ readonly ok: ServiceKind<M, P>;
425
+ readonly err: BytesKind<M>;
426
+ }
427
+
428
+ /// A streaming service opens an external-source channel before issuing the
429
+ /// request. Interim chunks arrive as canonical bytes on `event`; `ok` is the
430
+ /// one typed terminal and `err` is the loud cancellation/timeout/error arm.
431
+ export interface ServiceStreamRoute<M extends Msgish, P> extends ServiceRoute<M, P> {
432
+ readonly channelKey: number;
433
+ readonly event: ChannelEventKind<M>;
434
+ }
435
+
308
436
  /// The Msg arms a payload-less routed result may target: arms with no
309
437
  /// payload fields at all (`Cmd.writeFile`'s ok route — a successful write
310
438
  /// has nothing to report beyond success).
@@ -722,6 +850,62 @@ export interface WriteRoute<M extends Msgish> {
722
850
  readonly err: BytesKind<M>;
723
851
  }
724
852
 
853
+ /// Pagination controls for `Cmd.store.scan`. `limit` defaults to 100 and is
854
+ /// bounded at 256. `after` is the opaque key cursor returned by the previous
855
+ /// page; omit it for the first page.
856
+ export interface StoreScanOptions {
857
+ readonly limit?: number;
858
+ readonly after?: string | Uint8Array;
859
+ }
860
+
861
+ /// Dynamic UTF-8 SQLite TEXT. App-core text is byte-honest, so generated
862
+ /// TEXT parameters wrap model bytes with this marker; literal strings remain
863
+ /// accepted by the raw escape hatch.
864
+ export interface DbText {
865
+ readonly __dbText: true;
866
+ readonly bytes: ReadonlyArray<number>;
867
+ }
868
+
869
+ export function dbText(bytes: Uint8Array): DbText {
870
+ const out: number[] = [];
871
+ for (let i = 0; i < bytes.length; i++) out.push(bytes[i]!);
872
+ return { __dbText: true, bytes: out };
873
+ }
874
+
875
+ /// Runtime SQL parameter values. Numbers preserve integer storage when they
876
+ /// are finite integral values; booleans bind as SQLite INTEGER 0/1.
877
+ export type DbValue = null | number | string | Uint8Array | boolean | DbText;
878
+ export type DbStatement = readonly [sql: string, params: ReadonlyArray<DbValue>];
879
+
880
+ /// A query dispatches every encoded row page through `page`, then exactly one
881
+ /// payload-less `done`. Any closed DbOutcome instead dispatches `err` as its
882
+ /// UTF-8 name (`constraint`, `busy`, `io_failed`, `corrupt`, `misuse`,
883
+ /// `rejected`, or `cancelled`).
884
+ export interface DbRowsRoute<M extends Msgish> {
885
+ readonly key?: string;
886
+ readonly page: BytesKind<M>;
887
+ readonly done: EmptyKind<M>;
888
+ readonly err: BytesKind<M>;
889
+ }
890
+
891
+ /// A generated declared-query route. The row parameter is intentionally
892
+ /// phantom on the wire: generated `decode<Name>Page` turns each bounded page
893
+ /// into this exact row shape without reflection.
894
+ export interface TypedRowsRoute<Row, M extends Msgish> extends DbRowsRoute<M> {
895
+ readonly __row?: Row;
896
+ }
897
+
898
+ /// One generated, already-validated write statement. Only generated query
899
+ /// constructors create the brand; `Cmd.qTx` therefore cannot receive a
900
+ /// raw SQL string by accident.
901
+ export interface TypedDbStatement {
902
+ readonly sql: string;
903
+ readonly params: ReadonlyArray<DbValue>;
904
+ readonly __typedDbStatement: true;
905
+ }
906
+
907
+ // @native-sqlite-generated-types
908
+
725
909
  /// `Cmd.fetch` routing: the ok arm carries `{ status, body }` (one number
726
910
  /// field, one bytes field — matched by type); the err arm the reason bytes.
727
911
  export interface FetchRoute<M extends Msgish> {
@@ -863,6 +1047,11 @@ export interface NotificationSpec {
863
1047
  readonly body?: Uint8Array;
864
1048
  }
865
1049
 
1050
+ /// The three bounded host-local timestamp presentations. Unlike the markup
1051
+ /// `date`/`time`/`datetime` functions (fixed UTC), this is intentionally an
1052
+ /// effect because the user's locale and time zone are ambient host state.
1053
+ export type LocalTimeStyle = "date" | "time" | "datetime";
1054
+
866
1055
  /// An inert command value, parameterized by the app's Msg union so the
867
1056
  /// factories can validate message targets. Opaque to app code: build with
868
1057
  /// the `Cmd.*` factories, return from `update`/`initialModel`, never inspect
@@ -879,6 +1068,19 @@ export type Cmd<M extends Msgish> =
879
1068
  readonly key: string;
880
1069
  readonly okKind: string;
881
1070
  readonly errKind: string;
1071
+ readonly typedService: boolean;
1072
+ readonly payload: Uint8Array;
1073
+ }
1074
+ | {
1075
+ readonly op: "service_stream_request";
1076
+ readonly name: string;
1077
+ readonly key: string;
1078
+ readonly okKind: string;
1079
+ readonly errKind: string;
1080
+ readonly typedService: true;
1081
+ readonly channelKey: number;
1082
+ readonly eventKind: string;
1083
+ readonly maxPending: number;
882
1084
  readonly payload: Uint8Array;
883
1085
  }
884
1086
  | { readonly op: "cancel"; readonly key: string }
@@ -897,6 +1099,53 @@ export type Cmd<M extends Msgish> =
897
1099
  readonly path: Uint8Array;
898
1100
  readonly bytes: Uint8Array;
899
1101
  }
1102
+ | {
1103
+ readonly op: "store_set";
1104
+ readonly key: string;
1105
+ readonly okKind: string;
1106
+ readonly errKind: string;
1107
+ readonly storeKey: string;
1108
+ readonly bytes: Uint8Array;
1109
+ }
1110
+ | {
1111
+ readonly op: "store_get" | "store_delete";
1112
+ readonly key: string;
1113
+ readonly okKind: string;
1114
+ readonly errKind: string;
1115
+ readonly storeKey: string;
1116
+ }
1117
+ | {
1118
+ readonly op: "store_scan";
1119
+ readonly key: string;
1120
+ readonly okKind: string;
1121
+ readonly errKind: string;
1122
+ readonly prefix: string;
1123
+ readonly limit: number;
1124
+ readonly after: string | Uint8Array;
1125
+ }
1126
+ | {
1127
+ readonly op: "store_set_many";
1128
+ readonly key: string;
1129
+ readonly okKind: string;
1130
+ readonly errKind: string;
1131
+ readonly entries: ReadonlyArray<readonly [string, Uint8Array]>;
1132
+ }
1133
+ | {
1134
+ readonly op: "db_query";
1135
+ readonly key: string;
1136
+ readonly pageKind: string;
1137
+ readonly doneKind: string;
1138
+ readonly errKind: string;
1139
+ readonly sql: string;
1140
+ readonly params: ReadonlyArray<DbValue>;
1141
+ }
1142
+ | {
1143
+ readonly op: "db_exec";
1144
+ readonly key: string;
1145
+ readonly okKind: string;
1146
+ readonly errKind: string;
1147
+ readonly statements: ReadonlyArray<DbStatement>;
1148
+ }
900
1149
  | {
901
1150
  readonly op: "fetch";
902
1151
  readonly key: string;
@@ -977,6 +1226,8 @@ export type Cmd<M extends Msgish> =
977
1226
  readonly value: number;
978
1227
  }
979
1228
  | { readonly op: "window_show"; readonly label: string }
1229
+ | { readonly op: "window_hide"; readonly label: string }
1230
+ | { readonly op: "dock_presence"; readonly visible: boolean }
980
1231
  | { readonly op: "quit_app" }
981
1232
  | {
982
1233
  readonly op: "image_load";
@@ -989,7 +1240,7 @@ export type Cmd<M extends Msgish> =
989
1240
  }
990
1241
  | { readonly op: "image_cancel"; readonly id: number }
991
1242
  | { readonly op: "image_unregister"; readonly id: number }
992
- | { readonly op: "channel_open"; readonly key: number; readonly eventKind: string }
1243
+ | { readonly op: "channel_open"; readonly key: number; readonly eventKind: string; readonly maxPending: number }
993
1244
  | { readonly op: "channel_close"; readonly key: number }
994
1245
  | {
995
1246
  readonly op: "audio_capture_start";
@@ -1050,6 +1301,74 @@ export function hostRecordBytes(payload: HostRecord): Uint8Array {
1050
1301
  return out;
1051
1302
  }
1052
1303
 
1304
+ function serviceU32(value: number): Uint8Array {
1305
+ const out = new Uint8Array(4);
1306
+ out[0] = value & 255;
1307
+ out[1] = (value >>> 8) & 255;
1308
+ out[2] = (value >>> 16) & 255;
1309
+ out[3] = (value >>> 24) & 255;
1310
+ return out;
1311
+ }
1312
+
1313
+ /// Canonical service-value codec primitives. Generated clients compose these
1314
+ /// in declaration order; the service host and Zig result dispatcher consume
1315
+ /// the same format as shim_rt (LE scalars, length-prefixed bytes/slices,
1316
+ /// declaration-order enum/union tags).
1317
+ export function serviceConcat(parts: readonly Uint8Array[]): Uint8Array {
1318
+ let length = 0;
1319
+ for (const part of parts) length += part.length;
1320
+ const out = new Uint8Array(length);
1321
+ let at = 0;
1322
+ for (const part of parts) {
1323
+ out.set(part, at);
1324
+ at += part.length;
1325
+ }
1326
+ return out;
1327
+ }
1328
+
1329
+ export function serviceBoolBytes(value: boolean): Uint8Array {
1330
+ return new Uint8Array([value ? 1 : 0]);
1331
+ }
1332
+
1333
+ export function serviceF64Bytes(value: number): Uint8Array {
1334
+ const out = new Uint8Array(8);
1335
+ const view = new DataView(out.buffer);
1336
+ view.setFloat64(0, Number.isNaN(value) ? Number.NaN : value, true);
1337
+ return out;
1338
+ }
1339
+
1340
+ export function serviceI64Bytes(value: number): Uint8Array {
1341
+ const out = new Uint8Array(8);
1342
+ const view = new DataView(out.buffer);
1343
+ const base = 4294967296;
1344
+ let low = value % base;
1345
+ if (low < 0) low += base;
1346
+ const high = Math.floor(value / base);
1347
+ view.setUint32(0, low, true);
1348
+ view.setInt32(4, high, true);
1349
+ return out;
1350
+ }
1351
+
1352
+ export function serviceBytes(value: Uint8Array): Uint8Array {
1353
+ return serviceConcat([serviceU32(value.length), value]);
1354
+ }
1355
+
1356
+ export function serviceEnumBytes(index: number): Uint8Array {
1357
+ return serviceU32(index);
1358
+ }
1359
+
1360
+ export function serviceUnionBytes(index: number): Uint8Array {
1361
+ return new Uint8Array([index]);
1362
+ }
1363
+
1364
+ export function serviceOptionalBytes(value: Uint8Array | null): Uint8Array {
1365
+ return value === null ? new Uint8Array([0]) : serviceConcat([new Uint8Array([1]), value]);
1366
+ }
1367
+
1368
+ export function serviceSliceBytes(values: readonly Uint8Array[]): Uint8Array {
1369
+ return serviceConcat([serviceU32(values.length), ...values]);
1370
+ }
1371
+
1053
1372
  function lowerHostPayload(payload: Uint8Array | HostRecord): Uint8Array {
1054
1373
  return payload instanceof Uint8Array ? payload : hostRecordBytes(payload);
1055
1374
  }
@@ -1113,7 +1432,9 @@ export const Cmd = {
1113
1432
  /// No effects. `return model` is sugar for `return [model, Cmd.none]`.
1114
1433
  none: { op: "none" } as Cmd<never>,
1115
1434
 
1116
- /// Ask the host to persist the committed model.
1435
+ /// Snapshot the just-committed Model through the engine-owned store.
1436
+ /// Requires app.zon's `persist` capability and restore-route config;
1437
+ /// the host owns coalescing, atomic placement, and journal/replay.
1117
1438
  persist(): Cmd<never> {
1118
1439
  return { op: "persist" };
1119
1440
  },
@@ -1143,13 +1464,54 @@ export const Cmd = {
1143
1464
  key: route.key ?? "",
1144
1465
  okKind: route.ok,
1145
1466
  errKind: route.err,
1467
+ typedService: false,
1146
1468
  payload: lowerHostPayload(payload),
1147
1469
  };
1148
1470
  },
1149
1471
 
1472
+ /// Generated typed service clients use this constructor after encoding the
1473
+ /// request shape. It lowers to the ordinary request wire record with the
1474
+ /// typed-result bit set so the host decodes the success payload.
1475
+ serviceRequest<M extends Msgish, P>(
1476
+ name: string,
1477
+ payload: Uint8Array,
1478
+ route: ServiceRoute<M, P>,
1479
+ ): Cmd<M> {
1480
+ return {
1481
+ op: "request",
1482
+ name,
1483
+ key: route.key ?? "",
1484
+ okKind: route.ok,
1485
+ errKind: route.err,
1486
+ typedService: true,
1487
+ payload,
1488
+ };
1489
+ },
1490
+
1491
+ serviceStreamRequest<M extends Msgish, P>(
1492
+ name: string,
1493
+ channelKey: number,
1494
+ payload: Uint8Array,
1495
+ route: ServiceStreamRoute<M, P>,
1496
+ maxPending: number,
1497
+ ): Cmd<M> {
1498
+ return {
1499
+ op: "service_stream_request",
1500
+ name,
1501
+ key: route.key ?? "",
1502
+ okKind: route.ok,
1503
+ errKind: route.err,
1504
+ typedService: true,
1505
+ channelKey,
1506
+ eventKind: route.event,
1507
+ maxPending,
1508
+ payload: serviceConcat([serviceF64Bytes(channelKey), payload]),
1509
+ };
1510
+ },
1511
+
1150
1512
  /// Drop the in-flight keyed effect — request, named engine op, or delay —
1151
1513
  /// with this key, if any, SILENTLY (neither routing arm is dispatched for
1152
- /// it). Live spawn and streaming-fetch operations are the exceptions:
1514
+ /// it). Live spawn, streaming-fetch, and streaming-service operations are the exceptions:
1153
1515
  /// cancel ends the stream and its err arm runs with "cancelled".
1154
1516
  cancel(key: string): Cmd<never> {
1155
1517
  return { op: "cancel", key };
@@ -1169,6 +1531,103 @@ export const Cmd = {
1169
1531
  return { op: "write_file", key: route.key ?? "", okKind: route.ok, errKind: route.err, path, bytes };
1170
1532
  },
1171
1533
 
1534
+ /// Capability-gated, engine-owned per-record storage. Keys are UTF-8 text
1535
+ /// up to 512 bytes; values are bytes up to 1 MiB. Results remain effects:
1536
+ /// they arrive through the supplied Msg routes after update commits.
1537
+ store: {
1538
+ set<M extends Msgish>(storeKey: string, bytes: Uint8Array, route: WriteRoute<M>): Cmd<M> {
1539
+ return { op: "store_set", key: route.key ?? "", okKind: route.ok, errKind: route.err, storeKey, bytes };
1540
+ },
1541
+
1542
+ /// The ok payload is `[1][value...]` for a hit or `[0]` for a miss, so
1543
+ /// an empty stored value remains distinguishable from absence.
1544
+ get<M extends Msgish>(storeKey: string, route: RequestRoute<M>): Cmd<M> {
1545
+ return { op: "store_get", key: route.key ?? "", okKind: route.ok, errKind: route.err, storeKey };
1546
+ },
1547
+
1548
+ /// Deleting an absent key succeeds.
1549
+ delete<M extends Msgish>(storeKey: string, route: WriteRoute<M>): Cmd<M> {
1550
+ return { op: "store_delete", key: route.key ?? "", okKind: route.ok, errKind: route.err, storeKey };
1551
+ },
1552
+
1553
+ /// The ok payload is a length-prefixed page of `(key,value)` pairs and
1554
+ /// a next-key cursor. Pages end at record boundaries; data is never cut.
1555
+ scan<M extends Msgish>(prefix: string, options: StoreScanOptions, route: RequestRoute<M>): Cmd<M> {
1556
+ return {
1557
+ op: "store_scan",
1558
+ key: route.key ?? "",
1559
+ okKind: route.ok,
1560
+ errKind: route.err,
1561
+ prefix,
1562
+ limit: options.limit ?? 0,
1563
+ after: options.after ?? "",
1564
+ };
1565
+ },
1566
+
1567
+ /// Atomically upsert all entries (at most 64 entries / 8 MiB encoded).
1568
+ setMany<M extends Msgish>(entries: ReadonlyArray<readonly [string, Uint8Array]>, route: WriteRoute<M>): Cmd<M> {
1569
+ return { op: "store_set_many", key: route.key ?? "", okKind: route.ok, errKind: route.err, entries };
1570
+ },
1571
+ },
1572
+
1573
+ /// App-scoped OS credential storage. The app manifest id supplies the
1574
+ /// service namespace, so authored code chooses only a UTF-8 key. Declare
1575
+ /// both the `credentials` capability and permission in app.zon.
1576
+ credentials: {
1577
+ set<M extends Msgish>(credentialKey: string, secret: Uint8Array, route: WriteRoute<M>): Cmd<M> {
1578
+ return {
1579
+ op: "request",
1580
+ name: "core.credentials.set",
1581
+ key: route.key ?? "",
1582
+ okKind: route.ok,
1583
+ errKind: route.err,
1584
+ typedService: false,
1585
+ payload: hostRecordBytes({ key: utf8Bytes(credentialKey), secret }),
1586
+ };
1587
+ },
1588
+
1589
+ /// The ok arm carries secret bytes; a miss routes err with `miss`.
1590
+ get<M extends Msgish>(credentialKey: string, route: RequestRoute<M>): Cmd<M> {
1591
+ return Cmd.request("core.credentials.get", { key: utf8Bytes(credentialKey) }, route);
1592
+ },
1593
+
1594
+ /// Deleting an absent key succeeds.
1595
+ delete<M extends Msgish>(credentialKey: string, route: WriteRoute<M>): Cmd<M> {
1596
+ return {
1597
+ op: "request",
1598
+ name: "core.credentials.delete",
1599
+ key: route.key ?? "",
1600
+ okKind: route.ok,
1601
+ errKind: route.err,
1602
+ typedService: false,
1603
+ payload: hostRecordBytes({ key: utf8Bytes(credentialKey) }),
1604
+ };
1605
+ },
1606
+ },
1607
+
1608
+ /// Capability-gated relational SQLite. Reads remain effects: pages and the
1609
+ /// terminal arrive as Msg values after the issuing model has committed.
1610
+ /// Every exec statement in one command commits atomically or all roll back.
1611
+ db: {
1612
+ query<M extends Msgish>(sql: string, params: ReadonlyArray<DbValue>, route: DbRowsRoute<M>): Cmd<M> {
1613
+ return {
1614
+ op: "db_query",
1615
+ key: route.key ?? "",
1616
+ pageKind: route.page,
1617
+ doneKind: route.done,
1618
+ errKind: route.err,
1619
+ sql,
1620
+ params,
1621
+ };
1622
+ },
1623
+
1624
+ exec<M extends Msgish>(statements: ReadonlyArray<DbStatement>, route: WriteRoute<M>): Cmd<M> {
1625
+ return { op: "db_exec", key: route.key ?? "", okKind: route.ok, errKind: route.err, statements };
1626
+ },
1627
+ },
1628
+
1629
+ // @native-sqlite-generated-cmds
1630
+
1172
1631
  fetch: fetchCmd,
1173
1632
 
1174
1633
  /// Put bytes on the system clipboard, fire-and-forget (an over-bound or
@@ -1197,6 +1656,27 @@ export const Cmd = {
1197
1656
  };
1198
1657
  },
1199
1658
 
1659
+ /// Open an HTTP(S) URL in the user's system browser. Fire-and-forget: the
1660
+ /// runtime validates the URL and enforces `external_links`; invalid,
1661
+ /// denied, or unavailable requests fail closed.
1662
+ openExternalUrl(url: Uint8Array): Cmd<never> {
1663
+ return { op: "host_bytes", name: "native-sdk.os.openUrl", payload: url };
1664
+ },
1665
+
1666
+ /// Reveal a path in the system file manager (Finder, Files, or Explorer).
1667
+ /// Fire-and-forget; invalid or unavailable requests fail closed.
1668
+ revealPath(path: Uint8Array): Cmd<never> {
1669
+ return { op: "host_bytes", name: "native-sdk.os.revealPath", payload: path };
1670
+ },
1671
+
1672
+ /// Format an epoch timestamp (milliseconds, the same unit as `Cmd.now`) in
1673
+ /// the host's current locale and local time zone. The localized UTF-8 text
1674
+ /// arrives on ok; invalid timestamps or unavailable services route err.
1675
+ formatLocalTime<M extends Msgish>(timestampMs: number, style: LocalTimeStyle, route: RequestRoute<M>): Cmd<M> {
1676
+ const styleCode = style === "date" ? 0 : style === "time" ? 1 : 2;
1677
+ return Cmd.request("native-sdk.time.formatLocal", { style: styleCode, timestampMs }, route);
1678
+ },
1679
+
1200
1680
  /// A keyed one-shot delay: dispatch the named Msg arm once, `ms` from now,
1201
1681
  /// with the fire time (milliseconds) as its single number payload field.
1202
1682
  /// Re-issuing a live key re-arms it from now (the debounce discipline);
@@ -1364,6 +1844,31 @@ export const Cmd = {
1364
1844
  return { op: "window_show", label };
1365
1845
  },
1366
1846
 
1847
+ /// Hide a live window without closing it. Its views and native identity
1848
+ /// remain intact; `showWindow` brings it back. Unknown labels no-op.
1849
+ hideWindow(label: string): Cmd<never> {
1850
+ return { op: "window_hide", label };
1851
+ },
1852
+
1853
+ /// Control whether the app appears in the macOS Dock and app switcher.
1854
+ /// Unsupported hosts ignore the command.
1855
+ setDockPresence(visible: boolean): Cmd<never> {
1856
+ return { op: "dock_presence", visible };
1857
+ },
1858
+
1859
+ /// Query the installed bundle's launch-at-login registration. The ok
1860
+ /// route receives UTF-8 bytes naming `enabled`, `disabled`,
1861
+ /// `requires_approval`, or `not_found`; the err route receives a reason.
1862
+ launchAtLoginStatus<M extends Msgish>(route: RequestRoute<M>): Cmd<M> {
1863
+ return Cmd.request("native-sdk.launch-at-login.status", new Uint8Array(0), route);
1864
+ },
1865
+
1866
+ /// Register or unregister the installed bundle for launch at login. The
1867
+ /// ok and err payloads follow `launchAtLoginStatus`.
1868
+ setLaunchAtLogin<M extends Msgish>(enabled: boolean, route: RequestRoute<M>): Cmd<M> {
1869
+ return Cmd.request("native-sdk.launch-at-login.set", new Uint8Array([enabled ? 1 : 0]), route);
1870
+ },
1871
+
1367
1872
  /// Quit the app for real — the graceful terminate, and the tray "Quit"
1368
1873
  /// consequence. The host quits through the SAME shutdown path a
1369
1874
  /// last-window close takes, so the stop hook runs exactly once and a
@@ -1455,7 +1960,7 @@ export const Cmd = {
1455
1960
  /// one that consults the handle's live() before launching never
1456
1961
  /// starts, keeping replay fully offline.
1457
1962
  channelOpen<M extends Msgish>(key: number, route: ChannelRoute<M>): Cmd<M> {
1458
- return { op: "channel_open", key, eventKind: route.event };
1963
+ return { op: "channel_open", key, eventKind: route.event, maxPending: 64 };
1459
1964
  },
1460
1965
 
1461
1966
  /// Close the open channel under `key`: posts stop landing, the
@@ -1558,6 +2063,16 @@ export const Cmd = {
1558
2063
  export type Sub<M extends Msgish> =
1559
2064
  | { readonly op: "none" }
1560
2065
  | { readonly op: "timer"; readonly key: string; readonly everyMs: number; readonly msgKind: string }
2066
+ | {
2067
+ readonly op: "db_live";
2068
+ readonly key: string;
2069
+ readonly pageKind: string;
2070
+ readonly doneKind: string;
2071
+ readonly errKind: string;
2072
+ readonly sql: string;
2073
+ readonly params: ReadonlyArray<DbValue>;
2074
+ readonly tables: readonly string[];
2075
+ }
1561
2076
  | { readonly op: "batch"; readonly subs: readonly Sub<M>[] };
1562
2077
 
1563
2078
  export const Sub = {
@@ -1571,6 +2086,8 @@ export const Sub = {
1571
2086
  return { op: "timer", key, everyMs, msgKind };
1572
2087
  },
1573
2088
 
2089
+ // @native-sqlite-generated-subs
2090
+
1574
2091
  /// Several subscriptions at once.
1575
2092
  batch<M extends Msgish>(subs: readonly Sub<M>[]): Sub<M> {
1576
2093
  return { op: "batch", subs };