@microverse.ts/microverse-lua 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  **Lua microverse** facade for TypeScript applications: `MicroverseLua.create`, Wasm VM, script slots, and the fluent host surface builder.
4
4
 
5
- Monorepo overview: [root README](../../README.md).
5
+ Monorepo overview (protocol vision): [root README](../../README.md).
6
6
 
7
7
  ## What is a Lua microverse?
8
8
 
9
9
  One logical scripting universe in your process:
10
10
 
11
11
  - **One** Wasm-backed Lua VM (Wasmoon), shared for efficiency.
12
- - **Many** isolated **environment slots** — one per registered script.
13
- - **One** host **surface** (declarative bridges + optional workflow hooks).
12
+ - **Many** isolated **environment slots** — one per mounted script instance.
13
+ - **One** host **surface** (declarative bridges + optional component hooks).
14
14
  - **One** host **object** (your TypeScript services).
15
- - **Per-script capability allowlists** at registration.
15
+ - **Per-instance capability allowlists** at mount.
16
16
 
17
17
  ```
18
18
  ┌─────────────────────────────────────────────────────────────┐
@@ -24,7 +24,7 @@ One logical scripting universe in your process:
24
24
  │ │ │ caps: […] │ │ caps: […] │ │ caps: […] │ │ │
25
25
  │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
26
26
  │ └───────────────────────────────────────────────────────┘ │
27
- │ ▲ bridges from host surface + host services
27
+ │ ▲ bridges via self.bridges (scoped per instance)
28
28
  └─────────────────────────────────────────────────────────────┘
29
29
  ```
30
30
 
@@ -36,7 +36,68 @@ pnpm add @microverse.ts/microverse-lua
36
36
 
37
37
  Workspace: `"@microverse.ts/microverse-lua": "workspace:*"`.
38
38
 
39
- ## Quick start
39
+ ## Concepts
40
+
41
+ | Term | Meaning |
42
+ |------|---------|
43
+ | **Microverse** | One shared Wasm Lua VM + catalog of script definitions + N mounted instances ([`LuaMicroverse`](src/infrastructure/facade/luaMicroverse.ts)). |
44
+ | **Host** | Your typed services object passed to `MicroverseLua.create({ host })`. Handlers on the surface receive it; Lua never sees the raw host table. |
45
+ | **Surface** | Result of `defineHostSurfaceFor<THost>().bridge(…).method(…).componentHooks(…).build()` — bridges, capability ids, and a manifest for `.d.lua`. |
46
+ | **Bridge** | A named group of host methods exposed in Lua as `self.bridges.<name>:<method>(payload)`. **Not** a global in the script slot. |
47
+ | **Capability** | A `domain:action` string declared on each bridge method (`requires`). Each instance mounts with an allowlist (`surface.pickCapabilities(…)`). |
48
+ | **Script definition** | Catalog entry: `scriptId`, Lua source, optional `injectLuaChunks`, optional props schema / defaults. |
49
+ | **Script instance** | A mounted slot: `instanceId`, `scriptId`, capabilities, props, and the active component table from `component:extend()`. |
50
+ | **Component** | Lua table from `component:extend()` with `properties`, `state`, `bridges`, lifecycle (`init`, `onDestroy`, `onPropsChanged`), and domain `on*` hooks. |
51
+
52
+ ## Engine lifecycle
53
+
54
+ When you mount an instance, the runtime runs this pipeline (see [`mountScriptInstance`](src/infrastructure/facade/luaMicroverse.ts)):
55
+
56
+ ```mermaid
57
+ sequenceDiagram
58
+ participant App
59
+ participant MV as LuaMicroverse
60
+ participant Session as HostScriptSession
61
+ participant VM as Wasm Lua
62
+
63
+ App->>MV: registerScriptDefinition
64
+ App->>MV: mountScriptInstance
65
+ MV->>Session: openSession
66
+ Session->>VM: run shared + inject prelude chunks
67
+ Session->>VM: run main chunk
68
+ Session->>VM: setProps
69
+ Session->>VM: invoke init
70
+ App->>MV: emitToAllInstances
71
+ MV->>Session: invoke on* hooks
72
+ App->>MV: unmountScriptInstance
73
+ MV->>Session: onDestroy + dispose
74
+ ```
75
+
76
+ 1. **`registerScriptDefinition`** — Add source to the catalog (does not allocate a slot).
77
+ 2. **`mountScriptInstance`** — Open a slot, run preludes + main chunk, merge props, call `init`.
78
+ 3. **`emitToAllInstances`** — Broadcast a component hook (`OrderPlaced`, …) to every mounted instance.
79
+ 4. **`unmountScriptInstance`** — Call `onDestroy`, tear down the slot.
80
+ 5. **`dispose`** — Unmount all instances.
81
+
82
+ ## API
83
+
84
+ | Export / method | Purpose |
85
+ |-----------------|--------|
86
+ | `MicroverseLua.create` | Create a Lua microverse (Wasm VM included). |
87
+ | `registerScriptDefinition` | Catalog entry (source, optional preludes, props schema). |
88
+ | `mountScriptInstance` | New Wasm slot for one instance (capabilities, props, audit). |
89
+ | `unmountScriptInstance` | Tear down one instance. |
90
+ | `emitToAllInstances` | Call `on{Kind}` on every mounted instance (component hooks). |
91
+ | `setInstanceProps` / `patchInstanceProps` | Update host-synced props; may invoke `onPropsChanged`. |
92
+ | `getInstanceProps` / `flushInstanceProps` | Read or flush props proxy state. |
93
+ | `getSurfaceCapabilities` | All capability ids declared on the surface. |
94
+ | `surface.pickCapabilities(…)` | Build an allowlist for `mountScriptInstance`. |
95
+ | `dispose` | Unmount all instances. |
96
+ | `defineHostSurfaceFor`, `defineHostSurface` | Fluent surface builder (`bridge` → `method` → `build`). |
97
+
98
+ Re-exported from this package for convenience; lower-level session API lives in `@microverse.ts/host-surface`.
99
+
100
+ ## Quick start (minimal)
40
101
 
41
102
  ```ts
42
103
  import { MicroverseLua, defineHostSurfaceFor } from '@microverse.ts/microverse-lua';
@@ -60,38 +121,249 @@ const microverse = MicroverseLua.create({
60
121
  defaultTimeoutMs: 30_000,
61
122
  });
62
123
 
63
- await microverse.registerScript({
124
+ microverse.registerScriptDefinition({
125
+ scriptId: 'welcome',
126
+ source: `local msg = greet:hello({ name = "world" })`,
127
+ });
128
+ await microverse.mountScriptInstance({
129
+ instanceId: 'welcome',
64
130
  scriptId: 'welcome',
65
- script: `local msg = greet:hello({ name = "world" })`,
66
131
  capabilities: surface.pickCapabilities('demo:greet'),
67
132
  });
68
133
 
69
134
  await microverse.dispose();
70
135
  ```
71
136
 
