@briancray/belte 0.1.0 → 0.2.1

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 (103) hide show
  1. package/bin/belte.ts +25 -12
  2. package/package.json +2 -1
  3. package/src/appEntry.ts +124 -0
  4. package/src/belteResolverPlugin.ts +236 -202
  5. package/src/build.ts +6 -67
  6. package/src/buildCli.ts +36 -63
  7. package/src/buildDisconnected.ts +127 -0
  8. package/src/bundleApp.ts +123 -0
  9. package/src/bundleDisconnectedEntry.ts +17 -0
  10. package/src/cliEntry.ts +3 -9
  11. package/src/compile.ts +4 -15
  12. package/src/controlServerWorker.ts +261 -0
  13. package/src/dedupeSveltePlugin.ts +66 -0
  14. package/src/discoveryEntry.ts +12 -11
  15. package/src/lib/browser/cache.ts +3 -6
  16. package/src/lib/browser/page.svelte.ts +19 -21
  17. package/src/lib/browser/socketChannel.ts +11 -1
  18. package/src/lib/browser/types/Pages.ts +1 -1
  19. package/src/lib/bundle/BundleMenu.ts +11 -0
  20. package/src/lib/bundle/BundleMenuItem.ts +24 -0
  21. package/src/lib/bundle/BundleWindow.ts +20 -0
  22. package/src/lib/bundle/bindConnectedFlag.ts +29 -0
  23. package/src/lib/bundle/bindRequestNavigate.ts +31 -0
  24. package/src/lib/bundle/buildWebviewLib.ts +111 -0
  25. package/src/lib/bundle/disconnected.css +9 -0
  26. package/src/lib/bundle/disconnected.svelte +192 -0
  27. package/src/lib/bundle/ensureWebviewLib.ts +20 -0
  28. package/src/lib/bundle/exitWithParent.ts +28 -0
  29. package/src/lib/bundle/findFreePort.ts +14 -0
  30. package/src/lib/bundle/infoPlist.ts +46 -0
  31. package/src/lib/bundle/installMacMenu.ts +39 -0
  32. package/src/lib/bundle/listenLocalControlServer.ts +19 -0
  33. package/src/lib/bundle/native/belteMenu.mm +298 -0
  34. package/src/lib/bundle/native/webview.h +4557 -0
  35. package/src/lib/bundle/onMenu.ts +26 -0
  36. package/src/lib/bundle/openWebview.ts +81 -0
  37. package/src/lib/bundle/pngToIcns.ts +47 -0
  38. package/src/lib/bundle/probeBelteServer.ts +34 -0
  39. package/src/lib/bundle/resolveServerBinary.ts +12 -0
  40. package/src/lib/bundle/resolveWebviewLib.ts +51 -0
  41. package/src/lib/bundle/serverBinaryFilename.ts +8 -0
  42. package/src/lib/bundle/stableLocalPort.ts +19 -0
  43. package/src/lib/bundle/waitForServer.ts +23 -0
  44. package/src/lib/bundle/webviewBuildRevision.ts +9 -0
  45. package/src/lib/bundle/webviewCachePath.ts +23 -0
  46. package/src/lib/bundle/webviewLibName.ts +11 -0
  47. package/src/lib/bundle/webviewVersion.ts +7 -0
  48. package/src/lib/cli/createClient.ts +34 -36
  49. package/src/lib/cli/printHelp.ts +45 -2
  50. package/src/lib/cli/runCli.ts +12 -3
  51. package/src/lib/mcp/createMcpResourceServer.ts +1 -1
  52. package/src/lib/mcp/dispatchMcpRequest.ts +53 -73
  53. package/src/lib/server/AppModule.ts +2 -2
  54. package/src/lib/server/cli/handleCliDownload.ts +4 -5
  55. package/src/lib/server/cli/handleCliInstall.ts +17 -0
  56. package/src/lib/server/error.ts +23 -9
  57. package/src/lib/server/json.ts +5 -5
  58. package/src/lib/server/jsonl.ts +10 -5
  59. package/src/lib/server/prompts/definePrompt.ts +6 -6
  60. package/src/lib/server/prompts/renderPromptTemplate.ts +16 -0
  61. package/src/lib/server/prompts/types/Prompt.ts +8 -9
  62. package/src/lib/server/prompts/types/PromptOptions.ts +7 -12
  63. package/src/lib/server/prompts/types/PromptRegistryEntry.ts +3 -5
  64. package/src/lib/server/prompts/types/PromptRoutes.ts +4 -4
  65. package/src/lib/server/redirect.ts +13 -8
  66. package/src/lib/server/rpc/defineVerb.ts +4 -3
  67. package/src/lib/server/rpc/findVerbByCommandName.ts +18 -0
  68. package/src/lib/server/rpc/types/RemoteFunction.ts +1 -1
  69. package/src/lib/server/rpc/types/RemoteHandler.ts +4 -0
  70. package/src/lib/server/runtime/acceptsZstd.ts +8 -0
  71. package/src/lib/server/runtime/buildOpenApiSpec.ts +2 -0
  72. package/src/lib/server/runtime/cacheControlForAsset.ts +7 -2
  73. package/src/lib/server/runtime/createAssetHeaderCache.ts +35 -0
  74. package/src/lib/server/runtime/createPublicAssetServer.ts +6 -20
  75. package/src/lib/server/runtime/createServer.ts +50 -58
  76. package/src/lib/server/runtime/registryManifests.ts +33 -15
  77. package/src/lib/server/runtime/types/RequestStore.ts +2 -3
  78. package/src/lib/server/runtime/withResponseDefaults.ts +24 -0
  79. package/src/lib/server/sockets/createSocketDispatcher.ts +10 -7
  80. package/src/lib/server/sse.ts +10 -5
  81. package/src/lib/shared/belteImportName.test.ts +58 -0
  82. package/src/lib/shared/belteImportName.ts +45 -0
  83. package/src/lib/shared/beltePackageName.ts +7 -0
  84. package/src/lib/shared/cacheControlValues.ts +10 -2
  85. package/src/lib/shared/canonicalJson.ts +1 -5
  86. package/src/lib/shared/createCacheStore.ts +29 -20
  87. package/src/lib/shared/exitOnBuildFailure.ts +17 -0
  88. package/src/lib/shared/fileStem.ts +9 -0
  89. package/src/lib/shared/jsonSchemaForPromptArguments.ts +29 -0
  90. package/src/lib/shared/keyForRemoteCall.ts +7 -5
  91. package/src/lib/shared/parsePromptMarkdown.ts +34 -0
  92. package/src/lib/shared/prepareRpcModule.ts +14 -4
  93. package/src/lib/shared/prepareSocketModule.ts +16 -2
  94. package/src/lib/shared/promptNameForFile.ts +5 -5
  95. package/src/lib/shared/subscribableFromResponse.ts +104 -215
  96. package/src/lib/shared/types/PromptArgument.ts +12 -0
  97. package/src/lib/shared/writeRoutesDts.ts +5 -7
  98. package/src/serverBuildPlugins.ts +25 -0
  99. package/src/serverEntry.ts +4 -0
  100. package/template/package.json +3 -2
  101. package/src/lib/server/prompt.ts +0 -30
  102. package/src/lib/server/prompts/types/PromptMessage.ts +0 -10
  103. package/src/lib/shared/preparePromptModule.ts +0 -36
