@glassly/miniapp-cli 0.1.16 → 0.1.17-dev.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.
@@ -0,0 +1,269 @@
1
+ import {MiniappErrorCode, MiniappRequestType, MiniappResponseType} from "@glassly/miniapp"
2
+
3
+ import type {SimTypeContext, SimTypeHandler} from "./context.js"
4
+
5
+ // Mirrors LocalMiniappRuntime's speaker.createStream handlers, the native
6
+ // PcmStreamPlayer's buffering, GlassesSensorService, and the IMU and mic gate
7
+ // handlers in the Glassly App.
8
+
9
+ const SAMPLE_RATES = new Set([16000, 24000, 48000])
10
+ const MAX_BASE64_CHARS = Math.ceil((256 * 1024) / 3) * 4
11
+ const BACKPRESSURE_CEILING_MS = 2000
12
+ const MAXIMUM_BACKLOG_MS = 10000
13
+ const STREAM_CLOSED = "PCM stream is closed"
14
+
15
+ type Sensor = "light" | "compass"
16
+ type Options = Record<string, number>
17
+
18
+ const SENSOR_RANGES: Record<Sensor, Record<string, [number, number]>> = {
19
+ light: {intervalMs: [100, 5000], minDelta: [0, 65535], heartbeatMs: [0, 65535]},
20
+ compass: {intervalMs: [50, 2000], minChangeDegrees: [0, 359]},
21
+ }
22
+ const COMPASS_DEFAULTS: Options = {intervalMs: 1000, minChangeDegrees: 5}
23
+ const SENSOR_FAILED =
24
+ "Sensor command failed: the glasses need Glassly custom firmware with this sensor, and on the G2 an active miniapp display"
25
+ // The G2 custom firmware carries light and compass; MemoMind One's firmware only light.
26
+ const SENSOR_DEVICES: Record<Sensor, readonly string[]> = {light: ["g2-cfw", "memomind-one"], compass: ["g2-cfw"]}
27
+
28
+ interface Waiter {
29
+ timer: ReturnType<typeof setTimeout>
30
+ settle: (closed: boolean) => void
31
+ }
32
+
33
+ interface SpeakerStream {
34
+ id: string
35
+ sampleRate: number
36
+ openedAt: number
37
+ playsUntil: number
38
+ queuedMs: number
39
+ closing: boolean
40
+ waiters: Set<Waiter>
41
+ }
42
+
43
+ interface AudioState {
44
+ stream: SpeakerStream | null
45
+ streamSeq: number
46
+ lightOwner: Options | null
47
+ compassOverride: Options | null
48
+ compassApplied: Options | null
49
+ }
50
+
51
+ const states = new WeakMap<SimTypeContext, AudioState>()
52
+
53
+ function stateOf(ctx: SimTypeContext): AudioState {
54
+ let state = states.get(ctx)
55
+ if (!state) {
56
+ state = {stream: null, streamSeq: 0, lightOwner: null, compassOverride: null, compassApplied: null}
57
+ states.set(ctx, state)
58
+ }
59
+ return state
60
+ }
61
+
62
+ function speakerState(ctx: SimTypeContext, state: string, extra: Record<string, unknown> = {}): void {
63
+ ctx.deliver({type: MiniappResponseType.SPEAKER_STATE, state, ...extra})
64
+ if (state === "error") ctx.deliver({type: MiniappResponseType.SPEAKER_STATE, state: "stopped"})
65
+ }
66
+
67
+ const backlogMs = (stream: SpeakerStream) => Math.max(0, Math.floor(stream.playsUntil - Date.now()))
68
+ const playedMs = (stream: SpeakerStream) => Math.floor(stream.queuedMs - Math.max(0, stream.playsUntil - Date.now()))
69
+
70
+ function wait(stream: SpeakerStream, ms: number, settle: (closed: boolean) => void): void {
71
+ const waiter: Waiter = {
72
+ timer: setTimeout(() => {
73
+ stream.waiters.delete(waiter)
74
+ settle(false)
75
+ }, ms),
76
+ settle,
77
+ }
78
+ stream.waiters.add(waiter)
79
+ }
80
+
81
+ function endStream(ctx: SimTypeContext, state: AudioState, stream: SpeakerStream, extra: Record<string, unknown>): void {
82
+ if (state.stream === stream) state.stream = null
83
+ for (const waiter of stream.waiters) {
84
+ clearTimeout(waiter.timer)
85
+ waiter.settle(true)
86
+ }
87
+ stream.waiters.clear()
88
+ speakerState(ctx, extra.errorMessage === undefined ? "stopped" : "error", extra)
89
+ }
90
+
91
+ function abortStream(ctx: SimTypeContext, state: AudioState, stream: SpeakerStream): void {
92
+ stream.playsUntil = Math.min(stream.playsUntil, Date.now())
93
+ ctx.effect("speaker_stream_abort", {streamId: stream.id})
94
+ endStream(ctx, state, stream, {durationMs: Date.now() - stream.openedAt})
95
+ }
96
+
97
+ function failStream(ctx: SimTypeContext, state: AudioState, stream: SpeakerStream, requestId: string | undefined, message: string): void {
98
+ endStream(ctx, state, stream, {errorCode: MiniappErrorCode.INTERNAL, errorMessage: message, durationMs: Date.now() - stream.openedAt})
99
+ ctx.fail(requestId, MiniappErrorCode.INTERNAL, message)
100
+ }
101
+
102
+ function activeStream(state: AudioState, payload: Record<string, unknown>): SpeakerStream | null {
103
+ return typeof payload.streamId === "string" && payload.streamId && state.stream?.id === payload.streamId ? state.stream : null
104
+ }
105
+
106
+ function openStream(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
107
+ const state = stateOf(ctx)
108
+ if (state.stream) abortStream(ctx, state, state.stream)
109
+
110
+ const sampleRate = typeof payload.sampleRate === "number" ? payload.sampleRate : 16000
111
+ if (!SAMPLE_RATES.has(sampleRate)) {
112
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, `unsupported sampleRate ${sampleRate} (use 16000, 24000, or 48000)`)
113
+ }
114
+ const channels = typeof payload.channels === "number" ? payload.channels : 1
115
+ if (channels !== 1) return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "speaker streams currently support mono PCM only")
116
+ const volume = typeof payload.volume === "number" ? payload.volume : 1
117
+ if (!Number.isFinite(volume) || volume < 0 || volume > 1) {
118
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "speaker stream volume must be between 0 and 1")
119
+ }
120
+
121
+ const id = `spkstream_${Date.now()}_${++state.streamSeq}`
122
+ speakerState(ctx, "loading")
123
+ state.stream = {id, sampleRate, openedAt: Date.now(), playsUntil: 0, queuedMs: 0, closing: false, waiters: new Set()}
124
+ ctx.effect("speaker_stream_open", {streamId: id, sampleRate, channels, volume})
125
+ ctx.reply(requestId, true, {streamId: id})
126
+ speakerState(ctx, "playing")
127
+ }
128
+
129
+ // The phone holds a write's reply until the backlog drains to the ceiling, which throttles an awaiting producer to realtime.
130
+ function writeStream(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
131
+ const state = stateOf(ctx)
132
+ const stream = activeStream(state, payload)
133
+ if (!stream) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "speaker stream is not open")
134
+ const base64 = typeof payload.base64 === "string" ? payload.base64 : ""
135
+ if (!base64 || base64.length % 4 !== 0) {
136
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "speaker stream write requires padded base64 PCM data")
137
+ }
138
+ if (base64.length > MAX_BASE64_CHARS) {
139
+ return ctx.fail(requestId, MiniappErrorCode.PAYLOAD_TOO_LARGE, "speaker stream chunk exceeds the 256 KB raw PCM limit")
140
+ }
141
+ const bytes = /^[A-Za-z0-9+/]*={0,2}$/.test(base64) ? Buffer.from(base64, "base64").length : 0
142
+ if (bytes === 0) return failStream(ctx, state, stream, requestId, "PCM chunk is empty or invalid base64")
143
+ if (bytes % 2 !== 0) return failStream(ctx, state, stream, requestId, `PCM chunk length ${bytes} is not aligned to a 2-byte frame`)
144
+ if (stream.closing) return failStream(ctx, state, stream, requestId, STREAM_CLOSED)
145
+
146
+ const chunkMs = ((bytes / 2) * 1000) / stream.sampleRate
147
+ if (backlogMs(stream) + chunkMs > MAXIMUM_BACKLOG_MS) {
148
+ return failStream(ctx, state, stream, requestId, `PCM backlog exceeds ${MAXIMUM_BACKLOG_MS}ms; producer is not throttling`)
149
+ }
150
+ stream.playsUntil = Math.max(stream.playsUntil, Date.now()) + chunkMs
151
+ stream.queuedMs += chunkMs
152
+ const backlog = backlogMs(stream)
153
+ if (backlog <= BACKPRESSURE_CEILING_MS) return ctx.reply(requestId, true, {bufferedMs: backlog})
154
+ wait(stream, backlog - BACKPRESSURE_CEILING_MS, (closed) => {
155
+ if (closed) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, STREAM_CLOSED)
156
+ ctx.reply(requestId, true, {bufferedMs: backlogMs(stream)})
157
+ })
158
+ }
159
+
160
+ function closeStream(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
161
+ const state = stateOf(ctx)
162
+ const stream = activeStream(state, payload)
163
+ if (!stream) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "speaker stream is not open")
164
+ const first = !stream.closing
165
+ stream.closing = true
166
+ wait(stream, backlogMs(stream), (aborted) => {
167
+ const durationMs = playedMs(stream)
168
+ if (!aborted && first) {
169
+ ctx.effect("speaker_stream_close", {streamId: stream.id, durationMs})
170
+ endStream(ctx, state, stream, {durationMs})
171
+ }
172
+ ctx.reply(requestId, true, {durationMs})
173
+ })
174
+ }
175
+
176
+ function sensorControl(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
177
+ const state = stateOf(ctx)
178
+ const sensor = payload.sensor
179
+ const operation = payload.operation
180
+ if (sensor !== "light" && sensor !== "compass") return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "Unknown sensor")
181
+ if (operation !== "start" && operation !== "stop" && !(sensor === "light" && operation === "query")) {
182
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "Unknown sensor operation")
183
+ }
184
+ const raw = payload.options ?? {}
185
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
186
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "Invalid sensor options")
187
+ }
188
+ const options: Options = {}
189
+ for (const [key, value] of Object.entries(raw)) {
190
+ const range = Object.prototype.hasOwnProperty.call(SENSOR_RANGES[sensor], key) ? SENSOR_RANGES[sensor][key] : undefined
191
+ if (!range || typeof value !== "number" || !Number.isInteger(value) || value < range[0] || value > range[1]) {
192
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, `Invalid ${key}`)
193
+ }
194
+ options[key] = value
195
+ }
196
+
197
+ const supported = SENSOR_DEVICES[sensor].includes(ctx.device.id)
198
+ const command = (detail: Record<string, unknown>): boolean => {
199
+ if (!supported) return false
200
+ ctx.effect("glasses_sensor", {sensor, ...detail})
201
+ ctx.fake(MiniappRequestType.GLASSES_SENSOR_CONTROL, "the firmware accepts the command; no sensor readings are sent")
202
+ return true
203
+ }
204
+
205
+ if (sensor === "compass") {
206
+ state.compassOverride = operation === "start" ? options : null
207
+ const demand = state.compassOverride && {...COMPASS_DEFAULTS, ...state.compassOverride}
208
+ if (JSON.stringify(demand) === JSON.stringify(state.compassApplied)) return ctx.reply(requestId, true)
209
+ const ok = command(demand ? {operation: "start", options: demand} : {operation: "stop"})
210
+ if (ok || !demand) state.compassApplied = demand
211
+ return ok ? ctx.reply(requestId, true) : ctx.fail(requestId, MiniappErrorCode.INTERNAL, SENSOR_FAILED)
212
+ }
213
+
214
+ if (operation === "stop" && !state.lightOwner) return ctx.reply(requestId, true)
215
+ if (operation === "start") state.lightOwner = options
216
+ if (!command({operation, options})) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, SENSOR_FAILED)
217
+ if (operation === "stop") state.lightOwner = null
218
+ ctx.reply(requestId, true)
219
+ }
220
+
221
+ function micGate(ctx: SimTypeContext, type: string, gate: "vad" | "loudnessGate", payload: Record<string, unknown>, requestId?: string): void {
222
+ if (!ctx.requirePermission(requestId, "MICROPHONE", type)) return
223
+ if (typeof payload.enabled !== "boolean") return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "enabled must be a boolean")
224
+ const mic = (ctx.device.capabilities.microphone ?? {}) as {hasVAD?: boolean; volumeGate?: boolean}
225
+ const isLive = ctx.device.capabilities.modelName === "Mentra Live"
226
+ const applies = gate === "vad" ? mic.hasVAD === true : isLive || mic.volumeGate === true
227
+ ctx.effect(gate === "vad" ? "mic_vad" : "mic_loudness_gate", {enabled: payload.enabled, applies})
228
+ ctx.reply(requestId, true)
229
+ }
230
+
231
+ export const handleAudio: SimTypeHandler = (ctx, type, payload, requestId) => {
232
+ switch (type) {
233
+ case MiniappRequestType.SPEAKER_STREAM_OPEN:
234
+ openStream(ctx, payload, requestId)
235
+ return true
236
+ case MiniappRequestType.SPEAKER_STREAM_WRITE:
237
+ writeStream(ctx, payload, requestId)
238
+ return true
239
+ case MiniappRequestType.SPEAKER_STREAM_CLOSE:
240
+ closeStream(ctx, payload, requestId)
241
+ return true
242
+ case MiniappRequestType.SPEAKER_STREAM_ABORT: {
243
+ const state = stateOf(ctx)
244
+ const stream = activeStream(state, payload)
245
+ if (stream) abortStream(ctx, state, stream)
246
+ ctx.reply(requestId, true)
247
+ return true
248
+ }
249
+ case MiniappRequestType.GLASSES_SENSOR_CONTROL:
250
+ sensorControl(ctx, payload, requestId)
251
+ return true
252
+ case MiniappRequestType.IMU_SET_ENABLED: {
253
+ const enabled = !!payload.enabled
254
+ ctx.effect("imu", {enabled, applies: ctx.device.capabilities.modelName === "Even Realities G2"})
255
+ ctx.reply(requestId, true)
256
+ return true
257
+ }
258
+ case MiniappRequestType.MIC_SET_VAD_ENABLED:
259
+ micGate(ctx, type, "vad", payload, requestId)
260
+ return true
261
+ case MiniappRequestType.MIC_SET_LOUDNESS_GATE_ENABLED:
262
+ micGate(ctx, type, "loudnessGate", payload, requestId)
263
+ return true
264
+ }
265
+ return false
266
+ }
267
+
268
+ // Every code this group answers is shared with other groups, so it adds no default messages.
269
+ export const AUDIO_MESSAGES: Record<string, string> = {}
@@ -0,0 +1,88 @@
1
+ // Ports the Glassly App's blobPaths.ts and BlobStore's name helpers.
2
+
3
+ const MIME_EXT: Record<string, string> = {
4
+ "application/pdf": "pdf",
5
+ "application/epub+zip": "epub",
6
+ "application/zip": "zip",
7
+ "application/json": "json",
8
+ "text/plain": "txt",
9
+ "text/csv": "csv",
10
+ "text/html": "html",
11
+ "video/mp4": "mp4",
12
+ "video/quicktime": "mov",
13
+ "video/webm": "webm",
14
+ "audio/mpeg": "mp3",
15
+ "audio/mp4": "m4a",
16
+ "audio/wav": "wav",
17
+ "audio/x-wav": "wav",
18
+ "audio/wave": "wav",
19
+ "audio/ogg": "ogg",
20
+ "image/png": "png",
21
+ "image/jpeg": "jpg",
22
+ "image/gif": "gif",
23
+ "image/webp": "webp",
24
+ "image/svg+xml": "svg",
25
+ }
26
+
27
+ const EXT_MIME: Record<string, string> = {
28
+ pdf: "application/pdf",
29
+ epub: "application/epub+zip",
30
+ zip: "application/zip",
31
+ json: "application/json",
32
+ txt: "text/plain",
33
+ csv: "text/csv",
34
+ html: "text/html",
35
+ mp4: "video/mp4",
36
+ mov: "video/quicktime",
37
+ webm: "video/webm",
38
+ mp3: "audio/mpeg",
39
+ m4a: "audio/mp4",
40
+ wav: "audio/wav",
41
+ ogg: "audio/ogg",
42
+ png: "image/png",
43
+ jpg: "image/jpeg",
44
+ jpeg: "image/jpeg",
45
+ gif: "image/gif",
46
+ webp: "image/webp",
47
+ svg: "image/svg+xml",
48
+ }
49
+
50
+ export function extForMime(mime: string): string | undefined {
51
+ return MIME_EXT[(mime || "").split(";")[0].trim().toLowerCase()]
52
+ }
53
+
54
+ export function inferMime(name: string): string | undefined {
55
+ const dot = name.lastIndexOf(".")
56
+ return dot < 0 ? undefined : EXT_MIME[name.slice(dot + 1).toLowerCase()]
57
+ }
58
+
59
+ export function withDiskExt(fileName: string, mimeType: string): string {
60
+ if (fileName.includes(".")) return fileName
61
+ const ext = extForMime(mimeType)
62
+ return ext ? `${fileName}.${ext}` : fileName
63
+ }
64
+
65
+ export function urlBasename(uri: string): string {
66
+ const noQuery = uri.split("?")[0].split("#")[0]
67
+ const seg = noQuery.substring(noQuery.lastIndexOf("/") + 1)
68
+ try {
69
+ return decodeURIComponent(seg)
70
+ } catch {
71
+ return seg
72
+ }
73
+ }
74
+
75
+ export function shareFileName(meta: {name?: string; fileName: string; mimeType: string}): string {
76
+ const raw = (meta.name || meta.fileName).trim()
77
+ // eslint-disable-next-line no-control-regex
78
+ let safe = raw.replace(/[/\\<>:"|?*\x00-\x1f]/g, "_").trim()
79
+ if (!safe) safe = meta.fileName
80
+ const dot = safe.lastIndexOf(".")
81
+ const hasExt = dot > 0 && dot < safe.length - 1
82
+ let base = hasExt ? safe.slice(0, dot) : safe
83
+ const ext = hasExt ? safe.slice(dot + 1) : extForMime(meta.mimeType)
84
+ const suffix = ext ? `.${ext}` : ""
85
+ const MAX_LEN = 120
86
+ if (base.length + suffix.length > MAX_LEN) base = base.slice(0, Math.max(1, MAX_LEN - suffix.length))
87
+ return base + suffix
88
+ }