72
- ## API
137
+ > The minimal sample uses a global `greet` table for brevity. In production, use **`component:extend()`** and **`self.bridges`** (see [Lua authoring](#lua-authoring) and [Integrating in your app](#integrating-in-your-app)).
73
138
 
74
- | Export | Purpose |
75
- |--------|---------|
76
- | `MicroverseLua.create` | Create a Lua microverse (Wasm VM included). |
77
- | `registerScript` | New slot + optional preludes + main chunk. |
78
- | `emitToAllScripts` | Call `on{Kind}` on every script (workflow hooks). |
79
- | `defineHostSurfaceFor`, `defineHostSurface` | Fluent surface builder (`bridge` → `method` → `build`). |
139
+ ## Integrating in your app
140
+
141
+ The reference layout is [`examples/business-scripting-engine`](../../examples/business-scripting-engine). File tour: [example README](../../examples/business-scripting-engine/README.md).
142
+
143
+ ### 1. Define the host
144
+
145
+ Aggregate your real services into one object the surface handlers receive:
146
+
147
+ ```ts
148
+ // examples/business-scripting-engine/src/services/businessEngineHost.ts
149
+ export type BusinessEngineHost = {
150
+ readonly orders: OrdersService;
151
+ readonly billing: BillingService;
152
+ // …
153
+ };
154
+ ```
155
+
156
+ Construct it in your app and pass it to `MicroverseLua.create({ host, surface })`.
157
+
158
+ ### 2. Define the surface
159
+
160
+ Declare bridges (Lua → host), Zod payloads, capabilities, and optional component hooks (host → Lua):
161
+
162
+ ```ts
163
+ // examples/business-scripting-engine/src/businessSurface.ts
164
+ import { defineHostSurfaceFor } from '@microverse.ts/microverse-lua';
165
+ import { businessComponentHooks } from './schemas/components/businessComponentHooks';
166
+
167
+ export default defineHostSurfaceFor<BusinessEngineHost>()
168
+ .bridge('orders')
169
+ .method('get', {
170
+ requires: 'orders:read',
171
+ input: z.object({ orderId }),
172
+ output: orderDto.optional(),
173
+ handler: ({ host }, { orderId }) => host.orders.get(orderId),
174
+ })
175
+ // … more bridges …
176
+ .componentHooks(businessComponentHooks)
177
+ .build();
178
+ ```
179
+
180
+ - **`requires`** — Capability id enforced at runtime for this method.
181
+ - **`handler`** — Runs in TypeScript with `{ host, script }` context.
182
+ - **`componentHooks`** — Map of event kind → Zod object; generates `onOrderPlaced`, `MicroverseEvt_OrderPlaced`, etc. in `.d.lua`.
183
+
184
+ Default-export the built surface for `microverse generate-lua-defs --surface …`.
185
+
186
+ ### 3. Wrap the engine (optional)
187
+
188
+ A thin façade keeps app code free of microverse details:
189
+
190
+ ```ts
191
+ // examples/business-scripting-engine/src/BusinessScriptingEngine.ts
192
+ this.microverse = MicroverseLua.create({ host, surface, defaultTimeoutMs: 30_000 });
193
+
194
+ await this.microverse.mountScriptInstance({
195
+ instanceId: 'promo-1',
196
+ scriptId: 'promotions',
197
+ capabilities: surface.pickCapabilities('orders:read', 'audit:record'),
198
+ props: { label: 'summer-sale' },
199
+ });
200
+
201
+ await this.microverse.emitToAllInstances('OrderPlaced', { orderId, amountCents, customerId });
202
+ ```
203
+
204
+ Map your domain events to `emitToAllInstances` (see `dispatch` in the example).
205
+
206
+ ### 4. Load Lua sources
207
+
208
+ Keep scripts in `.lua` files and register by `scriptId`:
209
+
210
+ ```ts
211
+ import { readComponentLua } from './services/components/loadComponentScript';
212
+
213
+ engine.registerScriptDefinition('order_echo', readComponentLua('components/order_echo.lua'));
214
+ ```
215
+
216
+ Or inline strings for tests. Use **`sharedLuaChunks`** on `create` for libraries shared by every instance (e.g. `lua/lib/math_helpers.lua`), and **`injectLuaChunks`** per definition or mount for one-off preludes.
217
+
218
+ ### 5. Mount instances
80
219
 
81
- ## IDE stubs
220
+ Each instance needs a **capability allowlist** — only declared methods on allowed bridges are callable from Lua:
221
+
222
+ ```ts
223
+ await engine.mountScriptInstance({
224
+ scriptId: 'billing_denied',
225
+ capabilities: surface.pickCapabilities('billing:charge', 'audit:record'),
226
+ props: { maxCents: 1000 },
227
+ });
228
+ ```
229
+
230
+ Optional **`audit`** metadata is passed to script audit callbacks for observability.
231
+
232
+ ### 6. Dispatch events and shut down
233
+
234
+ ```ts
235
+ await engine.emitHook('OrderPlaced', payload);
236
+ // or engine.dispatch(domainEvent)
237
+
238
+ await engine.dispose();
239
+ ```
240
+
241
+ ## Lua authoring
242
+
243
+ ### Component pattern
244
+
245
+ Scripts use the injected **`component`** helper and implement hooks on the returned table:
246
+
247
+ ```lua
248
+ -- examples/business-scripting-engine/lua/components/order_echo.lua
249
+ local C = component:extend()
250
+
251
+ function C:onOrderPlaced(evt)
252
+ self.bridges.notifications:send({
253
+ channel = "echo",
254
+ message = "order:" .. evt.orderId,
255
+ })
256
+ end
257
+ ```
258
+
259
+ - **`component:extend()`** — Builds the instance with `properties`, `state`, and `bridges` for this slot.
260
+ - **Domain events** — Implement `onOrderPlaced`, `onInventoryLow`, … matching `.componentHooks()` on the surface.
261
+ - **Lifecycle** — `init`, `onDestroy`, `onPropsChanged` (see `props_demo.lua`).
262
+
263
+ ### Bridges (scoped, not global)
264
+
265
+ Call host APIs through **`self.bridges.<bridgeName>:<method>(payload)`**:
266
+
267
+ ```lua
268
+ local order = self.bridges.orders:get({ orderId = evt.orderId })
269
+ self.bridges.audit:record({ line = "seen:" .. evt.orderId })
270
+ ```
271
+
272
+ Bridge names match `.bridge('orders')` in TypeScript (camelCase field on `MicroverseBridges` in `.d.lua`).
273
+
274
+ ### Props and state
275
+
276
+ ```lua
277
+ -- examples/business-scripting-engine/lua/components/props_demo.lua
278
+ function C:init()
279
+ self.state = { hits = 0 }
280
+ end
281
+
282
+ function C:onPropsChanged(key, newValue)
283
+ self.state.lastKey = key
284
+ end
285
+
286
+ function C:onOrderPlaced(evt)
287
+ local label = self.properties.label or "?"
288
+ self.state.hits = (self.state.hits or 0) + 1
289
+ end
290
+ ```
291
+
292
+ Host patches props via `setInstanceProps` / `patchInstanceProps` on the microverse.
293
+
294
+ ### Shared Lua libraries
295
+
296
+ | Mechanism | Scope |
297
+ |-----------|--------|
298
+ | `sharedLuaChunks` on `MicroverseLua.create` | Every instance, every mount |
299
+ | `injectLuaChunks` on `registerScriptDefinition` | All mounts of that `scriptId` |
300
+ | `injectLuaChunks` on `mountScriptInstance` | Single instance |
301
+
302
+ Run order: shared → definition → mount preludes → main script chunk.
303
+
304
+ ### Async bridges
305
+
306
+ Mark a handler `async` in TypeScript; Lua may use a completion callback or `:await()` on the returned handle:
307
+
308
+ ```lua
309
+ -- examples/business-scripting-engine/lua/components/order_asyncio_tick.lua
310
+ self.bridges.asyncio:tick({ delayMs = 5, seed = evt.amountCents }, function(r)
311
+ self.bridges.audit:record({ line = "asyncio-value:" .. tostring(r.value) })
312
+ end)
313
+ ```
314
+
315
+ Generated stubs document `AsyncioTickHandle` and `AsyncioTickResult` for LuaLS.
316
+
317
+ ## IDE typing (LuaCATS)
318
+
319
+ Generate stubs from the same surface module that drives runtime:
82
320
 
83
321
  ```bash
84
322
  pnpm add -D @microverse.ts/cli
85
- pnpm exec microverse generate-lua-defs --surface src/mySurface.ts
323
+ pnpm exec microverse generate-lua-defs --surface src/businessSurface.ts
86
324
  ```
87
325
 
88
- Requires `export default` on the surface module (typically the result of `.build()`). Details: [`@microverse.ts/cli`](../cli/README.md).
326
+ The manifest emits **type-only** bridge classes (`---@class Orders` + `---@field get fun()`) so LuaLS does not treat `Orders` as a runtime global. Use:
89
327
 
90
- ## Reference example
328
+ - `self.bridges.orders` — field name (camelCase) on `MicroverseBridges`
329
+ - Types like `Orders`, `OrderDto` — PascalCase classes in the stub file
330
+
331
+ Point LuaLS at the generated folder:
332
+
333
+ ```json
334
+ {
335
+ "workspace.library": ["./generated"],
336
+ "diagnostics.globals": ["component"]
337
+ }
338
+ ```
339
+
340
+ See [`examples/business-scripting-engine/.luarc.json`](../../examples/business-scripting-engine/.luarc.json) and [`generated/businessSurface.d.lua`](../../examples/business-scripting-engine/generated/businessSurface.d.lua).
341
+
342
+ Details: [`@microverse.ts/cli`](../cli/README.md), [`@microverse.ts/lua-defs`](../lua-defs/README.md).
343
+
344
+ ## Security model
91
345
 
92
- [`examples/business-scripting-engine`](../../examples/business-scripting-engine) rules engine with orders, billing, workflow Lua, and `BusinessScriptingEngine` wrapping this package.
346
+ | Layer | Behavior |
347
+ |-------|----------|
348
+ | **Capabilities** | Each bridge method declares `requires`. Calls from Lua are denied unless the instance’s allowlist includes that capability. |
349
+ | **Per-instance allowlist** | `mountScriptInstance({ capabilities: surface.pickCapabilities(…) })` — principle of least privilege per script. |
350
+ | **Host isolation** | The TypeScript `host` object is not injected into Lua; only bridge tables built from the surface are visible (via `self.bridges`). |
351
+ | **Timeouts** | `defaultTimeoutMs` or `defaultTimeout` on `create`; Wasm instruction budget in `@microverse.ts/runtime-wasm`. |
352
+ | **Validation** | Zod validates bridge inputs and outputs at the TS boundary (`@microverse.ts/runtime-zod`). |
93
353
 
94
- ## Related
354
+ ## Related packages
95
355
 
96
- - Async bridges: [`host-surface/docs/async-subroutines-components.md`](../host-surface/docs/async-subroutines-components.md)
97
- - Component-style Lua: [`examples/.../COMPONENT_PATTERN.md`](../../examples/business-scripting-engine/docs/COMPONENT_PATTERN.md)
356
+ | Package | Use when |
357
+ |---------|----------|
358
+ | [`@microverse.ts/host-surface`](../host-surface/README.md) | Surface builder details, `HostScriptSession`, custom slot wiring. |
359
+ | [`@microverse.ts/lua-defs`](../lua-defs/README.md) | Manifest → LuaCATS document (library use). |
360
+ | [`@microverse.ts/cli`](../cli/README.md) | `microverse generate-lua-defs` in CI or locally. |
361
+ | `runtime-wasm`, `runtime-bridge`, `runtime-capabilities` | Advanced runtime customization (usually via host-surface). |
362
+
363
+ ## Reference example
364
+
365
+ [`examples/business-scripting-engine`](../../examples/business-scripting-engine) — orders, billing, notifications, audit, inventory, jobs, component Lua, and `BusinessScriptingEngine` wrapping this package.
366
+
367
+ ```bash
368
+ pnpm --filter @microverse.ts/business-scripting-engine test
369
+ ```
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * Plug-and-play Lua scripting: **{@link MicroverseLua.create}** (built-in Wasm VM) and the fluent
5
5
  * **{@link defineHostSurfaceFor}** builder (`bridge` → `method` → `build`).
6
6
  */
7
- export { augmentHostWithCapabilityRegistry, BridgeBuilder, buildBridgeMergeEnvForHost, collectCapabilitiesFromHostSurfaceSpec, compileHostSurface, compileHostSurfaceFor, createBridgeDeclarationsFromHostSurfaceSpec, defineHostSurface, defineHostSurfaceFor, HostScriptSession, luaGlobalHookName, luaType, MICROVERSE_CAPABILITY_REGISTRY, pickSurfaceCapabilities, SurfaceBuilder, zodToLuaTypeRef, type AnyHostSurfaceMethod, type HostFnContext, type HostScriptSessionOptions, type HostSurface, type HostSurfaceCore, type HostSurfaceMethodEntry, type HostSurfaceSpec, type HostSurfaceSpecForHost, type HostWorkflowHooksSpec, type InferSurfaceCapabilities, type LuaDefManifestGeneratorOpts, type LuaGlobalHookName, type SchemaValidationPort, type SurfaceCapabilityString, type SurfaceMethodDef, type WithMicroverseCapabilityRegistry, type WorkflowHookInvokeArgs, type ZodToLuaTypeRefOptions, } from '@microverse.ts/host-surface';
7
+ export { augmentHostWithCapabilityRegistry, BridgeBuilder, buildBridgeMergeEnvForHost, collectCapabilitiesFromHostSurfaceSpec, compileHostSurface, compileHostSurfaceFor, createBridgeDeclarationsFromHostSurfaceSpec, defineHostSurface, defineHostSurfaceFor, HostScriptSession, luaGlobalHookName, luaType, MICROVERSE_CAPABILITY_REGISTRY, pickSurfaceCapabilities, SurfaceBuilder, zodToLuaTypeRef, type AnyHostSurfaceMethod, type HostFnContext, type HostScriptSessionOptions, type HostSurface, type HostSurfaceCore, type HostSurfaceMethodEntry, type HostSurfaceSpec, type HostSurfaceSpecForHost, type HostComponentHooksSpec, type InferSurfaceCapabilities, type LuaDefManifestGeneratorOpts, type LuaGlobalHookName, type SchemaValidationPort, type SurfaceCapabilityString, type SurfaceMethodDef, type WithMicroverseCapabilityRegistry, type ComponentEventHookInvokeArgs, type ZodToLuaTypeRefOptions, } from '@microverse.ts/host-surface';
8
8
  export * from '@microverse.ts/shared';
9
9
  export * from '@microverse.ts/runtime-core';
10
10
  export * from '@microverse.ts/runtime-lua';
@@ -12,6 +12,6 @@ export * from '@microverse.ts/runtime-wasm';
12
12
  export * from '@microverse.ts/runtime-bridge';
13
13
  export * from '@microverse.ts/runtime-capabilities';
14
14
  export * from '@microverse.ts/runtime-zod';
15
- export { MicroverseLua } from './infrastructure/facade/microverseLuaNamespace.js';
16
- export { createLuaMicroverse, LuaMicroverse, type LuaMicroverseConfig, type InferScriptHooksFromHost, type InferScriptHooksFromSurface, type InferSurfaceCapabilitiesFromSurface, type TaggedLuaMicroverseHost, } from './infrastructure/facade/luaMicroverse.js';
15
+ export { MicroverseLua } from './infrastructure/facade/microverseLuaNamespace';
16
+ export { createLuaMicroverse, LuaMicroverse, type LuaMicroverseConfig, type InferScriptHooksFromHost, type InferScriptHooksFromSurface, type InferSurfaceCapabilitiesFromSurface, type TaggedLuaMicroverseHost, } from './infrastructure/facade/luaMicroverse';
17
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,iCAAiC,EACjC,aAAa,EACb,0BAA0B,EAC1B,sCAAsC,EACtC,kBAAkB,EAClB,qBAAqB,EACrB,2CAA2C,EAC3C,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,OAAO,EACP,8BAA8B,EAC9B,uBAAuB,EACvB,cAAc,EACd,eAAe,EACf,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,wBAAwB,EAC7B,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,EAC7B,KAAK,2BAA2B,EAChC,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,uBAAuB,EAC5B,KAAK,gBAAgB,EACrB,KAAK,gCAAgC,EACrC,KAAK,sBAAsB,EAC3B,KAAK,sBAAsB,GAC5B,MAAM,6BAA6B,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qCAAqC,CAAC;AACpD,cAAc,4BAA4B,CAAC;AAE3C,OAAO,EAA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C7B,KAAK,2BAA2B,EAChC,KAAK,mCAAmC,EACxC,KAAK,uBAAuB,GAC7B,MAAM,uCAAuC,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BridgeBuilder, HostScriptSession, HostScriptSession as HostScriptSession$1, MICROVERSE_CAPABILITY_REGISTRY, SurfaceBuilder, augmentHostWithCapabilityRegistry, buildBridgeMergeEnvForHost, collectCapabilitiesFromHostSurfaceSpec, compileHostSurface, compileHostSurfaceFor, createBridgeDeclarationsFromHostSurfaceSpec, defineHostSurface, defineHostSurfaceFor, luaGlobalHookName, luaGlobalHookName as luaGlobalHookName$1, luaType, pickSurfaceCapabilities, zodToLuaTypeRef } from "@microverse.ts/host-surface";
2
- import { createLuaEnvSlotKey, fixedTimeout } from "@microverse.ts/runtime-core";
2
+ import { createLuaEnvSlotKey, createScriptInstanceContext, fixedTimeout, mergeScriptPropertyBags, resolveLuaScriptSource } from "@microverse.ts/runtime-core";
3
3
  import { createWasmMicroverseRuntime } from "@microverse.ts/runtime-wasm";
4
4
  export * from "@microverse.ts/shared";
5
5
  export * from "@microverse.ts/runtime-core";
@@ -13,87 +13,151 @@ function resolveDefaultTimeout(config) {
13
13
  if (config.defaultTimeout !== void 0) return config.defaultTimeout;
14
14
  if (config.defaultTimeoutMs !== void 0) return fixedTimeout(config.defaultTimeoutMs);
15
15
  }
16
- /**
17
- * One **Lua microverse**: a shared built-in Wasm Lua VM, {@link HostScriptSession}s keyed by `scriptId`, and helpers to
18
- * register scripts and broadcast hook events. Capabilities per script come from the host surface
19
- * ({@link HostSurfaceCore.pickCapabilities} / {@link HostSurfaceCore.capabilities}). Created via {@link MicroverseLua.create}
20
- * or {@link createLuaMicroverse}.
21
- */
22
16
  var LuaMicroverse = class {
23
17
  config;
24
18
  runtime;
25
- sessions = /* @__PURE__ */ new Map();
19
+ definitions = /* @__PURE__ */ new Map();
20
+ instances = /* @__PURE__ */ new Map();
26
21
  host;
27
22
  surface;
28
23
  envSlotScope;
29
24
  defaultTimeout;
30
25
  sharedLuaChunks;
26
+ onScriptAudit;
31
27
  constructor(config) {
32
28
  this.config = config;
33
29
  this.host = config.host;
34
30
  this.surface = config.surface;
35
31
  this.defaultTimeout = resolveDefaultTimeout(config);
36
32
  this.sharedLuaChunks = config.sharedLuaChunks ?? [];
33
+ this.onScriptAudit = config.onScriptAudit;
37
34
  this.runtime = createWasmMicroverseRuntime(this.defaultTimeout !== void 0 ? { defaultTimeout: this.defaultTimeout } : {});
38
35
  this.envSlotScope = config.envSlotScope ?? "script";
39
36
  }
40
- /** Capability ids declared on the bound host surface. */
41
37
  getSurfaceCapabilities = () => this.surface.capabilities;
42
- /**
43
- * Loads one Lua chunk in an isolated env slot; `scriptId` must be unique within this microverse.
44
- *
45
- * @param args.capabilities - Subset of {@link getSurfaceCapabilities}; use `surface.pickCapabilities(…)` at the call site.
46
- */
47
- registerScript = async (args) => {
48
- const { scriptId, script, capabilities, injectLuaChunks } = args;
49
- const preludeChunks = [...this.sharedLuaChunks, ...injectLuaChunks ?? []];
50
- if (this.sessions.has(scriptId)) throw new Error(`script already registered: ${scriptId}`);
38
+ registerScriptDefinition = (def) => {
39
+ if (this.definitions.has(def.scriptId)) throw new Error(`script definition already registered: ${def.scriptId}`);
40
+ this.definitions.set(def.scriptId, def);
41
+ };
42
+ hasScriptDefinition = (scriptId) => this.definitions.has(scriptId);
43
+ getScriptDefinition = (scriptId) => this.definitions.get(scriptId);
44
+ mountScriptInstance = async (args) => {
45
+ const { instanceId, scriptId, capabilities, audit, injectLuaChunks } = args;
46
+ if (this.instances.has(instanceId)) throw new Error(`script instance already mounted: ${instanceId}`);
47
+ let def = this.definitions.get(scriptId);
48
+ if (def === void 0) {
49
+ if (args.script === void 0) throw new Error(`unknown scriptId "${scriptId}" and no inline script provided`);
50
+ def = {
51
+ scriptId,
52
+ source: args.script
53
+ };
54
+ this.definitions.set(scriptId, def);
55
+ } else if (args.script !== void 0) def = {
56
+ ...def,
57
+ source: args.script
58
+ };
59
+ const slotKey = createLuaEnvSlotKey(`${this.envSlotScope}:${instanceId}`);
60
+ const scriptContext = createScriptInstanceContext({
61
+ instanceId,
62
+ scriptId,
63
+ slotKey: String(slotKey),
64
+ audit
65
+ });
51
66
  const session = new HostScriptSession$1({
52
67
  runtime: this.runtime,
53
68
  surface: this.surface,
54
69
  host: this.host,
55
- slotKey: createLuaEnvSlotKey(`${this.envSlotScope}:${scriptId}`),
70
+ slotKey,
56
71
  allowedCapabilities: capabilities,
57
- defaultTimeout: this.defaultTimeout
72
+ defaultTimeout: this.defaultTimeout,
73
+ script: scriptContext,
74
+ propsSchema: def.propsSchema,
75
+ onScriptAudit: this.onScriptAudit
58
76
  });
59
- await session.openSession();
60
- for (const [index, chunk] of preludeChunks.entries()) {
61
- const injected = await session.runChunk(chunk);
62
- if (injected._tag !== "ok") {
63
- await session.dispose();
64
- const detail = injected._tag === "err" && injected.error._tag === "AdapterError" ? injected.error.message : JSON.stringify(injected.error);
65
- const which = index < this.sharedLuaChunks.length ? `shared Lua prelude [${index}]` : `script injectLuaChunks [${index - this.sharedLuaChunks.length}]`;
66
- throw new Error(`script "${scriptId}" failed injecting ${which}: ${detail}`);
77
+ try {
78
+ await session.openSession();
79
+ const preludeChunks = [
80
+ ...this.sharedLuaChunks,
81
+ ...def.injectLuaChunks ?? [],
82
+ ...injectLuaChunks ?? []
83
+ ];
84
+ for (const [index, chunk] of preludeChunks.entries()) {
85
+ const injected = await session.runChunk(chunk);
86
+ if (injected._tag !== "ok") throw new Error(formatRunError(instanceId, `prelude [${index}]`, injected));
67
87
  }
68
- }
69
- const loaded = await session.runChunk(script);
70
- if (loaded._tag !== "ok") {
88
+ const source = await resolveLuaScriptSource(def.source);
89
+ const loaded = await session.runChunk(source);
90
+ if (loaded._tag !== "ok") throw new Error(formatRunError(instanceId, "main chunk", loaded));
91
+ const mergedProps = mergeScriptPropertyBags(def.defaultProps ?? {}, args.props ?? {});
92
+ await session.setProps(mergedProps);
93
+ const initResult = await session.invokeComponentHook("init");
94
+ if (initResult._tag !== "ok") throw new Error(formatRunError(instanceId, "init hook", initResult));
95
+ this.instances.set(instanceId, { session });
96
+ this.onScriptAudit?.({
97
+ kind: "mounted",
98
+ context: scriptContext
99
+ });
100
+ } catch (e) {
71
101
  await session.dispose();
72
- const detail = loaded._tag === "err" && loaded.error._tag === "AdapterError" ? loaded.error.message : JSON.stringify(loaded.error);
73
- throw new Error(`script "${scriptId}" failed to load: ${detail}`);
102
+ const message = e instanceof Error ? e.message : String(e);
103
+ this.onScriptAudit?.({
104
+ kind: "scriptError",
105
+ context: scriptContext,
106
+ phase: "mount",
107
+ message
108
+ });
109
+ throw e;
74
110
  }
75
- this.sessions.set(scriptId, session);
76
111
  };
77
- /** Invokes `on{Kind}` on every registered script with the same payload table (Lua literals only). */
78
- emitToAllScripts = (async (kind, payload) => {
112
+ unmountScriptInstance = async (instanceId) => {
113
+ const mounted = this.instances.get(instanceId);
114
+ if (mounted === void 0) throw new Error(`script instance not mounted: ${instanceId}`);
115
+ const ctx = mounted.session.context;
116
+ await mounted.session.dispose();
117
+ this.instances.delete(instanceId);
118
+ this.onScriptAudit?.({
119
+ kind: "unmounted",
120
+ context: ctx
121
+ });
122
+ };
123
+ setInstanceProps = async (instanceId, bag) => {
124
+ await this.requireInstance(instanceId).setProps(bag);
125
+ };
126
+ patchInstanceProps = async (instanceId, partial) => {
127
+ await this.requireInstance(instanceId).patchProps(partial);
128
+ };
129
+ getInstanceProps = (instanceId) => {
130
+ return this.requireInstance(instanceId).getProps();
131
+ };
132
+ flushInstanceProps = async (instanceId) => {
133
+ return this.requireInstance(instanceId).flushDirtyProps();
134
+ };
135
+ getInstance = (instanceId) => {
136
+ return this.instances.get(instanceId)?.session;
137
+ };
138
+ emitToAllInstances = (async (kind, payload) => {
79
139
  const hook = luaGlobalHookName$1(kind);
80
- for (const [id, session] of this.sessions) {
81
- const r = await session.invokeGlobalHookIfPresent(hook, payload);
140
+ for (const [id, { session }] of this.instances) {
141
+ const r = await session.invokeComponentEventHook(hook, payload);
82
142
  if (r._tag !== "ok") {
83
143
  const detail = r._tag === "err" && r.error._tag === "AdapterError" ? r.error.message : JSON.stringify(r.error);
84
- throw new Error(`script "${id}" failed on emit: ${detail}`);
144
+ throw new Error(`instance "${id}" failed on emit: ${detail}`);
85
145
  }
86
146
  }
87
147
  });
88
148
  dispose = async () => {
89
- for (const s of this.sessions.values()) await s.dispose();
90
- this.sessions.clear();
149
+ for (const id of [...this.instances.keys()]) await this.unmountScriptInstance(id);
150
+ this.instances.clear();
91
151
  };
152
+ requireInstance(instanceId) {
153
+ const mounted = this.instances.get(instanceId);
154
+ if (mounted === void 0) throw new Error(`script instance not mounted: ${instanceId}`);
155
+ return mounted.session;
156
+ }
92
157
  };
93
- /**
94
- * Creates a {@link LuaMicroverse} with a **built-in** Wasm Lua VM (Wasmoon). Type `host` with
95
- * {@link TaggedLuaMicroverseHost} so hook emits narrow correctly.
96
- */
158
+ function formatRunError(instanceId, phase, result) {
159
+ return `instance "${instanceId}" failed ${phase}: ${result._tag === "err" && result.error?._tag === "AdapterError" ? result.error.message : JSON.stringify(result.error)}`;
160
+ }
97
161
  function createLuaMicroverse(config) {
98
162
  return new LuaMicroverse({
99
163
  host: config.host,
@@ -101,7 +165,8 @@ function createLuaMicroverse(config) {
101
165
  envSlotScope: config.envSlotScope,
102
166
  defaultTimeout: config.defaultTimeout,
103
167
  defaultTimeoutMs: config.defaultTimeoutMs,
104
- sharedLuaChunks: config.sharedLuaChunks
168
+ sharedLuaChunks: config.sharedLuaChunks,
169
+ onScriptAudit: config.onScriptAudit
105
170
  });
106
171
  }
107
172
  //#endregion
@@ -119,7 +184,8 @@ function createLuaMicroverse(config) {
119
184
  * surface: mySurface,
120
185
  * defaultTimeoutMs: 30_000,
121
186
  * });
122
- * await microverse.registerScript({ scriptId: 'ai', script: lua, capabilities: surface.pickCapabilities('demo:tick') });
187
+ * microverse.registerScriptDefinition({ scriptId: 'ai', source: lua });
188
+ * await microverse.mountScriptInstance({ instanceId: 'ai', scriptId: 'ai', capabilities: surface.pickCapabilities('demo:tick') });
123
189
  * ```
124
190
  */
125
191
  var MicroverseLua = {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/infrastructure/facade/luaMicroverse.ts","../src/infrastructure/facade/microverseLuaNamespace.ts"],"sourcesContent":["import type { z } from 'zod';\n\nimport {\n HostScriptSession,\n luaGlobalHookName,\n type HostSurface,\n type HostSurfaceCore,\n type HostWorkflowHooksSpec,\n} from '@microverse.ts/host-surface';\nimport type { CapabilityId } from '@microverse.ts/runtime-capabilities';\nimport {\n createLuaEnvSlotKey,\n fixedTimeout,\n type MicroverseRuntime,\n type TimeoutPolicy,\n} from '@microverse.ts/runtime-core';\nimport { createWasmMicroverseRuntime } from '@microverse.ts/runtime-wasm';\n\n/** Phantom key: optional on the host **type** so {@link InferScriptHooksFromHost} can recover hook Zod map typing. Never set at runtime. */\ndeclare const SCRIPT_HOOKS_TYPE: unique symbol;\n\n/**\n * Intersects `TBase` with an optional phantom field carrying `THooks` for {@link LuaMicroverse} / {@link createLuaMicroverse} inference.\n */\nexport type TaggedLuaMicroverseHost<\n THooks extends HostWorkflowHooksSpec,\n TBase = unknown,\n> = TBase & { readonly [SCRIPT_HOOKS_TYPE]?: THooks };\n\nexport type InferScriptHooksFromHost<THost> = THost extends TaggedLuaMicroverseHost<infer H, unknown>\n ? H extends HostWorkflowHooksSpec\n ? H\n : undefined\n : undefined;\n\nexport type InferScriptHooksFromSurface<S extends HostSurfaceCore> =\n S extends HostSurfaceCore<CapabilityId> & { readonly workflowHooks: infer H extends HostWorkflowHooksSpec }\n ? H\n : S extends HostSurface<infer H extends HostWorkflowHooksSpec, CapabilityId>\n ? H\n : undefined;\n\nexport type InferSurfaceCapabilitiesFromSurface<S extends HostSurfaceCore> = S extends HostSurfaceCore<\n infer C extends CapabilityId\n>\n ? C\n : CapabilityId;\n\ntype EffectiveScriptHooks<THost extends object, TSurface extends HostSurfaceCore> = [InferScriptHooksFromHost<THost>] extends [\n undefined,\n]\n ? InferScriptHooksFromSurface<TSurface>\n : InferScriptHooksFromHost<THost>;\n\nexport type LuaMicroverseConfig<\n THost extends object = object,\n THooks extends HostWorkflowHooksSpec | undefined = InferScriptHooksFromHost<THost>,\n TCapabilities extends CapabilityId = CapabilityId,\n> = {\n readonly host: THost;\n /** From {@link defineHostSurface} / {@link defineHostSurfaceFor}; hooks live on `surface.workflowHooks` when present. */\n readonly surface: HostSurface<THooks, TCapabilities>;\n /** Prefix for internal Lua env slot ids, default `script` (ids look like `script:my-id`). */\n readonly envSlotScope?: string | undefined;\n /** Wall-clock limit per `runChunk` / hook invocation (Wasm adapter + session forwarding). */\n readonly defaultTimeout?: TimeoutPolicy | undefined;\n /**\n * Shorthand for {@link defaultTimeout} as `fixedTimeout(ms)`. Ignored when `defaultTimeout` is set.\n */\n readonly defaultTimeoutMs?: number | undefined;\n /**\n * Lua sources run in **every** script slot after {@link HostScriptSession.openSession} and before that\n * script's main chunk. Define shared helpers/libraries here once instead of per {@link registerScript}.\n */\n readonly sharedLuaChunks?: readonly string[] | undefined;\n};\n\nfunction resolveDefaultTimeout<\n THost extends object,\n THooks extends HostWorkflowHooksSpec | undefined,\n TCapabilities extends CapabilityId,\n>(config: LuaMicroverseConfig<THost, THooks, TCapabilities>): TimeoutPolicy | undefined {\n if (config.defaultTimeout !== undefined) {\n return config.defaultTimeout;\n }\n if (config.defaultTimeoutMs !== undefined) {\n return fixedTimeout(config.defaultTimeoutMs);\n }\n return undefined;\n}\n\ntype EmitToAllScriptsFn<THooks extends HostWorkflowHooksSpec | undefined> = THooks extends HostWorkflowHooksSpec\n ? <const K extends keyof THooks & string>(kind: K, payload: Readonly<z.infer<THooks[K]>>) => Promise<void>\n : (\n kind: string,\n payload: Readonly<Record<string, string | number | boolean>>,\n ) => Promise<void>;\n\n/**\n * One **Lua microverse**: a shared built-in Wasm Lua VM, {@link HostScriptSession}s keyed by `scriptId`, and helpers to\n * register scripts and broadcast hook events. Capabilities per script come from the host surface\n * ({@link HostSurfaceCore.pickCapabilities} / {@link HostSurfaceCore.capabilities}). Created via {@link MicroverseLua.create}\n * or {@link createLuaMicroverse}.\n */\nexport class LuaMicroverse<\n THost extends object = object,\n THooks extends HostWorkflowHooksSpec | undefined = InferScriptHooksFromHost<THost>,\n TCapabilities extends CapabilityId = CapabilityId,\n> {\n private readonly runtime: MicroverseRuntime;\n\n private readonly sessions = new Map<string, HostScriptSession<THost, THooks>>();\n\n private readonly host: THost;\n\n private readonly surface: HostSurface<THooks, TCapabilities>;\n\n private readonly envSlotScope: string;\n\n private readonly defaultTimeout: TimeoutPolicy | undefined;\n\n private readonly sharedLuaChunks: readonly string[];\n\n constructor(private readonly config: LuaMicroverseConfig<THost, THooks, TCapabilities>) {\n this.host = config.host;\n this.surface = config.surface;\n this.defaultTimeout = resolveDefaultTimeout(config);\n this.sharedLuaChunks = config.sharedLuaChunks ?? [];\n this.runtime = createWasmMicroverseRuntime(\n this.defaultTimeout !== undefined ? { defaultTimeout: this.defaultTimeout } : {},\n );\n this.envSlotScope = config.envSlotScope ?? 'script';\n }\n\n /** Capability ids declared on the bound host surface. */\n readonly getSurfaceCapabilities = (): readonly TCapabilities[] => this.surface.capabilities;\n\n /**\n * Loads one Lua chunk in an isolated env slot; `scriptId` must be unique within this microverse.\n *\n * @param args.capabilities - Subset of {@link getSurfaceCapabilities}; use `surface.pickCapabilities(…)` at the call site.\n */\n readonly registerScript = async (args: {\n readonly scriptId: string;\n readonly script: string;\n readonly capabilities: readonly TCapabilities[];\n readonly injectLuaChunks?: readonly string[] | undefined;\n }): Promise<void> => {\n const { scriptId, script, capabilities, injectLuaChunks } = args;\n const preludeChunks = [...this.sharedLuaChunks, ...(injectLuaChunks ?? [])];\n if (this.sessions.has(scriptId)) {\n throw new Error(`script already registered: ${scriptId}`);\n }\n const session = new HostScriptSession<THost, THooks>({\n runtime: this.runtime,\n surface: this.surface,\n host: this.host,\n slotKey: createLuaEnvSlotKey(`${this.envSlotScope}:${scriptId}`),\n allowedCapabilities: capabilities,\n defaultTimeout: this.defaultTimeout,\n });\n await session.openSession();\n for (const [index, chunk] of preludeChunks.entries()) {\n const injected = await session.runChunk(chunk);\n if (injected._tag !== 'ok') {\n await session.dispose();\n const detail =\n injected._tag === 'err' && injected.error._tag === 'AdapterError'\n ? injected.error.message\n : JSON.stringify(injected.error);\n const which =\n index < this.sharedLuaChunks.length\n ? `shared Lua prelude [${index}]`\n : `script injectLuaChunks [${index - this.sharedLuaChunks.length}]`;\n throw new Error(`script \"${scriptId}\" failed injecting ${which}: ${detail}`);\n }\n }\n const loaded = await session.runChunk(script);\n if (loaded._tag !== 'ok') {\n await session.dispose();\n const detail =\n loaded._tag === 'err' && loaded.error._tag === 'AdapterError'\n ? loaded.error.message\n : JSON.stringify(loaded.error);\n throw new Error(`script \"${scriptId}\" failed to load: ${detail}`);\n }\n this.sessions.set(scriptId, session);\n };\n\n /** Invokes `on{Kind}` on every registered script with the same payload table (Lua literals only). */\n readonly emitToAllScripts = (async (\n kind: string,\n payload: Readonly<Record<string, string | number | boolean>>,\n ) => {\n const hook = luaGlobalHookName(kind);\n for (const [id, session] of this.sessions) {\n const r = await session.invokeGlobalHookIfPresent(hook, payload);\n if (r._tag !== 'ok') {\n const detail =\n r._tag === 'err' && r.error._tag === 'AdapterError' ? r.error.message : JSON.stringify(r.error);\n throw new Error(`script \"${id}\" failed on emit: ${detail}`);\n }\n }\n }) as EmitToAllScriptsFn<THooks>;\n\n readonly dispose = async (): Promise<void> => {\n for (const s of this.sessions.values()) {\n await s.dispose();\n }\n this.sessions.clear();\n };\n}\n\n/**\n * Creates a {@link LuaMicroverse} with a **built-in** Wasm Lua VM (Wasmoon). Type `host` with\n * {@link TaggedLuaMicroverseHost} so hook emits narrow correctly.\n */\nexport function createLuaMicroverse<\n THost extends object,\n const TSurface extends HostSurfaceCore<CapabilityId>,\n>(config: {\n readonly host: THost;\n readonly surface: TSurface;\n readonly envSlotScope?: string | undefined;\n readonly defaultTimeout?: TimeoutPolicy | undefined;\n readonly defaultTimeoutMs?: number | undefined;\n readonly sharedLuaChunks?: readonly string[] | undefined;\n}): LuaMicroverse<THost, EffectiveScriptHooks<THost, TSurface>, InferSurfaceCapabilitiesFromSurface<TSurface>> {\n type H = EffectiveScriptHooks<THost, TSurface>;\n type C = InferSurfaceCapabilitiesFromSurface<TSurface>;\n return new LuaMicroverse<THost, H, C>({\n host: config.host,\n surface: config.surface as unknown as HostSurface<H, C>,\n envSlotScope: config.envSlotScope,\n defaultTimeout: config.defaultTimeout,\n defaultTimeoutMs: config.defaultTimeoutMs,\n sharedLuaChunks: config.sharedLuaChunks,\n });\n}\n","import { createLuaMicroverse } from './luaMicroverse.js';\n\n/**\n * Plug-and-play **Lua microverse** entry point.\n *\n * A Wasmoon-backed VM is always created for you — pass only `host`, `surface`, and optional timeouts /\n * shared Lua preludes. Define bridges with {@link defineHostSurfaceFor} (`bridge` → `method` → `build`).\n *\n * @example\n * ```ts\n * const microverse = MicroverseLua.create({\n * host: myHost,\n * surface: mySurface,\n * defaultTimeoutMs: 30_000,\n * });\n * await microverse.registerScript({ scriptId: 'ai', script: lua, capabilities: surface.pickCapabilities('demo:tick') });\n * ```\n */\nexport const MicroverseLua = {\n /** Creates a {@link LuaMicroverse} with a built-in Wasm Lua runtime. */\n create: createLuaMicroverse,\n} as const;\n"],"mappings":";;;;;;;;;;;AA6EA,SAAS,sBAIP,QAAsF;CACtF,IAAI,OAAO,mBAAmB,KAAA,GAC5B,OAAO,OAAO;CAEhB,IAAI,OAAO,qBAAqB,KAAA,GAC9B,OAAO,aAAa,OAAO,gBAAgB;AAG/C;;;;;;;AAeA,IAAa,gBAAb,MAIE;CAe6B;CAd7B;CAEA,2BAA4B,IAAI,IAA8C;CAE9E;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAA4E;EAA3D,KAAA,SAAA;EAC3B,KAAK,OAAO,OAAO;EACnB,KAAK,UAAU,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MAAM,SACJ,OAAO,SAAS,SAAS,OAAO,MAAM,SAAS,iBAC3C,OAAO,MAAM,UACb,KAAK,UAAU,OAAO,KAAK;GACjC,MAAM,IAAI,MAAM,WAAW,SAAS,oBAAoB,QAAQ;EAClE;EACA,KAAK,SAAS,IAAI,UAAU,OAAO;CACrC;;CAGA,oBAA6B,OAC3B,MACA,YACG;EACH,MAAM,OAAO,oBAAkB,IAAI;EACnC,KAAK,MAAM,CAAC,IAAI,YAAY,KAAK,UAAU;GACzC,MAAM,IAAI,MAAM,QAAQ,0BAA0B,MAAM,OAAO;GAC/D,IAAI,EAAE,SAAS,MAAM;IACnB,MAAM,SACJ,EAAE,SAAS,SAAS,EAAE,MAAM,SAAS,iBAAiB,EAAE,MAAM,UAAU,KAAK,UAAU,EAAE,KAAK;IAChG,MAAM,IAAI,MAAM,WAAW,GAAG,oBAAoB,QAAQ;GAC5D;EACF;CACF;CAEA,UAAmB,YAA2B;EAC5C,KAAK,MAAM,KAAK,KAAK,SAAS,OAAO,GACnC,MAAM,EAAE,QAAQ;EAElB,KAAK,SAAS,MAAM;CACtB;AACF;;;;;AAMA,SAAgB,oBAGd,QAO6G;CAG7G,OAAO,IAAI,cAA2B;EACpC,MAAM,OAAO;EACb,SAAS,OAAO;EAChB,cAAc,OAAO;EACrB,gBAAgB,OAAO;EACvB,kBAAkB,OAAO;EACzB,iBAAiB,OAAO;CAC1B,CAAC;AACH;;;;;;;;;;;;;;;;;;;AC5NA,IAAa,gBAAgB;;AAE3B,QAAQ,oBACV"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/infrastructure/facade/luaMicroverse.ts","../src/infrastructure/facade/microverseLuaNamespace.ts"],"sourcesContent":["import type { z } from 'zod';\n\nimport {\n HostScriptSession,\n luaGlobalHookName,\n type HostSurface,\n type HostSurfaceCore,\n type HostComponentHooksSpec,\n} from '@microverse.ts/host-surface';\nimport type { CapabilityId } from '@microverse.ts/runtime-capabilities';\nimport {\n createLuaEnvSlotKey,\n createScriptInstanceContext,\n fixedTimeout,\n mergeScriptPropertyBags,\n resolveLuaScriptSource,\n type LuaScriptDefinition,\n type MicroverseRuntime,\n type ScriptAuditEvent,\n type ScriptPropertyBag,\n type TimeoutPolicy,\n} from '@microverse.ts/runtime-core';\nimport { createWasmMicroverseRuntime } from '@microverse.ts/runtime-wasm';\n\n/** Phantom key: optional on the host **type** so {@link InferScriptHooksFromHost} can recover hook Zod map typing. Never set at runtime. */\ndeclare const SCRIPT_HOOKS_TYPE: unique symbol;\n\nexport type TaggedLuaMicroverseHost<\n THooks extends HostComponentHooksSpec,\n TBase = unknown,\n> = TBase & { readonly [SCRIPT_HOOKS_TYPE]?: THooks };\n\nexport type InferScriptHooksFromHost<THost> = THost extends TaggedLuaMicroverseHost<infer H, unknown>\n ? H extends HostComponentHooksSpec\n ? H\n : undefined\n : undefined;\n\nexport type InferScriptHooksFromSurface<S extends HostSurfaceCore> =\n S extends HostSurfaceCore<CapabilityId> & { readonly componentHooks: infer H extends HostComponentHooksSpec }\n ? H\n : S extends HostSurface<infer H extends HostComponentHooksSpec, CapabilityId>\n ? H\n : undefined;\n\nexport type InferSurfaceCapabilitiesFromSurface<S extends HostSurfaceCore> = S extends HostSurfaceCore<\n infer C extends CapabilityId\n>\n ? C\n : CapabilityId;\n\ntype EffectiveScriptHooks<THost extends object, TSurface extends HostSurfaceCore> = [InferScriptHooksFromHost<THost>] extends [\n undefined,\n]\n ? InferScriptHooksFromSurface<TSurface>\n : InferScriptHooksFromHost<THost>;\n\nexport type LuaMicroverseConfig<\n THost extends object = object,\n THooks extends HostComponentHooksSpec | undefined = InferScriptHooksFromHost<THost>,\n TCapabilities extends CapabilityId = CapabilityId,\n> = {\n readonly host: THost;\n readonly surface: HostSurface<THooks, TCapabilities>;\n readonly envSlotScope?: string | undefined;\n readonly defaultTimeout?: TimeoutPolicy | undefined;\n readonly defaultTimeoutMs?: number | undefined;\n readonly sharedLuaChunks?: readonly string[] | undefined;\n readonly onScriptAudit?: ((event: ScriptAuditEvent) => void) | undefined;\n};\n\nfunction resolveDefaultTimeout<\n THost extends object,\n THooks extends HostComponentHooksSpec | undefined,\n TCapabilities extends CapabilityId,\n>(config: LuaMicroverseConfig<THost, THooks, TCapabilities>): TimeoutPolicy | undefined {\n if (config.defaultTimeout !== undefined) {\n return config.defaultTimeout;\n }\n if (config.defaultTimeoutMs !== undefined) {\n return fixedTimeout(config.defaultTimeoutMs);\n }\n return undefined;\n}\n\ntype EmitToAllInstancesFn<THooks extends HostComponentHooksSpec | undefined> = THooks extends HostComponentHooksSpec\n ? <const K extends keyof THooks & string>(kind: K, payload: Readonly<z.infer<THooks[K]>>) => Promise<void>\n : (\n kind: string,\n payload: Readonly<Record<string, string | number | boolean>>,\n ) => Promise<void>;\n\ntype MountedInstance = {\n readonly session: HostScriptSession<unknown, HostComponentHooksSpec | undefined>;\n};\n\nexport class LuaMicroverse<\n THost extends object = object,\n THooks extends HostComponentHooksSpec | undefined = InferScriptHooksFromHost<THost>,\n TCapabilities extends CapabilityId = CapabilityId,\n> {\n private readonly runtime: MicroverseRuntime;\n\n private readonly definitions = new Map<string, LuaScriptDefinition>();\n\n private readonly instances = new Map<string, MountedInstance>();\n\n private readonly host: THost;\n\n private readonly surface: HostSurface<THooks, TCapabilities>;\n\n private readonly envSlotScope: string;\n\n private readonly defaultTimeout: TimeoutPolicy | undefined;\n\n private readonly sharedLuaChunks: readonly string[];\n\n private readonly onScriptAudit: ((event: ScriptAuditEvent) => void) | undefined;\n\n constructor(private readonly config: LuaMicroverseConfig<THost, THooks, TCapabilities>) {\n this.host = config.host;\n this.surface = config.surface;\n this.defaultTimeout = resolveDefaultTimeout(config);\n this.sharedLuaChunks = config.sharedLuaChunks ?? [];\n this.onScriptAudit = config.onScriptAudit;\n this.runtime = createWasmMicroverseRuntime(\n this.defaultTimeout !== undefined ? { defaultTimeout: this.defaultTimeout } : {},\n );\n this.envSlotScope = config.envSlotScope ?? 'script';\n }\n\n readonly getSurfaceCapabilities = (): readonly TCapabilities[] => this.surface.capabilities;\n\n readonly registerScriptDefinition = (def: LuaScriptDefinition): void => {\n if (this.definitions.has(def.scriptId)) {\n throw new Error(`script definition already registered: ${def.scriptId}`);\n }\n this.definitions.set(def.scriptId, def);\n };\n\n readonly hasScriptDefinition = (scriptId: string): boolean => this.definitions.has(scriptId);\n\n readonly getScriptDefinition = (scriptId: string): LuaScriptDefinition | undefined =>\n this.definitions.get(scriptId);\n\n readonly mountScriptInstance = async (args: {\n readonly instanceId: string;\n readonly scriptId: string;\n readonly script?: string | undefined;\n readonly props?: ScriptPropertyBag | undefined;\n readonly capabilities: readonly TCapabilities[];\n readonly audit?: Readonly<Record<string, string | number | boolean>> | undefined;\n readonly injectLuaChunks?: readonly string[] | undefined;\n }): Promise<void> => {\n const { instanceId, scriptId, capabilities, audit, injectLuaChunks } = args;\n if (this.instances.has(instanceId)) {\n throw new Error(`script instance already mounted: ${instanceId}`);\n }\n\n let def = this.definitions.get(scriptId);\n if (def === undefined) {\n if (args.script === undefined) {\n throw new Error(`unknown scriptId \"${scriptId}\" and no inline script provided`);\n }\n def = {\n scriptId,\n source: args.script,\n };\n this.definitions.set(scriptId, def);\n } else if (args.script !== undefined) {\n def = { ...def, source: args.script };\n }\n\n const slotKey = createLuaEnvSlotKey(`${this.envSlotScope}:${instanceId}`);\n const scriptContext = createScriptInstanceContext({\n instanceId,\n scriptId,\n slotKey: String(slotKey),\n audit,\n });\n\n const session = new HostScriptSession<THost, THooks>({\n runtime: this.runtime,\n surface: this.surface,\n host: this.host,\n slotKey,\n allowedCapabilities: capabilities,\n defaultTimeout: this.defaultTimeout,\n script: scriptContext,\n propsSchema: def.propsSchema,\n onScriptAudit: this.onScriptAudit,\n });\n\n try {\n await session.openSession();\n const preludeChunks = [\n ...this.sharedLuaChunks,\n ...(def.injectLuaChunks ?? []),\n ...(injectLuaChunks ?? []),\n ];\n for (const [index, chunk] of preludeChunks.entries()) {\n const injected = await session.runChunk(chunk);\n if (injected._tag !== 'ok') {\n throw new Error(formatRunError(instanceId, `prelude [${index}]`, injected));\n }\n }\n\n const source = await resolveLuaScriptSource(def.source);\n const loaded = await session.runChunk(source);\n if (loaded._tag !== 'ok') {\n throw new Error(formatRunError(instanceId, 'main chunk', loaded));\n }\n\n const mergedProps = mergeScriptPropertyBags(def.defaultProps ?? {}, args.props ?? {});\n await session.setProps(mergedProps);\n const initResult = await session.invokeComponentHook('init');\n if (initResult._tag !== 'ok') {\n throw new Error(formatRunError(instanceId, 'init hook', initResult));\n }\n\n this.instances.set(instanceId, { session });\n this.onScriptAudit?.({ kind: 'mounted', context: scriptContext });\n } catch (e) {\n await session.dispose();\n const message = e instanceof Error ? e.message : String(e);\n this.onScriptAudit?.({\n kind: 'scriptError',\n context: scriptContext,\n phase: 'mount',\n message,\n });\n throw e;\n }\n };\n\n readonly unmountScriptInstance = async (instanceId: string): Promise<void> => {\n const mounted = this.instances.get(instanceId);\n if (mounted === undefined) {\n throw new Error(`script instance not mounted: ${instanceId}`);\n }\n const ctx = mounted.session.context;\n await mounted.session.dispose();\n this.instances.delete(instanceId);\n this.onScriptAudit?.({ kind: 'unmounted', context: ctx });\n };\n\n readonly setInstanceProps = async (instanceId: string, bag: ScriptPropertyBag): Promise<void> => {\n const session = this.requireInstance(instanceId);\n await session.setProps(bag);\n };\n\n readonly patchInstanceProps = async (\n instanceId: string,\n partial: ScriptPropertyBag,\n ): Promise<void> => {\n const session = this.requireInstance(instanceId);\n await session.patchProps(partial);\n };\n\n readonly getInstanceProps = (instanceId: string): Readonly<ScriptPropertyBag> => {\n return this.requireInstance(instanceId).getProps();\n };\n\n readonly flushInstanceProps = async (instanceId: string): Promise<ScriptPropertyBag | null> => {\n return this.requireInstance(instanceId).flushDirtyProps();\n };\n\n readonly getInstance = (instanceId: string): HostScriptSession<THost, THooks> | undefined => {\n return this.instances.get(instanceId)?.session as HostScriptSession<THost, THooks> | undefined;\n };\n\n readonly emitToAllInstances = (async (\n kind: string,\n payload: Readonly<Record<string, string | number | boolean>>,\n ) => {\n const hook = luaGlobalHookName(kind);\n for (const [id, { session }] of this.instances) {\n const r = await session.invokeComponentEventHook(hook, payload);\n if (r._tag !== 'ok') {\n const detail =\n r._tag === 'err' && r.error._tag === 'AdapterError' ? r.error.message : JSON.stringify(r.error);\n throw new Error(`instance \"${id}\" failed on emit: ${detail}`);\n }\n }\n }) as EmitToAllInstancesFn<THooks>;\n\n readonly dispose = async (): Promise<void> => {\n for (const id of [...this.instances.keys()]) {\n await this.unmountScriptInstance(id);\n }\n this.instances.clear();\n };\n\n private requireInstance(instanceId: string): HostScriptSession<THost, THooks> {\n const mounted = this.instances.get(instanceId);\n if (mounted === undefined) {\n throw new Error(`script instance not mounted: ${instanceId}`);\n }\n return mounted.session as HostScriptSession<THost, THooks>;\n }\n}\n\nfunction formatRunError(\n instanceId: string,\n phase: string,\n result: { _tag: string; error?: { _tag?: string; message?: string } },\n): string {\n const detail =\n result._tag === 'err' && result.error?._tag === 'AdapterError'\n ? result.error.message\n : JSON.stringify(result.error);\n return `instance \"${instanceId}\" failed ${phase}: ${detail}`;\n}\n\nexport function createLuaMicroverse<\n THost extends object,\n const TSurface extends HostSurfaceCore<CapabilityId>,\n>(config: {\n readonly host: THost;\n readonly surface: TSurface;\n readonly envSlotScope?: string | undefined;\n readonly defaultTimeout?: TimeoutPolicy | undefined;\n readonly defaultTimeoutMs?: number | undefined;\n readonly sharedLuaChunks?: readonly string[] | undefined;\n readonly onScriptAudit?: ((event: ScriptAuditEvent) => void) | undefined;\n}): LuaMicroverse<THost, EffectiveScriptHooks<THost, TSurface>, InferSurfaceCapabilitiesFromSurface<TSurface>> {\n type H = EffectiveScriptHooks<THost, TSurface>;\n type C = InferSurfaceCapabilitiesFromSurface<TSurface>;\n return new LuaMicroverse<THost, H, C>({\n host: config.host,\n surface: config.surface as unknown as HostSurface<H, C>,\n envSlotScope: config.envSlotScope,\n defaultTimeout: config.defaultTimeout,\n defaultTimeoutMs: config.defaultTimeoutMs,\n sharedLuaChunks: config.sharedLuaChunks,\n onScriptAudit: config.onScriptAudit,\n });\n}\n","import { createLuaMicroverse } from './luaMicroverse';\n\n/**\n * Plug-and-play **Lua microverse** entry point.\n *\n * A Wasmoon-backed VM is always created for you — pass only `host`, `surface`, and optional timeouts /\n * shared Lua preludes. Define bridges with {@link defineHostSurfaceFor} (`bridge` → `method` → `build`).\n *\n * @example\n * ```ts\n * const microverse = MicroverseLua.create({\n * host: myHost,\n * surface: mySurface,\n * defaultTimeoutMs: 30_000,\n * });\n * microverse.registerScriptDefinition({ scriptId: 'ai', source: lua });\n * await microverse.mountScriptInstance({ instanceId: 'ai', scriptId: 'ai', capabilities: surface.pickCapabilities('demo:tick') });\n * ```\n */\nexport const MicroverseLua = {\n /** Creates a {@link LuaMicroverse} with a built-in Wasm Lua runtime. */\n create: createLuaMicroverse,\n} as const;\n"],"mappings":";;;;;;;;;;;AAuEA,SAAS,sBAIP,QAAsF;CACtF,IAAI,OAAO,mBAAmB,KAAA,GAC5B,OAAO,OAAO;CAEhB,IAAI,OAAO,qBAAqB,KAAA,GAC9B,OAAO,aAAa,OAAO,gBAAgB;AAG/C;AAaA,IAAa,gBAAb,MAIE;CAmB6B;CAlB7B;CAEA,8BAA+B,IAAI,IAAiC;CAEpE,4BAA6B,IAAI,IAA6B;CAE9D;CAEA;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,QAA4E;EAA3D,KAAA,SAAA;EAC3B,KAAK,OAAO,OAAO;EACnB,KAAK,UAAU,OAAO;EACtB,KAAK,iBAAiB,sBAAsB,MAAM;EAClD,KAAK,kBAAkB,OAAO,mBAAmB,CAAC;EAClD,KAAK,gBAAgB,OAAO;EAC5B,KAAK,UAAU,4BACb,KAAK,mBAAmB,KAAA,IAAY,EAAE,gBAAgB,KAAK,eAAe,IAAI,CAAC,CACjF;EACA,KAAK,eAAe,OAAO,gBAAgB;CAC7C;CAEA,+BAAkE,KAAK,QAAQ;CAE/E,4BAAqC,QAAmC;EACtE,IAAI,KAAK,YAAY,IA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;EACrB,gBAAgB,OAAO;EACvB,kBAAkB,OAAO;EACzB,iBAAiB,OAAO;EACxB,eAAe,OAAO;CACxB,CAAC;AACH;;;;;;;;;;;;;;;;;;;;AC9TA,IAAa,gBAAgB;;AAE3B,QAAQ,oBACV"}
@@ -1,79 +1,65 @@
1
1
  import { z } from 'zod';
2
- import { HostSurface, HostSurfaceCore, HostWorkflowHooksSpec } from '@microverse.ts/host-surface';
2
+ import { HostScriptSession, HostSurface, HostSurfaceCore, HostComponentHooksSpec } from '@microverse.ts/host-surface';
3
3
  import { CapabilityId } from '@microverse.ts/runtime-capabilities';
4
- import { TimeoutPolicy } from '@microverse.ts/runtime-core';
4
+ import { LuaScriptDefinition, ScriptAuditEvent, ScriptPropertyBag, TimeoutPolicy } from '@microverse.ts/runtime-core';
5
5
  /** Phantom key: optional on the host **type** so {@link InferScriptHooksFromHost} can recover hook Zod map typing. Never set at runtime. */
6
6
  declare const SCRIPT_HOOKS_TYPE: unique symbol;
7
- /**
8
- * Intersects `TBase` with an optional phantom field carrying `THooks` for {@link LuaMicroverse} / {@link createLuaMicroverse} inference.
9
- */
10
- export type TaggedLuaMicroverseHost<THooks extends HostWorkflowHooksSpec, TBase = unknown> = TBase & {
7
+ export type TaggedLuaMicroverseHost<THooks extends HostComponentHooksSpec, TBase = unknown> = TBase & {
11
8
  readonly [SCRIPT_HOOKS_TYPE]?: THooks;
12
9
  };
13
- export type InferScriptHooksFromHost<THost> = THost extends TaggedLuaMicroverseHost<infer H, unknown> ? H extends HostWorkflowHooksSpec ? H : undefined : undefined;
10
+ export type InferScriptHooksFromHost<THost> = THost extends TaggedLuaMicroverseHost<infer H, unknown> ? H extends HostComponentHooksSpec ? H : undefined : undefined;
14
11
  export type InferScriptHooksFromSurface<S extends HostSurfaceCore> = S extends HostSurfaceCore<CapabilityId> & {
15
- readonly workflowHooks: infer H extends HostWorkflowHooksSpec;
16
- } ? H : S extends HostSurface<infer H extends HostWorkflowHooksSpec, CapabilityId> ? H : undefined;
12
+ readonly componentHooks: infer H extends HostComponentHooksSpec;
13
+ } ? H : S extends HostSurface<infer H extends HostComponentHooksSpec, CapabilityId> ? H : undefined;
17
14
  export type InferSurfaceCapabilitiesFromSurface<S extends HostSurfaceCore> = S extends HostSurfaceCore<infer C extends CapabilityId> ? C : CapabilityId;
18
15
  type EffectiveScriptHooks<THost extends object, TSurface extends HostSurfaceCore> = [InferScriptHooksFromHost<THost>] extends [
19
16
  undefined
20
17
  ] ? InferScriptHooksFromSurface<TSurface> : InferScriptHooksFromHost<THost>;
21
- export type LuaMicroverseConfig<THost extends object = object, THooks extends HostWorkflowHooksSpec | undefined = InferScriptHooksFromHost<THost>, TCapabilities extends CapabilityId = CapabilityId> = {
18
+ export type LuaMicroverseConfig<THost extends object = object, THooks extends HostComponentHooksSpec | undefined = InferScriptHooksFromHost<THost>, TCapabilities extends CapabilityId = CapabilityId> = {
22
19
  readonly host: THost;
23
- /** From {@link defineHostSurface} / {@link defineHostSurfaceFor}; hooks live on `surface.workflowHooks` when present. */
24
20
  readonly surface: HostSurface<THooks, TCapabilities>;
25
- /** Prefix for internal Lua env slot ids, default `script` (ids look like `script:my-id`). */
26
21
  readonly envSlotScope?: string | undefined;
27
- /** Wall-clock limit per `runChunk` / hook invocation (Wasm adapter + session forwarding). */
28
22
  readonly defaultTimeout?: TimeoutPolicy | undefined;
29
- /**
30
- * Shorthand for {@link defaultTimeout} as `fixedTimeout(ms)`. Ignored when `defaultTimeout` is set.
31
- */
32
23
  readonly defaultTimeoutMs?: number | undefined;
33
- /**
34
- * Lua sources run in **every** script slot after {@link HostScriptSession.openSession} and before that
35
- * script's main chunk. Define shared helpers/libraries here once instead of per {@link registerScript}.
36
- */
37
24
  readonly sharedLuaChunks?: readonly string[] | undefined;
25
+ readonly onScriptAudit?: ((event: ScriptAuditEvent) => void) | undefined;
38
26
  };
39
- type EmitToAllScriptsFn<THooks extends HostWorkflowHooksSpec | undefined> = THooks extends HostWorkflowHooksSpec ? <const K extends keyof THooks & string>(kind: K, payload: Readonly<z.infer<THooks[K]>>) => Promise<void> : (kind: string, payload: Readonly<Record<string, string | number | boolean>>) => Promise<void>;
40
- /**
41
- * One **Lua microverse**: a shared built-in Wasm Lua VM, {@link HostScriptSession}s keyed by `scriptId`, and helpers to
42
- * register scripts and broadcast hook events. Capabilities per script come from the host surface
43
- * ({@link HostSurfaceCore.pickCapabilities} / {@link HostSurfaceCore.capabilities}). Created via {@link MicroverseLua.create}
44
- * or {@link createLuaMicroverse}.
45
- */
46
- export declare class LuaMicroverse<THost extends object = object, THooks extends HostWorkflowHooksSpec | undefined = InferScriptHooksFromHost<THost>, TCapabilities extends CapabilityId = CapabilityId> {
27
+ type EmitToAllInstancesFn<THooks extends HostComponentHooksSpec | undefined> = THooks extends HostComponentHooksSpec ? <const K extends keyof THooks & string>(kind: K, payload: Readonly<z.infer<THooks[K]>>) => Promise<void> : (kind: string, payload: Readonly<Record<string, string | number | boolean>>) => Promise<void>;
28
+ export declare class LuaMicroverse<THost extends object = object, THooks extends HostComponentHooksSpec | undefined = InferScriptHooksFromHost<THost>, TCapabilities extends CapabilityId = CapabilityId> {
47
29
  private readonly config;
48
30
  private readonly runtime;
49
- private readonly sessions;
31
+ private readonly definitions;
32
+ private readonly instances;
50
33
  private readonly host;
51
34
  private readonly surface;
52
35
  private readonly envSlotScope;
53
36
  private readonly defaultTimeout;
54
37
  private readonly sharedLuaChunks;
38
+ private readonly onScriptAudit;
55
39
  constructor(config: LuaMicroverseConfig<THost, THooks, TCapabilities>);
56
- /** Capability ids declared on the bound host surface. */
57
40
  readonly getSurfaceCapabilities: () => readonly TCapabilities[];
58
- /**
59
- * Loads one Lua chunk in an isolated env slot; `scriptId` must be unique within this microverse.
60
- *
61
- * @param args.capabilities - Subset of {@link getSurfaceCapabilities}; use `surface.pickCapabilities(…)` at the call site.
62
- */
63
- readonly registerScript: (args: {
41
+ readonly registerScriptDefinition: (def: LuaScriptDefinition) => void;
42
+ readonly hasScriptDefinition: (scriptId: string) => boolean;
43
+ readonly getScriptDefinition: (scriptId: string) => LuaScriptDefinition | undefined;
44
+ readonly mountScriptInstance: (args: {
45
+ readonly instanceId: string;
64
46
  readonly scriptId: string;
65
- readonly script: string;
47
+ readonly script?: string | undefined;
48
+ readonly props?: ScriptPropertyBag | undefined;
66
49
  readonly capabilities: readonly TCapabilities[];
50
+ readonly audit?: Readonly<Record<string, string | number | boolean>> | undefined;
67
51
  readonly injectLuaChunks?: readonly string[] | undefined;
68
52
  }) => Promise<void>;
69
- /** Invokes `on{Kind}` on every registered script with the same payload table (Lua literals only). */
70
- readonly emitToAllScripts: EmitToAllScriptsFn<THooks>;
53
+ readonly unmountScriptInstance: (instanceId: string) => Promise<void>;
54
+ readonly setInstanceProps: (instanceId: string, bag: ScriptPropertyBag) => Promise<void>;
55
+ readonly patchInstanceProps: (instanceId: string, partial: ScriptPropertyBag) => Promise<void>;
56
+ readonly getInstanceProps: (instanceId: string) => Readonly<ScriptPropertyBag>;
57
+ readonly flushInstanceProps: (instanceId: string) => Promise<ScriptPropertyBag | null>;
58
+ readonly getInstance: (instanceId: string) => HostScriptSession<THost, THooks> | undefined;
59
+ readonly emitToAllInstances: EmitToAllInstancesFn<THooks>;
71
60
  readonly dispose: () => Promise<void>;
61
+ private requireInstance;
72
62
  }
73
- /**
74
- * Creates a {@link LuaMicroverse} with a **built-in** Wasm Lua VM (Wasmoon). Type `host` with
75
- * {@link TaggedLuaMicroverseHost} so hook emits narrow correctly.
76
- */
77
63
  export declare function createLuaMicroverse<THost extends object, const TSurface extends HostSurfaceCore<CapabilityId>>(config: {
78
64
  readonly host: THost;
79
65
  readonly surface: TSurface;
@@ -81,6 +67,7 @@ export declare function createLuaMicroverse<THost extends object, const TSurface
81
67
  readonly defaultTimeout?: TimeoutPolicy | undefined;
82
68
  readonly defaultTimeoutMs?: number | undefined;
83
69
  readonly sharedLuaChunks?: readonly string[] | undefined;
70
+ readonly onScriptAudit?: ((event: ScriptAuditEvent) => void) | undefined;
84
71
  }): LuaMicroverse<THost, EffectiveScriptHooks<THost, TSurface>, InferSurfaceCapabilitiesFromSurface<TSurface>>;
85
72
  export {};
86
73
  //# sourceMappingURL=luaMicroverse.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"luaMicroverse.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/facade/luaMicroverse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAGL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC3B,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAIL,KAAK,aAAa,EACnB,MAAM,6BAA6B,CAAC;AAGrC,4IAA4I;AAC5I,OAAO,CAAC,MAAM,iBAAiB,EAAE,OAAO,MAAM,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,uBAAuB,CACjC,MAAM,SAAS,qBAAqB,EACpC,KAAK,GAAG,OAAO,IACb,KAAK,GAAG;IAAE,QAAQ,CAAC,CAAC,iBAAiB,CAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtD,MAAM,MAAM,wBAAwB,CAAC,KAAK,IAAI,KAAK,SAAS,uBAAuB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,GACjG,CAAC,SAAS,qBAAqB,GAC7B,CAAC,GACD,SAAS,GACX,SAAS,CAAC;AAEd,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,eAAe,IAC/D,CAAC,SAAS,eAAe,CAAC,YAAY,CAAC,GAAG;IAAE,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC,SAAS,qBAAqB,CAAA;CAAE,GACvG,CAAC,GACD,CAAC,SAAS,WAAW,CAAC,MAAM,CAAC,SAAS,qBAAqB,EAAE,YAAY,CAAC,GACxE,CAAC,GACD,SAAS,CAAC;AAElB,MAAM,MAAM,mCAAmC,CA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AS,CAAC;CAC1E,GAAG,aAAa,CAAC,KAAK,EAAE,oBAAoB,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,mCAAmC,CAAC,QAAQ,CAAC,CAAC,CAY7G"}
@@ -1,4 +1,4 @@
1
- import { createLuaMicroverse } from './luaMicroverse.js';
1
+ import { createLuaMicroverse } from './luaMicroverse';
2
2
  /**
3
3
  * Plug-and-play **Lua microverse** entry point.
4
4
  *
@@ -12,7 +12,8 @@ import { createLuaMicroverse } from './luaMicroverse.js';
12
12
  * surface: mySurface,
13
13
  * defaultTimeoutMs: 30_000,
14
14
  * });
15
- * await microverse.registerScript({ scriptId: 'ai', script: lua, capabilities: surface.pickCapabilities('demo:tick') });
15
+ * microverse.registerScriptDefinition({ scriptId: 'ai', source: lua });
16
+ * await microverse.mountScriptInstance({ instanceId: 'ai', scriptId: 'ai', capabilities: surface.pickCapabilities('demo:tick') });
16
17
  * ```
17
18
  */
18
19
  export declare const MicroverseLua: {
@@ -1 +1 @@
1
- {"version":3,"file":"microverseLuaNamespace.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/facade/microverseLuaNamespace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,aAAa;IACxB,wEAAwE;;CAEhE,CAAC"}
1
+ {"version":3,"file":"microverseLuaNamespace.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/facade/microverseLuaNamespace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,aAAa;IACxB,wEAAwE;;CAEhE,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@microverse.ts/microverse-lua",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
- "description": "Plug-and-play Lua microverse facade: Wasm VM, MicroverseLua, and re-exports for host surfaces and bridges.",
7
+ "description": "Main entry for Microverse Lua: MicroverseLua.create (Wasm sandbox, mounts, events) and defineHostSurfaceFor—re-exports the stack.",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/QADRAX/Microverse.ts.git",
@@ -30,14 +30,14 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "zod": "^3.24.0",
33
- "@microverse.ts/host-surface": "0.1.0",
34
- "@microverse.ts/runtime-bridge": "0.1.0",
35
- "@microverse.ts/runtime-capabilities": "0.1.0",
36
- "@microverse.ts/runtime-core": "0.1.0",
37
- "@microverse.ts/runtime-lua": "0.1.0",
38
- "@microverse.ts/runtime-wasm": "0.1.0",
39
- "@microverse.ts/runtime-zod": "0.1.0",
40
- "@microverse.ts/shared": "0.1.0"
33
+ "@microverse.ts/host-surface": "0.2.0",
34
+ "@microverse.ts/runtime-bridge": "0.2.0",
35
+ "@microverse.ts/runtime-capabilities": "0.2.0",
36
+ "@microverse.ts/runtime-core": "0.2.0",
37
+ "@microverse.ts/runtime-lua": "0.2.0",
38
+ "@microverse.ts/runtime-wasm": "0.2.0",
39
+ "@microverse.ts/runtime-zod": "0.2.0",
40
+ "@microverse.ts/shared": "0.2.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "typescript": "^5.8.3",