package/bin/belte.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env bun
2
2
  import { build } from '../src/build.ts'
3
3
  import { buildCli } from '../src/buildCli.ts'
4
+ import { bundleApp } from '../src/bundleApp.ts'
4
5
  import { compile } from '../src/compile.ts'
5
6
  import { normalizeTarget } from '../src/lib/shared/normalizeTarget.ts'
6
7
  import { scaffold } from '../src/scaffold.ts'
@@ -68,17 +69,16 @@ async function compileCmd(): Promise<void> {
68
69
  })
69
70
  }
70
71
 
71
- // Builds the standalone CLI binary. Defaults to full (backend embedded, runs
72
- // locally); `--thin` builds the remote client (manifest only, needs APP_URL
73
- // at runtime). Discovery walks the rpc registry to bake the manifest in.
74
- // `--platforms a,b,c` cross-compiles per target thin binaries land in
75
- // dist/cli-thin/<platform>/ (the layout the /__belte/cli download endpoint
76
- // streams), full binaries in dist/cli/<platform>/.
72
+ // Builds the standalone CLI binary a thin remote client that talks to a
73
+ // running server (manifest baked in, needs APP_URL at runtime). Discovery
74
+ // walks the rpc registry to bake the manifest in. `--platforms a,b,c`
75
+ // cross-compiles per target into dist/cli-thin/<platform>/ the layout the
76
+ // /__belte/cli download endpoint streams. For an embedded backend, use
77
+ // `belte compile` (standalone server binary) instead.
77
78
  async function cliCmd(): Promise<void> {
78
79
  const targetFlag = parseFlag('target')
79
80
  const outFlag = parseFlag('out')
80
81
  const platformsFlag = parseFlag('platforms')
81
- const thin = rest.includes('--thin')
82
82
  const platforms = platformsFlag
83
83
  ? platformsFlag.split(',').map((value) => normalizeTarget(value.trim()))
84
84
  : undefined
@@ -87,10 +87,17 @@ async function cliCmd(): Promise<void> {
87
87
  target: targetFlag ? normalizeTarget(targetFlag) : undefined,
88
88
  outfile: outFlag,
89
89
  platforms,
90
- thin,
91
90
  })
