@native-sdk/cli 0.8.3 → 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 (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  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 +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -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 +1849 -18
  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 +84 -18
  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/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -34,10 +34,10 @@
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 it stays LOUD: the
38
- // child dies and the err arm runs with
39
- // "cancelled" — killing a process is an
40
- // observable event
37
+ // Aimed at a live spawn, streaming fetch, or
38
+ // streaming service it
39
+ // stays LOUD: the err arm runs with
40
+ // "cancelled" — ending a stream is observable
41
41
  // Cmd.batch([a, b, ...]) several commands from one dispatch
42
42
  //
43
43
  // The named engine ops (each maps onto the host's effect engine directly;
@@ -61,6 +61,13 @@
61
61
  // the reason bytes ("connect_failed",
62
62
  // "tls_failed", "protocol_failed", "timed_out",
63
63
  // "rejected", "truncated")
64
+ // Cmd.fetch({ ..., maxLineBytes? }, { key?, line, ok, err })
65
+ // streaming HTTP(S) exchange; each complete
66
+ // response line dispatches the one-bytes-field
67
+ // `line` arm, then exactly one terminal follows:
68
+ // `ok` with the HTTP status as its one number
69
+ // field, or `err` with the transport reason
70
+ // (or "truncated" if any line was cut/dropped)
64
71
  // Cmd.clipboardWrite(bytes) fire-and-forget clipboard write
65
72
  // Cmd.clipboardRead({ key?, ok, err })
66
73
  // clipboard read; ok arm carries the text bytes,
@@ -69,6 +76,18 @@
69
76
  // Cmd.showNotification({ title, subtitle?, body? })
70
77
  // fire-and-forget desktop notification; the OS
71
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
72
91
  // Cmd.delay(key, ms, "fired") a keyed ONE-SHOT timer: dispatches the named
73
92
  // arm once after `ms`, with the fire time (ms)
74
93
  // as its single number payload; re-issuing a
@@ -223,6 +242,10 @@
223
242
  // "Open" consequence; also restores a
224
243
  // minimized window. An unknown label is a
225
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.
226
249
  // Cmd.quitApp() graceful terminate, the tray "Quit"
227
250
  // consequence: the host quits through the
228
251
  // SAME shutdown path a last-window close
@@ -232,10 +255,10 @@
232
255
  // The keyed-effect discipline is ONE rule: a keyed effect REPLACES its live
233
256
  // predecessor (the superseded effect's result is dropped — no message), and
234
257
  // Cmd.cancel drops it silently. That holds for request, readFile, writeFile,
235
- // fetch, clipboardRead, and delay alike. The ONE exception is a live spawn
236
- // key: a duplicate REJECTS the new spawn (err arm "rejected") — a running
237
- // subprocess is never killed implicitly; cancel it first. And spawn's cancel
238
- // stays loud (err arm "cancelled"): killing a process is an observable event.
258
+ // buffered fetch, clipboardRead, and delay alike. Live spawn and streaming-
259
+ // fetch keys are the exceptions: a duplicate REJECTS the new stream (err arm
260
+ // "rejected") so results from two sources are never spliced together. Cancel
261
+ // either stream first; its err arm runs with "cancelled".
239
262
  //
240
263
  // `Sub` is the recurring-effects surface: an app may export
241
264
  // `subscriptions(model): Sub<Msg>` returning declarative descriptors the
@@ -256,22 +279,101 @@
256
279
  // import "@native-sdk/core", so tsc-in-the-editor and `native check` agree.
257
280
  /// <reference path="./bytes_text_methods.d.ts" />
258
281
 
259
- /// The text intrinsic: turn a string literal or template into bytes. The
260
- /// transpiler recognizes calls BY IDENTITY (this import, renames honored)
261
- /// and folds them at compile time — a literal argument becomes rodata, a
262
- /// template becomes frame-arena bytes via bufPrint — so no string ever
263
- /// exists at native runtime. Under node this body runs as-is, byte for
264
- /// byte the same result. Arguments must be literals or templates; dynamic
265
- /// 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.
266
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
+ }
267
297
  const out = new Uint8Array(s.length);
268
298
  for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i);
269
299
  return out;
270
300
  }
271
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
+
272
366
  /// Every app Msg is a discriminated union on a string `kind` tag.
273
367
  export type Msgish = { readonly kind: string };
274
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
+
275
377
  /// The Msg arms `Cmd.now` may target: arms whose payload is exactly one
