@glassly/miniapp-cli 0.1.14 → 0.1.15-beta.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.
- package/README.md +3 -1
- package/package.json +1 -1
- package/src/api.ts +2 -1
- package/src/sim/client/app.css +5 -0
- package/src/sim/client/main.ts +7 -2
- package/src/sim/device.test.ts +59 -12
- package/src/sim/device.ts +184 -51
- package/src/sim/host.test.ts +369 -10
- package/src/sim/host.ts +664 -88
- package/src/sim/models.json +599 -0
- package/src/sim/regenerate-models.ts +34 -0
- package/src/sim/registry.ts +394 -0
- package/src/sim/scenarios.test.ts +74 -0
- package/src/sim/script.test.ts +31 -0
- package/src/sim/script.ts +48 -0
- package/src/sim/server.ts +49 -1
- package/src/sim/shots.test.ts +59 -0
- package/src/sim/shots.ts +161 -0
- package/src/sim/sim.ts +114 -12
- package/src/sim/storage.ts +8 -7
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import * as hostRules from "@glassly/miniapp/host"
|
|
2
|
+
import {MiniappErrorCode, MiniappRequestType, MiniappResponseType} from "@glassly/miniapp/protocol"
|
|
3
|
+
|
|
4
|
+
import type {SimHost} from "./host.js"
|
|
5
|
+
|
|
6
|
+
type Outcome = {ok: true; data: unknown} | {ok: false; error: {code: string; message: string} & Record<string, unknown>}
|
|
7
|
+
type WidgetBox = {size: "XS" | "XSM" | "SM" | "MD" | "LG"; width: number; height: number}
|
|
8
|
+
|
|
9
|
+
/** The caller name the CLI acts under: a host, and a system caller, like the phone itself. */
|
|
10
|
+
export const CLI_CALLER = "glassly.sim"
|
|
11
|
+
|
|
12
|
+
const WAKE_TIMEOUT_MS = 10_000
|
|
13
|
+
const PAYLOAD_MAX = 256 * 1024
|
|
14
|
+
const WIDGET_FRAME_CAP = 64
|
|
15
|
+
const WIDGET_EXPANDED_FRAME_CAP = 128
|
|
16
|
+
const COMPATIBLE = {isCompatible: true, missingRequired: [], missingOptional: [], warnings: []}
|
|
17
|
+
|
|
18
|
+
const fail = (code: string, message: string): Outcome => ({ok: false, error: {code, message}})
|
|
19
|
+
const tooLarge = (value: unknown) => JSON.stringify(value ?? null).length > PAYLOAD_MAX
|
|
20
|
+
|
|
21
|
+
export interface SimRegistryOptions {
|
|
22
|
+
/** Starts a miniapp's background script; its host reports the CONNECT. */
|
|
23
|
+
launch?: (packageName: string) => void
|
|
24
|
+
/** Stops it. */
|
|
25
|
+
halt?: (packageName: string) => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The miniapps installed in one simulator and the requests that cross between
|
|
30
|
+
* them: listing, starting and stopping miniapps, actions, and widgets. Follows
|
|
31
|
+
* the Glassly App's LocalMiniappRuntime through the shared rule modules.
|
|
32
|
+
*/
|
|
33
|
+
export class SimRegistry {
|
|
34
|
+
private readonly hosts = new Map<string, SimHost>()
|
|
35
|
+
private placements = hostRules.emptyWidgetPlacements?.()
|
|
36
|
+
private readonly actionCalls = hostRules.PendingActionCalls
|
|
37
|
+
? new hostRules.PendingActionCalls(
|
|
38
|
+
{setTimeout: (cb, ms) => setTimeout(cb, ms) as unknown as number, clearTimeout: (id) => clearTimeout(id)},
|
|
39
|
+
tooLarge,
|
|
40
|
+
)
|
|
41
|
+
: null
|
|
42
|
+
private readonly widgetCalls = new Map<string, {host: string; settle: (outcome: Outcome) => void; timer: ReturnType<typeof setTimeout>}>()
|
|
43
|
+
private readonly cliFrames = new Map<string, (elements: unknown[]) => void>()
|
|
44
|
+
private callSeq = 0
|
|
45
|
+
/** The wearer's per-widget choice whether a tap expands an EX widget. */
|
|
46
|
+
private readonly expandOnTap = new Map<string, boolean>()
|
|
47
|
+
|
|
48
|
+
constructor(private readonly options: SimRegistryOptions = {}) {}
|
|
49
|
+
|
|
50
|
+
add(host: SimHost): void {
|
|
51
|
+
this.hosts.set(host.packageName, host)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
apps(): SimHost[] {
|
|
55
|
+
return [...this.hosts.values()]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The installed apps as the shared rules read them. */
|
|
59
|
+
private interopApps() {
|
|
60
|
+
return this.apps().map((host) => ({
|
|
61
|
+
packageName: host.packageName,
|
|
62
|
+
name: host.manifest.name,
|
|
63
|
+
version: host.manifest.version ?? "",
|
|
64
|
+
permissions: host.manifest.permissions,
|
|
65
|
+
actions: hostRules.normalizeManifestActions?.(host.manifest.actions) ?? [],
|
|
66
|
+
widgets: hostRules.normalizeManifestWidgets?.(host.manifest.widgets) ?? [],
|
|
67
|
+
running: host.connected,
|
|
68
|
+
}))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Answers a cross-app request from `caller`; false when the type is not one. */
|
|
72
|
+
handle(caller: SimHost, type: string, payload: Record<string, unknown>, requestId?: string): boolean {
|
|
73
|
+
if (!this.actionCalls || !this.placements) return false
|
|
74
|
+
const answer = (outcome: Outcome) =>
|
|
75
|
+
outcome.ok ? caller.reply(requestId, true, outcome.data) : caller.reply(requestId, false, undefined, outcome.error)
|
|
76
|
+
const run = (work: Promise<Outcome>) => void work.then(answer)
|
|
77
|
+
switch (type) {
|
|
78
|
+
case MiniappRequestType.MINIAPPS_LIST:
|
|
79
|
+
if (!caller.requirePermission(requestId, "MANAGE_MINIAPPS")) return true
|
|
80
|
+
return answer({
|
|
81
|
+
ok: true,
|
|
82
|
+
data: this.interopApps()
|
|
83
|
+
.filter((app) => app.packageName !== caller.packageName)
|
|
84
|
+
.map(({packageName, name, version, running, actions}) => ({
|
|
85
|
+
packageName,
|
|
86
|
+
name,
|
|
87
|
+
version,
|
|
88
|
+
running,
|
|
89
|
+
hidden: false,
|
|
90
|
+
system: false,
|
|
91
|
+
compatibility: COMPATIBLE,
|
|
92
|
+
actions,
|
|
93
|
+
})),
|
|
94
|
+
}), true
|
|
95
|
+
case MiniappRequestType.MINIAPPS_START: {
|
|
96
|
+
if (!caller.requirePermission(requestId, "MANAGE_MINIAPPS")) return true
|
|
97
|
+
const target = payload.packageName as string | undefined
|
|
98
|
+
if (!target || !this.hosts.has(target)) return answer(fail(MiniappErrorCode.APP_NOT_FOUND, `Miniapp not found: ${target ?? "(missing packageName)"}`)), true
|
|
99
|
+
run(this.wake(target).then((error) => error ?? {ok: true, data: undefined}))
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
case MiniappRequestType.MINIAPPS_STOP: {
|
|
103
|
+
if (!caller.requirePermission(requestId, "MANAGE_MINIAPPS")) return true
|
|
104
|
+
const target = payload.packageName as string | undefined
|
|
105
|
+
if (!target) return answer(fail(MiniappErrorCode.APP_NOT_FOUND, "stop requires a packageName")), true
|
|
106
|
+
this.stop(target)
|
|
107
|
+
return answer({ok: true, data: undefined}), true
|
|
108
|
+
}
|
|
109
|
+
case MiniappRequestType.ACTIONS_LIST:
|
|
110
|
+
return answer({ok: true, data: hostRules.visibleActionRows!(this.interopApps(), caller.packageName, false)}), true
|
|
111
|
+
case MiniappRequestType.ACTION_INVOKE: {
|
|
112
|
+
const timeoutMs = Math.min(Math.max(Number(payload.timeoutMs) || 30_000, 6_000), 120_000)
|
|
113
|
+
const params = (payload.params as Record<string, unknown> | undefined) ?? {}
|
|
114
|
+
run(this.invoke(caller.packageName, false, payload.targetPackageName as string | undefined, payload.actionId as string | undefined, params, timeoutMs))
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
case MiniappRequestType.ACTION_RESULT:
|
|
118
|
+
this.actionCalls.handleResult(caller.packageName, payload)
|
|
119
|
+
return true
|
|
120
|
+
case MiniappRequestType.WIDGETS_LIST:
|
|
121
|
+
if (!caller.requirePermission(requestId, "HOST_WIDGETS")) return true
|
|
122
|
+
return answer({ok: true, data: this.widgetRows(caller.packageName)}), true
|
|
123
|
+
case MiniappRequestType.WIDGET_PLACE:
|
|
124
|
+
if (!caller.requirePermission(requestId, "HOST_WIDGETS")) return true
|
|
125
|
+
run(this.place(caller.packageName, payload))
|
|
126
|
+
return true
|
|
127
|
+
case MiniappRequestType.WIDGET_UNPLACE:
|
|
128
|
+
if (!caller.requirePermission(requestId, "HOST_WIDGETS")) return true
|
|
129
|
+
return answer(this.unplace(caller.packageName, payload)), true
|
|
130
|
+
case MiniappRequestType.WIDGET_SEND:
|
|
131
|
+
if (!caller.requirePermission(requestId, "HOST_WIDGETS")) return true
|
|
132
|
+
run(this.sendWidgetEvent(caller.packageName, payload))
|
|
133
|
+
return true
|
|
134
|
+
case MiniappRequestType.WIDGET_EXPANDABLE: {
|
|
135
|
+
const widgetId = payload.widgetId as string | undefined
|
|
136
|
+
const widget = this.interopApps()
|
|
137
|
+
.find((app) => app.packageName === caller.packageName)
|
|
138
|
+
?.widgets.find((candidate) => candidate.id === widgetId)
|
|
139
|
+
if (!widget?.expandable) {
|
|
140
|
+
return answer(fail(MiniappErrorCode.WIDGET_NOT_FOUND, `${caller.packageName} has no widget "${widgetId ?? "(missing widgetId)"}" that lists EX`)), true
|
|
141
|
+
}
|
|
142
|
+
const key = `${caller.packageName}/${widget.id}`
|
|
143
|
+
if (typeof payload.expandable === "boolean") this.expandOnTap.set(key, payload.expandable)
|
|
144
|
+
return answer({ok: true, data: {expandable: this.expandOnTap.get(key) ?? widget.expandOnTap}}), true
|
|
145
|
+
}
|
|
146
|
+
case MiniappRequestType.WIDGET_UPDATE:
|
|
147
|
+
this.widgetUpdate(caller.packageName, payload)
|
|
148
|
+
return true
|
|
149
|
+
case MiniappRequestType.WIDGET_RESULT: {
|
|
150
|
+
const pending = this.widgetCalls.get(String(payload.callId))
|
|
151
|
+
if (!pending) return true
|
|
152
|
+
this.widgetCalls.delete(String(payload.callId))
|
|
153
|
+
clearTimeout(pending.timer)
|
|
154
|
+
if (payload.ok !== true) {
|
|
155
|
+
pending.settle({ok: false, error: (payload.error as (Outcome & {ok: false})["error"] | undefined) ?? {code: MiniappErrorCode.INTERNAL, message: "widget event handler error"}})
|
|
156
|
+
} else if (tooLarge(payload.result)) {
|
|
157
|
+
pending.settle(fail(MiniappErrorCode.PAYLOAD_TOO_LARGE, "widget event result exceeded the 256 KB cap"))
|
|
158
|
+
} else pending.settle({ok: true, data: payload.result ?? null})
|
|
159
|
+
return true
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return false
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Calls a declared action as the phone or the assistant would (a system caller). */
|
|
166
|
+
callAction(target: string, actionId: string, params: Record<string, unknown>, timeoutMs = 10_000): Promise<unknown> {
|
|
167
|
+
if (!this.actionCalls) return Promise.reject(new Error("Calling actions needs a newer @glassly/miniapp. Run `glassly update`."))
|
|
168
|
+
return this.invoke(CLI_CALLER, true, target, actionId, params, timeoutMs).then(unwrap)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Places a widget in a box for the CLI and resolves with the first card frame its provider draws. */
|
|
172
|
+
placeForCli(provider: string, widgetId: string, box: WidgetBox, timeoutMs = 10_000): Promise<unknown> {
|
|
173
|
+
const key = `${provider}/${widgetId}`
|
|
174
|
+
// A frame carries no call id, so only one placement per widget can wait for it.
|
|
175
|
+
if (this.cliFrames.has(key)) {
|
|
176
|
+
return Promise.reject(new Error(`Widget "${widgetId}" is still waiting for the frame of an earlier placement`))
|
|
177
|
+
}
|
|
178
|
+
return new Promise((resolve, reject) => {
|
|
179
|
+
const timer = setTimeout(() => {
|
|
180
|
+
if (this.cliFrames.get(key) === onFrame) this.cliFrames.delete(key)
|
|
181
|
+
reject(new Error(`No card frame for widget "${widgetId}" within ${timeoutMs} ms`))
|
|
182
|
+
}, timeoutMs)
|
|
183
|
+
const onFrame = (elements: unknown[]) => {
|
|
184
|
+
clearTimeout(timer)
|
|
185
|
+
this.cliFrames.delete(key)
|
|
186
|
+
resolve({view: "card", elements})
|
|
187
|
+
}
|
|
188
|
+
this.cliFrames.set(key, onFrame)
|
|
189
|
+
void this.place(CLI_CALLER, {packageName: provider, widgetId, box}).then((outcome) => {
|
|
190
|
+
if (outcome.ok) return
|
|
191
|
+
clearTimeout(timer)
|
|
192
|
+
this.cliFrames.delete(key)
|
|
193
|
+
reject(callError(outcome.error))
|
|
194
|
+
})
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Sends a placed (or placed now) widget an event for the CLI and resolves with the provider's answer. */
|
|
199
|
+
async sendForCli(provider: string, widgetId: string, event: Record<string, unknown>, timeoutMs = 10_000): Promise<unknown> {
|
|
200
|
+
if (!hostRules.placementOf?.(this.placements!, CLI_CALLER, provider, widgetId)) {
|
|
201
|
+
const placed = await this.place(CLI_CALLER, {packageName: provider, widgetId, box: {size: "MD", width: 251, height: 120}})
|
|
202
|
+
if (!placed.ok) throw callError(placed.error)
|
|
203
|
+
}
|
|
204
|
+
return unwrap(await this.sendWidgetEvent(CLI_CALLER, {packageName: provider, widgetId, event, timeoutMs}))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
stop(packageName: string): void {
|
|
208
|
+
const host = this.hosts.get(packageName)
|
|
209
|
+
if (!host?.connected) return
|
|
210
|
+
this.placements = hostRules.dropHost!(this.placements!, packageName)
|
|
211
|
+
this.placements = hostRules.dropProvider!(this.placements!, packageName)
|
|
212
|
+
this.options.halt?.(packageName)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Starts a miniapp that is not running and waits for its CONNECT; resolves with an error outcome on failure. */
|
|
216
|
+
private async wake(packageName: string): Promise<Outcome | null> {
|
|
217
|
+
const host = this.hosts.get(packageName)
|
|
218
|
+
if (!host) return fail(MiniappErrorCode.APP_NOT_FOUND, `Miniapp not found: ${packageName}`)
|
|
219
|
+
if (host.connected) return null
|
|
220
|
+
if (!this.options.launch) return fail(MiniappErrorCode.WAKE_FAILED, `failed to wake ${packageName}`)
|
|
221
|
+
this.options.launch(packageName)
|
|
222
|
+
const connected = () => this.hosts.get(packageName)?.connected === true
|
|
223
|
+
const deadline = Date.now() + WAKE_TIMEOUT_MS
|
|
224
|
+
while (!connected() && Date.now() < deadline) await Bun.sleep(20)
|
|
225
|
+
return connected() ? null : fail(MiniappErrorCode.WAKE_FAILED, `failed to wake ${packageName}`)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private async invoke(
|
|
229
|
+
caller: string,
|
|
230
|
+
callerIsSystem: boolean,
|
|
231
|
+
target: string | undefined,
|
|
232
|
+
actionId: string | undefined,
|
|
233
|
+
params: Record<string, unknown>,
|
|
234
|
+
timeoutMs: number,
|
|
235
|
+
): Promise<Outcome> {
|
|
236
|
+
if (tooLarge(params)) return fail(MiniappErrorCode.PAYLOAD_TOO_LARGE, "action params exceeded the 256 KB cap")
|
|
237
|
+
const app = target ? this.interopApps().find((candidate) => candidate.packageName === target) : undefined
|
|
238
|
+
const denied = hostRules.checkActionAccess!(app, target, actionId, callerIsSystem)
|
|
239
|
+
if (denied) return {ok: false, error: denied}
|
|
240
|
+
const woken = await this.wake(target!)
|
|
241
|
+
if (woken) return woken
|
|
242
|
+
const host = this.hosts.get(target!)!
|
|
243
|
+
const callId = `act-sim-${++this.callSeq}`
|
|
244
|
+
const outcome = this.actionCalls!.register(callId, target!, actionId!, timeoutMs)
|
|
245
|
+
host.deliver({type: MiniappResponseType.ACTION_CALL, callId, actionId, params, callerPackageName: caller})
|
|
246
|
+
return outcome
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private widgetRows(hostPackageName: string): Record<string, unknown>[] {
|
|
250
|
+
const rows: Record<string, unknown>[] = []
|
|
251
|
+
for (const app of this.interopApps()) {
|
|
252
|
+
if (app.packageName === hostPackageName) continue
|
|
253
|
+
for (const widget of app.widgets) {
|
|
254
|
+
rows.push({
|
|
255
|
+
packageName: app.packageName,
|
|
256
|
+
appName: app.name,
|
|
257
|
+
id: widget.id,
|
|
258
|
+
name: widget.name,
|
|
259
|
+
sizes: widget.sizes,
|
|
260
|
+
defaultSize: widget.defaultSize,
|
|
261
|
+
expandable: widget.expandOnTap,
|
|
262
|
+
running: app.running,
|
|
263
|
+
compatible: true,
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return rows
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private widgetTarget(payload: Record<string, unknown>): {provider: string; widgetId: string} | Outcome {
|
|
271
|
+
const target = payload.packageName as string | undefined
|
|
272
|
+
const widgetId = payload.widgetId as string | undefined
|
|
273
|
+
const app = target ? this.interopApps().find((candidate) => candidate.packageName === target) : undefined
|
|
274
|
+
if (!target || !app) return fail(MiniappErrorCode.APP_NOT_FOUND, `Miniapp not found: ${target ?? "(missing packageName)"}`)
|
|
275
|
+
if (!widgetId || !app.widgets.some((widget) => widget.id === widgetId)) {
|
|
276
|
+
return fail(MiniappErrorCode.WIDGET_NOT_FOUND, `${target} does not declare widget "${widgetId ?? "(missing widgetId)"}"`)
|
|
277
|
+
}
|
|
278
|
+
return {provider: target, widgetId}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private async place(hostPackageName: string, payload: Record<string, unknown>): Promise<Outcome> {
|
|
282
|
+
const target = this.widgetTarget(payload)
|
|
283
|
+
if ("ok" in target) return target
|
|
284
|
+
const box = parseWidgetBox(payload.box)
|
|
285
|
+
if (!box) return fail(MiniappErrorCode.INVALID_ARGUMENT, "place needs a box {size, width, height}")
|
|
286
|
+
const {provider, widgetId} = target
|
|
287
|
+
this.placements = hostRules.placeWidget!(this.placements!, {hostPackageName, providerPackageName: provider, widgetId, box})
|
|
288
|
+
const woken = await this.wake(provider)
|
|
289
|
+
if (woken) {
|
|
290
|
+
this.placements = hostRules.unplaceWidget!(this.placements!, hostPackageName, provider, widgetId)
|
|
291
|
+
return woken
|
|
292
|
+
}
|
|
293
|
+
if (hostRules.placementOf!(this.placements!, hostPackageName, provider, widgetId)) {
|
|
294
|
+
this.hosts.get(provider)!.deliver({type: MiniappResponseType.WIDGET_CALL, callId: `wdg-sim-${++this.callSeq}`, widgetId, kind: "box", box})
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
ok: true,
|
|
298
|
+
data: {
|
|
299
|
+
frame: hostRules.frameOf!(this.placements!, provider, widgetId),
|
|
300
|
+
expandedFrame: hostRules.frameOf!(this.placements!, provider, widgetId, "expanded"),
|
|
301
|
+
},
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private unplace(hostPackageName: string, payload: Record<string, unknown>): Outcome {
|
|
306
|
+
const target = this.widgetTarget(payload)
|
|
307
|
+
if ("ok" in target) return target
|
|
308
|
+
const {provider, widgetId} = target
|
|
309
|
+
const had = hostRules.placementOf!(this.placements!, hostPackageName, provider, widgetId)
|
|
310
|
+
this.placements = hostRules.unplaceWidget!(this.placements!, hostPackageName, provider, widgetId)
|
|
311
|
+
if (had && hostRules.hostsOf!(this.placements!, provider, widgetId).length === 0) {
|
|
312
|
+
this.hosts.get(provider)?.deliver({type: MiniappResponseType.WIDGET_CALL, callId: `wdg-sim-${++this.callSeq}`, widgetId, kind: "box", box: null})
|
|
313
|
+
}
|
|
314
|
+
return {ok: true, data: undefined}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private async sendWidgetEvent(hostPackageName: string, payload: Record<string, unknown>): Promise<Outcome> {
|
|
318
|
+
const target = this.widgetTarget(payload)
|
|
319
|
+
if ("ok" in target) return target
|
|
320
|
+
const {provider, widgetId} = target
|
|
321
|
+
const event = payload.event as Record<string, unknown> | undefined
|
|
322
|
+
if (!event || typeof event !== "object" || typeof event.type !== "string") {
|
|
323
|
+
return fail(MiniappErrorCode.INVALID_ARGUMENT, "send needs an event with a string type")
|
|
324
|
+
}
|
|
325
|
+
if (!hostRules.placementOf!(this.placements!, hostPackageName, provider, widgetId)) {
|
|
326
|
+
return fail(MiniappErrorCode.WIDGET_NOT_PLACED, `${hostPackageName} has not placed ${provider}/${widgetId}`)
|
|
327
|
+
}
|
|
328
|
+
const timeoutMs = Math.min(Math.max(Number(payload.timeoutMs) || 5_000, 6_000), 30_000)
|
|
329
|
+
const woken = await this.wake(provider)
|
|
330
|
+
if (woken) return woken
|
|
331
|
+
const callId = `wdg-sim-${++this.callSeq}`
|
|
332
|
+
return new Promise((settle) => {
|
|
333
|
+
const timer = setTimeout(() => {
|
|
334
|
+
this.widgetCalls.delete(callId)
|
|
335
|
+
settle(fail(MiniappErrorCode.ACTION_TIMEOUT, `widget event "${event.type}" timed out after ${timeoutMs}ms`))
|
|
336
|
+
}, timeoutMs)
|
|
337
|
+
this.widgetCalls.set(callId, {host: hostPackageName, settle, timer})
|
|
338
|
+
this.hosts.get(provider)!.deliver({type: MiniappResponseType.WIDGET_CALL, callId, widgetId, kind: "event", event})
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private widgetUpdate(provider: string, payload: Record<string, unknown>): void {
|
|
343
|
+
const widgetId = payload.widgetId as string | undefined
|
|
344
|
+
const elements = payload.elements
|
|
345
|
+
if (!widgetId || !Array.isArray(elements)) return
|
|
346
|
+
const widget = this.interopApps()
|
|
347
|
+
.find((app) => app.packageName === provider)
|
|
348
|
+
?.widgets.find((candidate) => candidate.id === widgetId)
|
|
349
|
+
if (!widget) return
|
|
350
|
+
const view = payload.view === "expanded" ? "expanded" : "card"
|
|
351
|
+
const host = this.hosts.get(provider)
|
|
352
|
+
if (view === "expanded" && !widget.expandable) {
|
|
353
|
+
return host?.log("host", "warn", `Dropped an expanded frame from ${provider}/${widgetId}: it does not list EX`)
|
|
354
|
+
}
|
|
355
|
+
if (elements.length > (view === "expanded" ? WIDGET_EXPANDED_FRAME_CAP : WIDGET_FRAME_CAP) || tooLarge(elements)) {
|
|
356
|
+
return host?.log("host", "warn", `Dropped an oversized ${view} widget frame from ${provider}/${widgetId}`)
|
|
357
|
+
}
|
|
358
|
+
this.placements = hostRules.setFrame!(this.placements!, provider, widgetId, elements, view)
|
|
359
|
+
for (const hostName of hostRules.hostsOf!(this.placements!, provider, widgetId)) {
|
|
360
|
+
if (hostName === CLI_CALLER) {
|
|
361
|
+
if (view === "card") this.cliFrames.get(`${provider}/${widgetId}`)?.(elements)
|
|
362
|
+
continue
|
|
363
|
+
}
|
|
364
|
+
this.hosts.get(hostName)?.deliver({
|
|
365
|
+
type: MiniappResponseType.WIDGET_FRAME,
|
|
366
|
+
packageName: provider,
|
|
367
|
+
widgetId,
|
|
368
|
+
elements,
|
|
369
|
+
...(view === "expanded" ? {view} : {}),
|
|
370
|
+
})
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function parseWidgetBox(raw: unknown): WidgetBox | null {
|
|
376
|
+
const box = raw as {size?: unknown; width?: unknown; height?: unknown} | null
|
|
377
|
+
if (!box || typeof box !== "object") return null
|
|
378
|
+
if (box.size !== "XS" && box.size !== "XSM" && box.size !== "SM" && box.size !== "MD" && box.size !== "LG") return null
|
|
379
|
+
const width = Number(box.width)
|
|
380
|
+
const height = Number(box.height)
|
|
381
|
+
if (!(width > 0) || !(height > 0)) return null
|
|
382
|
+
return {size: box.size, width: Math.round(width), height: Math.round(height)}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function unwrap(outcome: Outcome): unknown {
|
|
386
|
+
if (outcome.ok) return outcome.data
|
|
387
|
+
throw callError(outcome.error)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** An Error whose message leads with the code, carrying the reply's other fields. */
|
|
391
|
+
export function callError(error: {code: string; message: string} & Record<string, unknown>): Error {
|
|
392
|
+
const err = new Error(`${error.code}: ${error.message}`)
|
|
393
|
+
return Object.assign(err, {...error, message: err.message})
|
|
394
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
|
|
3
|
+
import {expect, test} from "bun:test"
|
|
4
|
+
import {existsSync} from "fs"
|
|
5
|
+
import {fileURLToPath} from "url"
|
|
6
|
+
|
|
7
|
+
import {simDevice} from "./device.js"
|
|
8
|
+
import {SimHost} from "./host.js"
|
|
9
|
+
import {createSimStorage} from "./storage.js"
|
|
10
|
+
|
|
11
|
+
// The shared scenario list lives in the SDK's source tree; a computed path
|
|
12
|
+
// keeps it out of this package's typecheck, as device.test.ts does.
|
|
13
|
+
const scenariosPath = fileURLToPath(new URL("../../../../mobile/modules/miniapp/scenarios/requests.ts", import.meta.url))
|
|
14
|
+
|
|
15
|
+
interface Scenario {
|
|
16
|
+
name: string
|
|
17
|
+
manifest?: {type?: string; permissions?: string[]; actions?: unknown[]}
|
|
18
|
+
device?: string
|
|
19
|
+
before?: Record<string, unknown>[]
|
|
20
|
+
request: Record<string, unknown>
|
|
21
|
+
reply: Record<string, unknown>
|
|
22
|
+
only?: {host: string; reason: string}
|
|
23
|
+
exact?: string[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const {REQUEST_SCENARIOS, SCENARIO_PACKAGE} = existsSync(scenariosPath)
|
|
27
|
+
? ((await import(scenariosPath)) as {REQUEST_SCENARIOS: Scenario[]; SCENARIO_PACKAGE: string})
|
|
28
|
+
: {REQUEST_SCENARIOS: [] as Scenario[], SCENARIO_PACKAGE: ""}
|
|
29
|
+
|
|
30
|
+
async function run(scenario: Scenario): Promise<unknown> {
|
|
31
|
+
const replies = new Map<string, unknown>()
|
|
32
|
+
const manifest = scenario.manifest ?? {}
|
|
33
|
+
const host = new SimHost({
|
|
34
|
+
manifest: {
|
|
35
|
+
packageName: SCENARIO_PACKAGE,
|
|
36
|
+
name: "Scenario",
|
|
37
|
+
type: manifest.type,
|
|
38
|
+
permissions: (manifest.permissions ?? []).map((type) => ({type})),
|
|
39
|
+
actions: manifest.actions,
|
|
40
|
+
},
|
|
41
|
+
device: simDevice(scenario.device),
|
|
42
|
+
storage: createSimStorage(),
|
|
43
|
+
toBackground: (raw) => {
|
|
44
|
+
const {requestId, payload} = JSON.parse(raw) as {requestId?: string; payload: unknown}
|
|
45
|
+
if (requestId) replies.set(requestId, payload)
|
|
46
|
+
},
|
|
47
|
+
emit: () => {},
|
|
48
|
+
frameLatencyMs: 0,
|
|
49
|
+
})
|
|
50
|
+
host.backgroundStarted()
|
|
51
|
+
const send = async (payload: Record<string, unknown>, requestId: string) => {
|
|
52
|
+
host.fromBackground(JSON.stringify({requestId, payload}))
|
|
53
|
+
for (let i = 0; i < 50 && !replies.has(requestId); i++) await Bun.sleep(1)
|
|
54
|
+
return replies.get(requestId)
|
|
55
|
+
}
|
|
56
|
+
await send({type: "miniapp_connect"}, "connect")
|
|
57
|
+
for (const [i, before] of (scenario.before ?? []).entries()) await send(before, `before-${i}`)
|
|
58
|
+
const reply = await send(scenario.request, "scenario")
|
|
59
|
+
host.backgroundStopped(false)
|
|
60
|
+
return reply
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const at = (value: unknown, path: string) => path.split(".").reduce<any>((node, key) => node?.[key], value)
|
|
64
|
+
|
|
65
|
+
const runnable = REQUEST_SCENARIOS.filter((scenario) => scenario.only?.host !== "phone")
|
|
66
|
+
|
|
67
|
+
test.skipIf(!existsSync(scenariosPath)).each(runnable.map((scenario) => [scenario.name, scenario] as const))(
|
|
68
|
+
"%s",
|
|
69
|
+
async (_name, scenario) => {
|
|
70
|
+
const reply = await run(scenario)
|
|
71
|
+
expect(reply).toMatchObject(scenario.reply)
|
|
72
|
+
for (const path of scenario.exact ?? []) expect(at(reply, path)).toEqual(at(scenario.reply, path))
|
|
73
|
+
},
|
|
74
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
|
|
3
|
+
import {expect, test} from "bun:test"
|
|
4
|
+
|
|
5
|
+
import {parseScript} from "./script.js"
|
|
6
|
+
|
|
7
|
+
test("a script line carries its time, speaker, language, text and translations", () => {
|
|
8
|
+
const script = `
|
|
9
|
+
# A conversation at the station
|
|
10
|
+
[0:00] A (en): Where is the train station?
|
|
11
|
+
[0:03] B (es): Está a dos cuadras. | en: It's two blocks away.
|
|
12
|
+
A: Thanks!
|
|
13
|
+
[1:02.5] Somebody said this
|
|
14
|
+
`
|
|
15
|
+
expect(parseScript(script)).toEqual([
|
|
16
|
+
{line: 3, atMs: 0, speaker: "A", language: "en", text: "Where is the train station?", translations: {}},
|
|
17
|
+
{line: 4, atMs: 3000, speaker: "B", language: "es", text: "Está a dos cuadras.", translations: {en: "It's two blocks away."}},
|
|
18
|
+
{line: 5, speaker: "A", text: "Thanks!", translations: {}},
|
|
19
|
+
{line: 6, atMs: 62_500, text: "Somebody said this", translations: {}},
|
|
20
|
+
])
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test("a colon inside the text is not a speaker", () => {
|
|
24
|
+
expect(parseScript("Meet at 10:30 by the gate")).toEqual([
|
|
25
|
+
{line: 1, text: "Meet at 10:30 by the gate", translations: {}},
|
|
26
|
+
])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test("a malformed time names its line", () => {
|
|
30
|
+
expect(() => parseScript("hello\n[1:7x] A: hi")).toThrow("Line 2: a time is [m:ss] or [m:ss.s], got [1:7x]")
|
|
31
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface ScriptLine {
|
|
2
|
+
line: number
|
|
3
|
+
/** From the start of the script; absent lines follow the one before. */
|
|
4
|
+
atMs?: number
|
|
5
|
+
speaker?: string
|
|
6
|
+
language?: string
|
|
7
|
+
text: string
|
|
8
|
+
/** Translated text by target language code, from `| code: text`. */
|
|
9
|
+
translations: Record<string, string>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const TIME = /^\[([^\]]*)\]\s*/
|
|
13
|
+
const SPEAKER = /^([\p{L}\p{N}_.-]{1,24})(?:\s*\(([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})?)\))?:\s+/u
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses a transcript script: one utterance per line, as
|
|
17
|
+
* `[m:ss] SPEAKER (lang): text | lang: translation`, every part but the text
|
|
18
|
+
* optional. Lines starting with # are comments.
|
|
19
|
+
*/
|
|
20
|
+
export function parseScript(source: string): ScriptLine[] {
|
|
21
|
+
const lines: ScriptLine[] = []
|
|
22
|
+
source.split(/\r?\n/).forEach((raw, index) => {
|
|
23
|
+
let rest = raw.trim()
|
|
24
|
+
if (!rest || rest.startsWith("#")) return
|
|
25
|
+
const entry: ScriptLine = {line: index + 1, text: "", translations: {}}
|
|
26
|
+
const time = TIME.exec(rest)
|
|
27
|
+
if (time) {
|
|
28
|
+
const match = /^(\d+):(\d{1,2}(?:\.\d+)?)$/.exec(time[1].trim())
|
|
29
|
+
if (!match) throw new Error(`Line ${index + 1}: a time is [m:ss] or [m:ss.s], got [${time[1]}]`)
|
|
30
|
+
entry.atMs = Math.round((Number(match[1]) * 60 + Number(match[2])) * 1000)
|
|
31
|
+
rest = rest.slice(time[0].length)
|
|
32
|
+
}
|
|
33
|
+
const speaker = SPEAKER.exec(rest)
|
|
34
|
+
if (speaker) {
|
|
35
|
+
entry.speaker = speaker[1]
|
|
36
|
+
if (speaker[2]) entry.language = speaker[2]
|
|
37
|
+
rest = rest.slice(speaker[0].length)
|
|
38
|
+
}
|
|
39
|
+
const [text, ...translations] = rest.split("|")
|
|
40
|
+
entry.text = text.trim()
|
|
41
|
+
for (const part of translations) {
|
|
42
|
+
const match = /^\s*([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})?)\s*:\s*(.*)$/.exec(part)
|
|
43
|
+
if (match && match[2].trim()) entry.translations[match[1]] = match[2].trim()
|
|
44
|
+
}
|
|
45
|
+
if (entry.text) lines.push(entry)
|
|
46
|
+
})
|
|
47
|
+
return lines
|
|
48
|
+
}
|
package/src/sim/server.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {existsSync, readFileSync, statSync} from "fs"
|
|
1
|
+
import {existsSync, mkdirSync, readFileSync, statSync, writeFileSync} from "fs"
|
|
2
2
|
import {join, normalize, sep} from "path"
|
|
3
3
|
import type {ServerWebSocket} from "bun"
|
|
4
4
|
import {buildSafeAreaUpdateScript, buildUiShim, buildWebviewGlobalsScript} from "@glassly/miniapp/host"
|
|
@@ -6,6 +6,7 @@ import type {MirrorSnapshot} from "@glassly/miniapp/mirror"
|
|
|
6
6
|
|
|
7
7
|
import type {SimHost, SimHostEvent} from "./host.js"
|
|
8
8
|
import {parsePhone, phoneLayout, RAIL_BACK, type PhoneLayout} from "./phone.js"
|
|
9
|
+
import {parseScript} from "./script.js"
|
|
9
10
|
import type {ScreenRenderer} from "./screenshot.js"
|
|
10
11
|
|
|
11
12
|
export interface SimServerOptions {
|
|
@@ -53,6 +54,8 @@ const CONTENT_TYPES: Record<string, string> = {
|
|
|
53
54
|
wav: "audio/wav",
|
|
54
55
|
}
|
|
55
56
|
|
|
57
|
+
const FRAME_SETTLE_MS = 120
|
|
58
|
+
|
|
56
59
|
function contentType(path: string): string {
|
|
57
60
|
return CONTENT_TYPES[path.slice(path.lastIndexOf(".") + 1).toLowerCase()] ?? "application/octet-stream"
|
|
58
61
|
}
|
|
@@ -260,6 +263,18 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
260
263
|
})
|
|
261
264
|
}
|
|
262
265
|
|
|
266
|
+
// While a script plays with a frames folder, each frame is saved once it settles.
|
|
267
|
+
let recordFrames: {dir: string; count: number; timer: ReturnType<typeof setTimeout> | null} | null = null
|
|
268
|
+
const scheduleFrame = (recording: NonNullable<typeof recordFrames>) => {
|
|
269
|
+
if (recording.timer) clearTimeout(recording.timer)
|
|
270
|
+
recording.timer = setTimeout(async () => {
|
|
271
|
+
recording.timer = null
|
|
272
|
+
if (typeof options.screen === "string") return
|
|
273
|
+
const file = join(recording.dir, `frame-${String(++recording.count).padStart(4, "0")}.png`)
|
|
274
|
+
writeFileSync(file, await options.screen.png(2))
|
|
275
|
+
}, Math.max(FRAME_SETTLE_MS, host.displaySettlesAt() - Date.now() + FRAME_SETTLE_MS))
|
|
276
|
+
}
|
|
277
|
+
|
|
263
278
|
const api = async (request: Request, url: URL): Promise<Response> => {
|
|
264
279
|
const route = url.pathname.slice("/api/".length)
|
|
265
280
|
if (request.method === "GET") {
|
|
@@ -272,6 +287,7 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
272
287
|
}
|
|
273
288
|
if (route === "screenshot") {
|
|
274
289
|
if (typeof options.screen === "string") return json({error: `Screenshots are unavailable: ${options.screen}`}, 501)
|
|
290
|
+
if (!host.snapshot().canvas) return json({error: "These glasses have no display"}, 409)
|
|
275
291
|
if (url.searchParams.get("wait") !== "0") {
|
|
276
292
|
const wait = Math.min(10_000, host.displaySettlesAt() - Date.now())
|
|
277
293
|
if (wait > 0) await Bun.sleep(wait)
|
|
@@ -303,6 +319,7 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
303
319
|
return json({error: "Body is not valid JSON"}, 400)
|
|
304
320
|
}
|
|
305
321
|
const text = (key: string) => (typeof body[key] === "string" ? (body[key] as string) : undefined)
|
|
322
|
+
const timeout = () => (body.timeoutMs === undefined ? undefined : number("timeoutMs"))
|
|
306
323
|
const number = (key: string) => {
|
|
307
324
|
const value = Number(body[key])
|
|
308
325
|
if (!Number.isFinite(value)) throw new Error(`"${key}" must be a number`)
|
|
@@ -329,8 +346,24 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
329
346
|
target: text("target"),
|
|
330
347
|
isFinal: body.isFinal !== false,
|
|
331
348
|
originalText: text("originalText"),
|
|
349
|
+
speakerId: text("speakerId"),
|
|
332
350
|
})
|
|
333
351
|
break
|
|
352
|
+
case "play": {
|
|
353
|
+
const lines = parseScript(text("script") ?? "")
|
|
354
|
+
if (lines.length === 0) throw new Error("The script has no lines to play")
|
|
355
|
+
const frames = text("framesDir")
|
|
356
|
+
if (frames && typeof options.screen === "string") throw new Error(`Frames need screenshots, which are unavailable: ${options.screen}`)
|
|
357
|
+
recordFrames = frames ? {dir: frames, count: 0, timer: null} : null
|
|
358
|
+
if (frames) mkdirSync(frames, {recursive: true})
|
|
359
|
+
const played = await host.play(lines, {
|
|
360
|
+
wordsPerMinute: body.wordsPerMinute === undefined ? undefined : number("wordsPerMinute"),
|
|
361
|
+
})
|
|
362
|
+
await Bun.sleep(400)
|
|
363
|
+
const written = recordFrames?.count ?? 0
|
|
364
|
+
recordFrames = null
|
|
365
|
+
return json({ok: true, ...played, frames: written})
|
|
366
|
+
}
|
|
334
367
|
case "location":
|
|
335
368
|
host.setLocation(number("lat"), number("lng"), body.accuracy === undefined ? undefined : number("accuracy"))
|
|
336
369
|
break
|
|
@@ -368,6 +401,20 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
368
401
|
break
|
|
369
402
|
case "ui/request":
|
|
370
403
|
return json({ok: true, result: await host.uiRequest(text("channel") ?? "", body.payload, Number(body.timeoutMs) || undefined)})
|
|
404
|
+
case "reset":
|
|
405
|
+
await host.reset()
|
|
406
|
+
break
|
|
407
|
+
case "action":
|
|
408
|
+
return json({
|
|
409
|
+
ok: true,
|
|
410
|
+
result: await host.callAction(text("actionId") ?? "", (body.params ?? {}) as Record<string, unknown>, timeout(), text("app")),
|
|
411
|
+
})
|
|
412
|
+
case "widget": {
|
|
413
|
+
const box = body.box as {size: "XS" | "XSM" | "SM" | "MD" | "LG"; width: number; height: number} | undefined
|
|
414
|
+
const event = body.event as Record<string, unknown> | undefined
|
|
415
|
+
const call = event ? {event} : {box: box ?? {size: "MD" as const, width: 251, height: 120}}
|
|
416
|
+
return json({ok: true, result: await host.callWidget(text("widgetId") ?? "", call, timeout(), text("app"))})
|
|
417
|
+
}
|
|
371
418
|
case "restart":
|
|
372
419
|
await options.restart()
|
|
373
420
|
break
|
|
@@ -445,6 +492,7 @@ export function startSimServer(options: SimServerOptions): SimServer {
|
|
|
445
492
|
handle(event) {
|
|
446
493
|
if (event.type === "display") {
|
|
447
494
|
if (typeof options.screen !== "string") options.screen.update(event.snapshot)
|
|
495
|
+
if (recordFrames) scheduleFrame(recordFrames)
|
|
448
496
|
broadcast(pages, {type: "display", snapshot: event.snapshot})
|
|
449
497
|
} else if (event.type === "log") {
|
|
450
498
|
if (event.entry.source !== "bridge") broadcast(pages, {type: "log", entry: event.entry})
|