92
91
  }
93
92
 
93
+ // Assembles a movable, self-contained app bundle for the host platform —
94
+ // the server binary, the launcher, and the webview lib together (a .app on
95
+ // macOS, a flat directory elsewhere). Unsigned; for distribution to other
96
+ // users the bundle still needs platform signing/notarization.
97
+ async function bundleCmd(): Promise<void> {
98
+ await bundleApp({ cwd })
99
+ }
100
+
94
101
  // Scaffolds the bundled template into a new project directory.
95
102
  async function scaffoldCmd(): Promise<void> {
96
103
  const name = rest.find((arg) => !arg.startsWith('--'))
@@ -111,10 +118,14 @@ function usage(): never {
111
118
  ' belte start run the production server against dist/\n' +
112
119
  ' belte compile [--target=<bun-...>] [--out=<path>]\n' +
113
120
  ' build a standalone server executable\n' +
114
- ' belte cli [--thin] [--target=<bun-...>] [--out=<path>] [--platforms=<a,b,c>]\n' +
115
- ' build the cli binary (full by default runs\n' +
116
- ' locally; --thin builds the remote client;\n' +
117
- ' --platforms cross-compiles per platform)',
121
+ ' belte cli [--target=<bun-...>] [--out=<path>] [--platforms=<a,b,c>]\n' +
122
+ ' build the cli binary a thin remote client\n' +
123
+ ' that talks to a running server (needs APP_URL;\n' +
124
+ ' --platforms cross-compiles per platform)\n' +
125
+ ' belte bundle build a movable, self-contained app\n' +
126
+ ' bundle for this platform (unsigned). Boots\n' +
127
+ ' into a connect screen — start the embedded\n' +
128
+ ' server or connect to a remote one',
118
129
  )
119
130
  process.exit(1)
120
131
  }