276
378
  /// number-typed field (the runtime dispatches the arm with the timestamp in
277
379
  /// that field). Anything else is unrepresentable — the runtime has only a
@@ -299,6 +401,38 @@ export type BytesKind<M extends Msgish> = M extends Msgish
299
401
  }[Exclude<keyof M, "kind">]
300
402
  : never;
301
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
+
302
436
  /// The Msg arms a payload-less routed result may target: arms with no
303
437
  /// payload fields at all (`Cmd.writeFile`'s ok route — a successful write
304
438
  /// has nothing to report beyond success).
@@ -559,6 +693,64 @@ export interface ChannelRoute<M extends Msgish> {
559
693
  readonly event: ChannelEventKind<M>;
560
694
  }
561
695
 
696
+ /// The native capture source. "microphone" captures the selected/default
697
+ /// input device; "system" captures the process-independent desktop output
698
+ /// mix (the OS may present a screen/audio consent prompt).
699
+ export type AudioCaptureSource = "microphone" | "system";
700
+
701
+ /// Capture stream states. A successful stream begins with "started", emits
702
+ /// zero or more "data" chunks, and ends with "stopped" after
703
+ /// `Cmd.audioCaptureStop`. "failed" reports a source/permission failure;
704
+ /// stop that stream to retire it. "rejected" means the runtime refused the
705
+ /// start before a native source was opened.
706
+ export type AudioCaptureState = "started" | "data" | "failed" | "stopped" | "rejected";
707
+
708
+ export type AudioCaptureSampleRate = 16000 | 24000 | 48000;
709
+ export type AudioCaptureChannels = 1 | 2;
710
+
711
+ /// Requested canonical output format. Native inputs are converted to
712
+ /// interleaved signed 16-bit little-endian PCM before delivery.
713
+ export interface AudioCaptureSpec {
714
+ readonly source: AudioCaptureSource;
715
+ readonly sampleRate?: AudioCaptureSampleRate;
716
+ readonly channels?: AudioCaptureChannels;
717
+ }
718
+
719
+ /// The payload shape of an audio-capture event arm, matched by field name.
720
+ /// `pcm` is populated only for "data" and is valid for this dispatch; copy
721
+ /// bytes retained by the model. Chunks are at most 20ms. Drop counters make
722
+ /// bounded-queue back-pressure observable rather than silent.
723
+ export type AudioCaptureEventArm = {
724
+ readonly key: number;
725
+ readonly state: AudioCaptureState;
726
+ readonly source: AudioCaptureSource;
727
+ readonly sampleRate: number;
728
+ readonly channels: number;
729
+ readonly timestampMs: number;
730
+ readonly frames: number;
731
+ readonly pcm: Uint8Array;
732
+ readonly droppedPending: number;
733
+ readonly droppedTotal: number;
734
+ };
735
+
736
+ export type AudioCaptureEventKind<M extends Msgish> = M extends Msgish
737
+ ? [Exclude<keyof M, "kind">] extends [keyof AudioCaptureEventArm]
738
+ ? [keyof AudioCaptureEventArm] extends [Exclude<keyof M, "kind">]
739
+ ? M extends Msgish & AudioCaptureEventArm
740
+ ? [AudioCaptureState] extends [M["state"]]
741
+ ? [AudioCaptureSource] extends [M["source"]]
742
+ ? M["kind"]
743
+ : never
744
+ : never
745
+ : never
746
+ : never
747
+ : never
748
+ : never;
749
+
750
+ export interface AudioCaptureRoute<M extends Msgish> {
751
+ readonly event: AudioCaptureEventKind<M>;
752
+ }
753
+
562
754
  /// The pty session event states, mirroring the engine's vocabulary:
563
755
  /// "output" is one coalesced batch of child output; "exit" is the
564
756
  /// exactly-one terminal every session produces — a clean end, a signal,
@@ -575,8 +767,9 @@ export type PtyExitReason = "exited" | "signaled" | "cancelled" | "rejected" | "
575
767
 
576
768
  /// The payload shape of a pty event arm — seven fields, matched by NAME
577
769
  /// (the AudioEventArm convention). `key` is the app's own session key
578
- /// (the `Cmd.ptySpawn` `key`, or "" when the spawn named none) — two
579
- /// sessions routing one event arm are told apart by this field. `state`
770
+ /// as byte text (the `Cmd.ptySpawn` `key`, or empty bytes when the spawn
771
+ /// named none) — two sessions routing one event arm are told apart by
772
+ /// this field. `state`
580
773
  /// must be a named string-literal-union alias carrying exactly the two