@@ -131,6 +142,8 @@ if (command === 'scaffold') {
131
142
  await compileCmd()
132
143
  } else if (command === 'cli') {
133
144
  await cliCmd()
145
+ } else if (command === 'bundle') {
146
+ await bundleCmd()
134
147
  } else {
135
148
  usage()
136
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@briancray/belte",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Isomorphic multimodal HTTP framework built for humans and machines in a single Bun runtime",
6
6
  "license": "MIT",
@@ -43,6 +43,7 @@
43
43
  "./browser/HttpError": "./src/lib/server/HttpError.ts",
44
44
  "./mcp/*": "./src/lib/mcp/*.ts",
45
45
  "./cli/*": "./src/lib/cli/*.ts",
46
+ "./bundle/*": "./src/lib/bundle/*.ts",
46
47
  "./shared/*": "./src/lib/shared/*.ts",
47
48
  "./build": "./src/build.ts",
48
49
  "./compile": "./src/compile.ts",
@@ -0,0 +1,124 @@
1
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
2
+ import { disconnectedHtml } from './_virtual/bundle-disconnected.ts'
3
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
4
+ import bundleWindow from './_virtual/bundle-window.ts'
5
+ // @ts-expect-error virtual module resolved by belteResolverPlugin
6
+ import programName from './_virtual/cli-name.ts'
7
+ import type { BundleMenu } from './lib/bundle/BundleMenu.ts'
8
+ import type { BundleWindow } from './lib/bundle/BundleWindow.ts'
9
+ import { openWebview } from './lib/bundle/openWebview.ts'
10
+ import { log } from './lib/shared/log.ts'
11
+
12
+ /*
13
+ Compiled bundle launcher entry — the executable a bundle runs. Instead of a blank
14
+ window it boots into a connect screen, letting the user either connect to a remote
15
+ server by URL or start the embedded server binary that ships beside this launcher.
16
+
17
+ The connect screen is served by a tiny control server, but that server can't live
18
+ on this main thread: `openWebview` calls `webview_run`, a native UI loop that
19
+ blocks the thread (and its JS event loop) until the window closes, so an
20
+ in-process `Bun.serve` would never answer. The control server runs in a Worker
21
+ instead (see controlServerWorker.ts) and hands back the origin to point the window
22
+ at; it also owns the embedded-server child and the native menu flag, since neither
23
+ can be driven from a main thread frozen inside webview_run.
24
+
25
+ Bun doesn't apply this build's plugins to the worker entry, so the worker can't
26
+ import belte's virtual modules; this entry imports them (the connect-screen HTML,
27
+ the app title) and passes them in the worker's `init` message.
28
+
29
+ The window owns the main thread; on close we tell the worker to reap its child.
30
+ */
31
+ const window = bundleWindow as BundleWindow
32
+ const title = window.title ?? programName
33
+
34
+ /*
35
+ Spawn the control server worker. `__BELTE_WORKER_ENTRY__` is the worker's absolute
36
+ path, injected by bundleApp via Bun's `define` so the specifier is a static literal
37
+ at build time — that's what makes `bun build --compile` embed the worker module
38
+ into the standalone binary. A relative specifier resolves against the build's cwd
39
+ (the consumer project), not this file, so it isn't found; a
40
+ `new URL(..., import.meta.url)` specifier isn't embedded at all.
41
+ */
42
+ declare const __BELTE_WORKER_ENTRY__: string
43
+ const worker = new Worker(__BELTE_WORKER_ENTRY__)
44
+
45
+ worker.addEventListener('error', (event: ErrorEvent) => {
46
+ log.error(`control server worker failed: ${event.message}`)
47
+ })
48
+
49
+ // Hand the worker the plugin-resolved data it can't import itself, then start it.
50
+ worker.postMessage({
51
+ type: 'init',
52
+ init: { disconnectedHtml: disconnectedHtml as string, title, programName },
53
+ })
54
+
55
+ // The worker posts its control-server origin once bound; the window points here.
56
+ const origin = await new Promise<string>((resolve) => {
57
+ worker.addEventListener('message', (event: MessageEvent) => {
58
+ const data = event.data as { type: string; origin?: string }
59
+ if (data.type === 'ready' && data.origin) {
60
+ resolve(data.origin)
61
+ }
62
+ })
63
+ })
64
+
65
+ /*
66
+ The built-in File menu (Start / Disconnect), placed before Edit. Each item is a
67
+ `navigate` repointing the window at a control-server URL the connect screen
68
+ interprets; the internal `role` drives the native validateMenuItem: gating (Start
69
+ when disconnected, Disconnect when connected). There's no Connect item — Disconnect
70
+ already returns to the connect screen, whose form is the place to point at another
71
+ server. Roled items are launcher-only, so they carry an extra field the public
72
+ BundleMenuItem type doesn't advertise — modelled here with a local type and bridged
73
+ to BundleMenu when handed to openWebview (which only serialises the menu to JSON).
74
+ */
75
+ type FileMenuItem =
76
+ | { separator: true }
77
+ | {
78
+ label: string
79
+ shortcut?: string
80
+ navigate: string
81
+ role: 'start' | 'disconnect'
82
+ }
83
+ const fileMenu: { label: string; items: FileMenuItem[] } = {
84
+ label: 'File',
85
+ items: [
86
+ { label: 'Start Server', navigate: `${origin}/?action=start`, role: 'start' },
87
+ { separator: true },
88
+ { label: 'Disconnect', navigate: `${origin}/?action=disconnect`, role: 'disconnect' },
89
+ ],
90
+ }
91
+
92
+ log.info(`opening ${title} connect screen at ${origin}`)
93
+ await openWebview({
94
+ url: origin,
95
+ title,
96
+ width: window.width,
97
+ height: window.height,
98
+ menu: window.menu,
99
+ fileMenu: fileMenu as unknown as BundleMenu,
100
+ // Forward the window handle so the worker can bounce it back to the connect
101
+ // screen if the connected server stops responding.
102
+ onWindow: (handle) => {
103
+ if (handle) {
104
+ worker.postMessage({ type: 'window', handle: Number(handle) })
105
+ }
106
+ },
107
+ })
108
+
109
+ /*
110
+ Window closed — have the worker reap the embedded server child and stop its
111
+ control server before we exit, since both live on the worker thread. Bounded by a
112
+ timeout so a wedged worker can't keep the launcher alive.
113
+ */
114
+ await new Promise<void>((resolve) => {
115
+ worker.addEventListener('message', (event: MessageEvent) => {
116
+ if ((event.data as { type?: string }).type === 'shutdownDone') {
117
+ resolve()
118
+ }
119
+ })
120
+ worker.postMessage({ type: 'shutdown' })
121
+ setTimeout(resolve, 1000)
122
+ })
123
+ worker.terminate()
124
+ process.exit(0)