581
774
  /// PtyState members and `reason` one carrying exactly the five
582
775
  /// PtyExitReason members (any declaration order — the host matches
@@ -587,7 +780,7 @@ export type PtyExitReason = "exited" | "signaled" | "cancelled" | "rejected" | "
587
780
  /// payloads refused over the session's life — zero means every write
588
781
  /// reached the child, never a silent drop.
589
782
  export type PtyEventArm = {
590
- readonly key: string;
783
+ readonly key: Uint8Array;
591
784
  readonly state: PtyState;
592
785
  readonly bytes: Uint8Array;
593
786
  readonly code: number;
@@ -597,7 +790,7 @@ export type PtyEventArm = {
597
790
  };
598
791
 
599
792
  /// The Msg arms a pty session may target: arms whose payload is exactly
600
- /// the six PtyEventArm fields. The `state` and `reason` checks run BOTH
793
+ /// the seven PtyEventArm fields. The `state` and `reason` checks run BOTH
601
794
  /// directions (the AudioEventKind convention): the `&` constraint holds
602
795
  /// the arm's unions to PtyState/PtyExitReason, and the tuple-wrapped
603
796
  /// reverse checks hold them to the arm's — a narrower union would
@@ -618,7 +811,7 @@ export type PtyEventKind<M extends Msgish> = M extends Msgish
618
811
  : never;
619
812
 
620
813
  /// `Cmd.ptySpawn` routing: every session event dispatches the `event`
621
- /// arm (the six-field PtyEventArm record, matched by field name).
814
+ /// arm (the seven-field PtyEventArm record, matched by field name).
622
815
  /// `cols`/`rows` are the initial grid the child observes (80x24 when
623
816
  /// omitted); `term` is the TERM the child starts with (omitted = the
624
817
  /// engine's default). The optional `key` names the session for
@@ -657,6 +850,62 @@ export interface WriteRoute<M extends Msgish> {
657
850
  readonly err: BytesKind<M>;
658
851
  }
659
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
+
660
909
  /// `Cmd.fetch` routing: the ok arm carries `{ status, body }` (one number
661
910
  /// field, one bytes field — matched by type); the err arm the reason bytes.
662
911
  export interface FetchRoute<M extends Msgish> {
@@ -665,6 +914,18 @@ export interface FetchRoute<M extends Msgish> {
665
914
  readonly err: BytesKind<M>;
666
915
  }
667
916
 
917
+ /// Streaming `Cmd.fetch` routing: each complete response line dispatches the
918
+ /// `line` arm with its bytes; the one successful terminal dispatches `ok` with
919
+ /// the HTTP status. A cut/dropped line dispatches `err` with `"truncated"` at
920
+ /// the terminal; transport failure and cancellation dispatch `err` with their
921
+ /// machine-readable reason bytes.
922
+ export interface FetchStreamRoute<M extends Msgish> {
923
+ readonly key?: string;
924
+ readonly line: BytesKind<M>;
925
+ readonly ok: TimestampKind<M>;
926
+ readonly err: BytesKind<M>;
927
+ }
928
+
668
929
  /// `Cmd.spawn` routing, line mode: each stdout line dispatches the optional
669
930
  /// `line` arm (one bytes field; omitted = lines dropped), a clean exit the
670
931
  /// `exit` arm (one number field — the exit code), every other end the `err`
@@ -767,6 +1028,15 @@ export interface FetchSpec {
767
1028
  readonly timeoutMs?: number;
768
1029
  }
769
1030
 
1031
+ /// A line-streamed fetch request. `maxLineBytes` overrides the engine's 4 KiB
1032
+ /// per-line default for SSE/NDJSON protocols whose individual records are
1033
+ /// larger; it is bounded by the engine's 256 KiB per-line ceiling. If a line
1034
+ /// is cut or dropped, the stream still ends through `err: "truncated"` rather
1035
+ /// than reporting a successful terminal.
1036
+ export interface FetchStreamSpec extends FetchSpec {
1037
+ readonly maxLineBytes?: number;
1038
+ }
1039
+
770
1040
  /// A desktop notification request. Text is bytes so every field may come
771
1041
  /// directly from model data; subtitle and body default to empty. Delivery is
772
1042
  /// fire-and-forget because the OS may suppress an accepted request through
@@ -777,6 +1047,11 @@ export interface NotificationSpec {
777
1047
  readonly body?: Uint8Array;
778
1048
  }
779
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
+
780
1055
  /// An inert command value, parameterized by the app's Msg union so the
781
1056
  /// factories can validate message targets. Opaque to app code: build with
782
1057
  /// the `Cmd.*` factories, return from `update`/`initialModel`, never inspect
@@ -793,6 +1068,19 @@ export type Cmd<M extends Msgish> =
793
1068
  readonly key: string;
794
1069
  readonly okKind: string;
795
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;
796
1084
  readonly payload: Uint8Array;
797
1085
  }
798
1086
  | { readonly op: "cancel"; readonly key: string }
@@ -811,6 +1099,53 @@ export type Cmd<M extends Msgish> =
811
1099
  readonly path: Uint8Array;
812
1100
  readonly bytes: Uint8Array;
813
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
+ }
814
1149
  | {
815
1150
  readonly op: "fetch";
816
1151
  readonly key: string;
@@ -826,6 +1161,19 @@ export type Cmd<M extends Msgish> =
826
1161
  readonly headers: readonly { readonly name: string; readonly value: string | Uint8Array }[];
827
1162
  readonly body: Uint8Array;
828
1163
  }
1164
+ | {
1165
+ readonly op: "fetch_stream";
1166
+ readonly key: string;
1167
+ readonly lineKind: string;
1168
+ readonly okKind: string;
1169
+ readonly errKind: string;
1170
+ readonly method: FetchMethod;
1171
+ readonly timeoutMs: number;
1172
+ readonly maxLineBytes: number;
1173
+ readonly url: Uint8Array;
1174
+ readonly headers: readonly { readonly name: string; readonly value: string | Uint8Array }[];
1175
+ readonly body: Uint8Array;
1176
+ }
829
1177
  | { readonly op: "clip_write"; readonly bytes: Uint8Array }
830
1178
  | { readonly op: "clip_read"; readonly key: string; readonly okKind: string; readonly errKind: string }
831
1179
  | { readonly op: "show_notification"; readonly title: Uint8Array; readonly subtitle: Uint8Array; readonly body: Uint8Array }
@@ -878,6 +1226,8 @@ export type Cmd<M extends Msgish> =
878
1226
  readonly value: number;
879
1227
  }
880
1228
  | { readonly op: "window_show"; readonly label: string }
1229
+ | { readonly op: "window_hide"; readonly label: string }
1230
+ | { readonly op: "dock_presence"; readonly visible: boolean }
881
1231
  | { readonly op: "quit_app" }
882
1232
  | {
883
1233
  readonly op: "image_load";
@@ -890,8 +1240,17 @@ export type Cmd<M extends Msgish> =
890
1240
  }
891
1241
  | { readonly op: "image_cancel"; readonly id: number }
892
1242
  | { readonly op: "image_unregister"; readonly id: number }
893
- | { readonly op: "channel_open"; readonly key: number; readonly eventKind: string }
1243
+ | { readonly op: "channel_open"; readonly key: number; readonly eventKind: string; readonly maxPending: number }
894
1244
  | { readonly op: "channel_close"; readonly key: number }
1245
+ | {
1246
+ readonly op: "audio_capture_start";
1247
+ readonly key: number;
1248
+ readonly source: AudioCaptureSource;
1249
+ readonly sampleRate: number;
1250
+ readonly channels: number;
1251
+ readonly eventKind: string;
1252
+ }
1253
+ | { readonly op: "audio_capture_stop"; readonly key: number }
895
1254
  | {
896
1255
  readonly op: "pty_spawn";
897
1256
  readonly key: string;
@@ -942,6 +1301,74 @@ export function hostRecordBytes(payload: HostRecord): Uint8Array {
942
1301
  return out;
943
1302
  }
944
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
+
945
1372
  function lowerHostPayload(payload: Uint8Array | HostRecord): Uint8Array {
946
1373
  return payload instanceof Uint8Array ? payload : hostRecordBytes(payload);
947
1374
  }
@@ -959,11 +1386,55 @@ function hostCmd(name: string, ...rest: readonly (number | Uint8Array | HostReco
959
1386
  return { op: "host", name, args: rest as readonly number[] };
960
1387
  }
961
1388
 
1389
+ /// HTTP(S) as effect data. The two-field `{ ok, err }` route buffers the whole
1390
+ /// response. Adding `line` selects line streaming: each complete line arrives
1391
+ /// through that one-bytes-field arm, then `ok` receives the terminal HTTP
1392
+ /// status as its one number payload. A non-2xx status is still `ok` because the
1393
+ /// server delivered a response; cut/dropped stream lines, transport failures,
1394
+ /// and cancellation use `err` (`"truncated"` for stream data loss).
1395
+ function fetchCmd<M extends Msgish>(spec: FetchSpec, route: FetchRoute<M>): Cmd<M>;
1396
+ function fetchCmd<M extends Msgish>(spec: FetchStreamSpec, route: FetchStreamRoute<M>): Cmd<M>;
1397
+ function fetchCmd<M extends Msgish>(
1398
+ spec: FetchStreamSpec,
1399
+ route: FetchRoute<M> | FetchStreamRoute<M>,
1400
+ ): Cmd<M> {
1401
+ const names = Object.keys(spec.headers ?? {}).sort();
1402
+ const headers = names.map((n) => ({ name: n, value: spec.headers![n] }));
1403
+ if ("line" in route) {
1404
+ return {
1405
+ op: "fetch_stream",
1406
+ key: route.key ?? "",
1407
+ lineKind: route.line,
1408
+ okKind: route.ok,
1409
+ errKind: route.err,
1410
+ method: spec.method ?? "GET",
1411
+ timeoutMs: spec.timeoutMs ?? 0,
1412
+ maxLineBytes: spec.maxLineBytes ?? 0,
1413
+ url: spec.url,
1414
+ headers,
1415
+ body: spec.body ?? new Uint8Array(0),
1416
+ };
1417
+ }
1418
+ return {
1419
+ op: "fetch",
1420
+ key: route.key ?? "",
1421
+ okKind: route.ok,
1422
+ errKind: route.err,
1423
+ method: spec.method ?? "GET",
1424
+ timeoutMs: spec.timeoutMs ?? 0,
1425
+ url: spec.url,
1426
+ headers,
1427
+ body: spec.body ?? new Uint8Array(0),
1428
+ };
1429
+ }
1430
+
962
1431
  export const Cmd = {
963
1432
  /// No effects. `return model` is sugar for `return [model, Cmd.none]`.
964
1433
  none: { op: "none" } as Cmd<never>,
965
1434
 
966
- /// 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.
967
1438
  persist(): Cmd<never> {
968
1439
  return { op: "persist" };
969
1440
  },
@@ -993,14 +1464,55 @@ export const Cmd = {
993
1464
  key: route.key ?? "",
994
1465
  okKind: route.ok,
995
1466
  errKind: route.err,
1467
+ typedService: false,
996
1468
  payload: lowerHostPayload(payload),
997
1469
  };
998
1470
  },
999
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
+
1000
1512
  /// Drop the in-flight keyed effect — request, named engine op, or delay —
1001
1513
  /// with this key, if any, SILENTLY (neither routing arm is dispatched for
1002
- /// it). The exception is a live spawn: cancel ends the child and its err
1003
- /// arm runs with "cancelled" — killing a process is an observable event.
1514
+ /// it). Live spawn, streaming-fetch, and streaming-service operations are the exceptions:
1515
+ /// cancel ends the stream and its err arm runs with "cancelled".
1004
1516
  cancel(key: string): Cmd<never> {
1005
1517
  return { op: "cancel", key };
1006
1518
  },
@@ -1019,25 +1531,105 @@ export const Cmd = {
1019
1531
  return { op: "write_file", key: route.key ?? "", okKind: route.ok, errKind: route.err, path, bytes };
1020
1532
  },
1021
1533
 
1022
- /// A buffered HTTP(S) exchange. Exactly one terminal Msg: the `ok` arm
1023
- /// with `{ status, body }` (one number field, one bytes field — a non-2xx
1024
- /// status is still ok: an HTTP-level error is a delivered response), or
1025
- /// the `err` arm with the reason bytes.
1026
- fetch<M extends Msgish>(spec: FetchSpec, route: FetchRoute<M>): Cmd<M> {
1027
- const names = Object.keys(spec.headers ?? {}).sort();
1028
- return {
1029
- op: "fetch",
1030
- key: route.key ?? "",
1031
- okKind: route.ok,
1032
- errKind: route.err,
1033
- method: spec.method ?? "GET",
1034
- timeoutMs: spec.timeoutMs ?? 0,
1035
- url: spec.url,
1036
- headers: names.map((n) => ({ name: n, value: spec.headers![n] })),
1037
- body: spec.body ?? new Uint8Array(0),
1038
- };
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
+ },
1039
1606
  },
1040
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
+
1631
+ fetch: fetchCmd,
1632
+
1041
1633
  /// Put bytes on the system clipboard, fire-and-forget (an over-bound or
1042
1634
  /// refused write is dropped — there is no route to report on).
1043
1635
  clipboardWrite(bytes: Uint8Array): Cmd<never> {
@@ -1064,6 +1656,27 @@ export const Cmd = {
1064
1656
  };
1065
1657
  },
1066
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
+
1067
1680
  /// A keyed one-shot delay: dispatch the named Msg arm once, `ms` from now,
1068
1681
  /// with the fire time (milliseconds) as its single number payload field.
1069
1682
  /// Re-issuing a live key re-arms it from now (the debounce discipline);
@@ -1231,6 +1844,31 @@ export const Cmd = {
1231
1844
  return { op: "window_show", label };
1232
1845
  },
1233
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
+
1234
1872
  /// Quit the app for real — the graceful terminate, and the tray "Quit"
1235
1873
  /// consequence. The host quits through the SAME shutdown path a
1236
1874
  /// last-window close takes, so the stop hook runs exactly once and a
@@ -1322,7 +1960,7 @@ export const Cmd = {
1322
1960
  /// one that consults the handle's live() before launching never
1323
1961
  /// starts, keeping replay fully offline.
1324
1962
  channelOpen<M extends Msgish>(key: number, route: ChannelRoute<M>): Cmd<M> {
1325
- return { op: "channel_open", key, eventKind: route.event };
1963
+ return { op: "channel_open", key, eventKind: route.event, maxPending: 64 };
1326
1964
  },
1327
1965
 
1328
1966
  /// Close the open channel under `key`: posts stop landing, the
@@ -1336,6 +1974,29 @@ export const Cmd = {
1336
1974
  return { op: "channel_close", key };
1337
1975
  },
1338
1976
 
1977
+ /// Start microphone or system-output capture under a positive numeric
1978
+ /// `key`. The stream is normalized to interleaved signed 16-bit LE PCM
1979
+ /// at the requested rate/channels (48kHz mono by default). Microphone and
1980
+ /// system capture may run concurrently; only one stream per source may
1981
+ /// be live. The event stream is bounded and journal/replay aware.
1982
+ audioCaptureStart<M extends Msgish>(key: number, spec: AudioCaptureSpec, route: AudioCaptureRoute<M>): Cmd<M> {
1983
+ return {
1984
+ op: "audio_capture_start",
1985
+ key,
1986
+ source: spec.source,
1987
+ sampleRate: spec.sampleRate ?? 48000,
1988
+ channels: spec.channels ?? 1,
1989
+ eventKind: route.event,
1990
+ };
1991
+ },
1992
+
1993
+ /// Stop capture under `key`. Native callbacks quiesce synchronously,
1994
+ /// accepted backlog drains, then exactly one "stopped" event retires the
1995
+ /// stream. A key with no live capture is a no-op.
1996
+ audioCaptureStop(key: number): Cmd<never> {
1997
+ return { op: "audio_capture_stop", key };
1998
+ },
1999
+
1339
2000
  /// Open a pseudo-terminal session — a spawn with a different
1340
2001
  /// transport: run `argv` on a fresh pty (same argv budgets, same
1341
2002
  /// child environment policy) whose initial grid is `cols` x `rows`
@@ -1402,6 +2063,16 @@ export const Cmd = {
1402
2063
  export type Sub<M extends Msgish> =
1403
2064
  | { readonly op: "none" }
1404
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
+ }
1405
2076
  | { readonly op: "batch"; readonly subs: readonly Sub<M>[] };
1406
2077
 
1407
2078
  export const Sub = {
@@ -1415,6 +2086,8 @@ export const Sub = {
1415
2086
  return { op: "timer", key, everyMs, msgKind };
1416
2087
  },
1417
2088
 
2089
+ // @native-sqlite-generated-subs
2090
+
1418
2091
  /// Several subscriptions at once.
1419
2092
  batch<M extends Msgish>(subs: readonly Sub<M>[]): Sub<M> {
1420
2093
  return { op: "batch", subs };