@glassly/miniapp 0.4.3-dev.1267 → 0.4.3-dev.1269

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +9 -1
  2. package/dist/host/index.d.ts +1 -0
  3. package/dist/host/index.d.ts.map +1 -1
  4. package/dist/host/index.js +1 -0
  5. package/dist/host/index.js.map +1 -1
  6. package/dist/host/settingsBridge.d.ts +31 -8
  7. package/dist/host/settingsBridge.d.ts.map +1 -1
  8. package/dist/host/settingsBridge.js +63 -11
  9. package/dist/host/settingsBridge.js.map +1 -1
  10. package/dist/host/ttsSanitizer.d.ts +5 -0
  11. package/dist/host/ttsSanitizer.d.ts.map +1 -0
  12. package/dist/host/ttsSanitizer.js +61 -0
  13. package/dist/host/ttsSanitizer.js.map +1 -0
  14. package/dist/modules/cloud.d.ts +2 -0
  15. package/dist/modules/cloud.d.ts.map +1 -1
  16. package/dist/modules/cloud.js +17 -7
  17. package/dist/modules/cloud.js.map +1 -1
  18. package/dist/modules/settings.d.ts +4 -1
  19. package/dist/modules/settings.d.ts.map +1 -1
  20. package/dist/modules/settings.js +4 -1
  21. package/dist/modules/settings.js.map +1 -1
  22. package/dist/reader/Reader.d.ts +147 -0
  23. package/dist/reader/Reader.d.ts.map +1 -0
  24. package/dist/reader/Reader.js +525 -0
  25. package/dist/reader/Reader.js.map +1 -0
  26. package/dist/reader/document.d.ts +54 -0
  27. package/dist/reader/document.d.ts.map +1 -0
  28. package/dist/reader/document.js +114 -0
  29. package/dist/reader/document.js.map +1 -0
  30. package/dist/reader/index.d.ts +12 -0
  31. package/dist/reader/index.d.ts.map +1 -0
  32. package/dist/reader/index.js +9 -0
  33. package/dist/reader/index.js.map +1 -0
  34. package/dist/reader/lens.d.ts +55 -0
  35. package/dist/reader/lens.d.ts.map +1 -0
  36. package/dist/reader/lens.js +210 -0
  37. package/dist/reader/lens.js.map +1 -0
  38. package/dist/sub-path-types.test-d.d.ts +3 -1
  39. package/dist/sub-path-types.test-d.d.ts.map +1 -1
  40. package/dist/sub-path-types.test-d.js +1 -7
  41. package/dist/sub-path-types.test-d.js.map +1 -1
  42. package/package.json +6 -2
  43. package/src/host/index.ts +1 -0
  44. package/src/host/settingsBridge.ts +70 -11
  45. package/src/host/ttsSanitizer.ts +66 -0
  46. package/src/modules/cloud.ts +21 -7
  47. package/src/modules/settings.ts +4 -1
  48. package/src/reader/Reader.ts +580 -0
  49. package/src/reader/document.ts +154 -0
  50. package/src/reader/index.ts +12 -0
  51. package/src/reader/lens.ts +250 -0
  52. package/src/sub-path-types.test-d.ts +5 -0
@@ -0,0 +1,154 @@
1
+ /**
2
+ * The text as the lens reads it: every section wrapped into display lines,
3
+ * with each line's word count, where paragraphs end, and the parts between
4
+ * stops (an end screen). Pure: the Reader rebuilds it when the text, the
5
+ * glasses or the highlight style change.
6
+ */
7
+
8
+ import type {TextWrapper} from "../display/wrapper"
9
+
10
+ export interface ReaderSection {
11
+ label?: string
12
+ text: string
13
+ status?: string
14
+ stop?: ReaderStop
15
+ }
16
+
17
+ export interface ReaderStop {
18
+ title?: string
19
+ tap?: {label: string; action?: string}
20
+ }
21
+
22
+ export interface ReaderLine {
23
+ text: string
24
+ words: number
25
+ section: number
26
+ kind: "label" | "text" | "blank"
27
+ paragraphEnd: boolean
28
+ }
29
+
30
+ export interface ReaderPart {
31
+ first: number
32
+ last: number
33
+ words: number
34
+ stopSection: number
35
+ }
36
+
37
+ export const PARAGRAPH_BEAT_WORDS = 3
38
+
39
+ const countWords = (text: string) => text.split(/\s+/).filter(Boolean).length
40
+
41
+ export class ReaderDocument {
42
+ readonly lines: ReaderLine[] = []
43
+ readonly parts: ReaderPart[] = []
44
+ private readonly before: number[] = []
45
+
46
+ constructor(
47
+ readonly sections: ReaderSection[],
48
+ wrapper: TextWrapper,
49
+ wrapWidthPx: number,
50
+ ) {
51
+ const wrap = (text: string) =>
52
+ wrapper.wrap(text, {
53
+ maxWidthPx: wrapWidthPx,
54
+ maxLines: Infinity,
55
+ maxBytes: Infinity,
56
+ breakMode: "word",
57
+ trimLines: true,
58
+ preserveNewlines: true,
59
+ }).lines
60
+ sections.forEach((section, index) => {
61
+ if (index > 0 && this.lines.length)
62
+ this.push({text: "", words: 0, section: index, kind: "blank", paragraphEnd: false})
63
+ if (section.label) {
64
+ for (const text of wrap(section.label))
65
+ this.push({text, words: countWords(text), section: index, kind: "label", paragraphEnd: false})
66
+ }
67
+ const body = wrap(section.text.trim())
68
+ body.forEach((text, i) => {
69
+ const blank = text.trim() === ""
70
+ const next = body[i + 1]
71
+ this.push({
72
+ text: blank ? "" : text,
73
+ words: blank ? 0 : countWords(text),
74
+ section: index,
75
+ kind: blank ? "blank" : "text",
76
+ paragraphEnd: !blank && (next === undefined || next.trim() === ""),
77
+ })
78
+ })
79
+ })
80
+ if (!this.lines.length) this.push({text: "", words: 0, section: 0, kind: "blank", paragraphEnd: true})
81
+
82
+ let first = 0
83
+ let words = 0
84
+ this.lines.forEach((line, i) => {
85
+ this.before.push(words)
86
+ words += line.words
87
+ const next = this.lines[i + 1]
88
+ const sectionEnds = !next || next.section !== line.section
89
+ const stops = sectionEnds && (!next || sections[line.section]?.stop !== undefined)
90
+ if (!stops) return
91
+ this.parts.push({first, last: i, words, stopSection: line.section})
92
+ first = i + 1
93
+ words = 0
94
+ })
95
+ }
96
+
97
+ private push(line: ReaderLine): void {
98
+ this.lines.push(line)
99
+ }
100
+
101
+ partOf(line: number): number {
102
+ const index = this.parts.findIndex((part) => line <= part.last)
103
+ return index < 0 ? this.parts.length - 1 : index
104
+ }
105
+
106
+ percent(line: number, finished = false): number {
107
+ const part = this.parts[this.partOf(line)]
108
+ if (finished || !part.words) return finished ? 100 : 0
109
+ return Math.min(100, Math.round((this.before[line] / part.words) * 100))
110
+ }
111
+
112
+ lineAt(partIndex: number, percent: number): number {
113
+ const part = this.parts[Math.max(0, Math.min(this.parts.length - 1, partIndex))]
114
+ return this.lineAtWord(partIndex, (Math.max(0, Math.min(100, percent)) / 100) * part.words)
115
+ }
116
+
117
+ /** The line holding the part's word at `words` (words read before it); past the end, the part's last line. */
118
+ lineAtWord(partIndex: number, words: number): number {
119
+ const part = this.parts[Math.max(0, Math.min(this.parts.length - 1, partIndex))]
120
+ for (let i = part.first; i <= part.last; i++) {
121
+ if (this.before[i] + this.lines[i].words > words && this.lines[i].words > 0) return i
122
+ }
123
+ return this.previousReadable(part.last + 1, part.first) ?? part.first
124
+ }
125
+
126
+ wordsBefore(line: number): number {
127
+ return this.before[line] ?? 0
128
+ }
129
+
130
+ nextReadable(line: number, limit = this.lines.length - 1): number | null {
131
+ for (let i = line + 1; i <= limit; i++) if (this.lines[i].words > 0) return i
132
+ return null
133
+ }
134
+
135
+ previousReadable(line: number, floor = 0): number | null {
136
+ for (let i = line - 1; i >= floor; i--) if (this.lines[i].words > 0) return i
137
+ return null
138
+ }
139
+
140
+ wordsLeft(line: number): number {
141
+ const part = this.parts[this.partOf(line)]
142
+ return part.words - this.before[line]
143
+ }
144
+
145
+ lineMs(line: number, wpm: number, beats: boolean): number {
146
+ const entry = this.lines[line]
147
+ const beat = beats && entry.paragraphEnd ? PARAGRAPH_BEAT_WORDS : 0
148
+ return ((entry.words + beat) * 60_000) / wpm
149
+ }
150
+
151
+ rowsAround(line: number, at: number, count: number): string[] {
152
+ return Array.from({length: count}, (_, i) => this.lines[line - at + i]?.text ?? "")
153
+ }
154
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @glassly/miniapp/reader — text that reads itself on the glasses. Open it
3
+ * from the background side with the session and your text; see
4
+ * sdk/docs/reader.md.
5
+ */
6
+
7
+ export {openReader, Reader, READER_WPM} from "./Reader"
8
+ export type {ReaderDeps, ReaderEvent, ReaderOptions, ReaderSession} from "./Reader"
9
+ export {ReaderDocument, PARAGRAPH_BEAT_WORDS} from "./document"
10
+ export type {ReaderLine, ReaderPart, ReaderSection, ReaderStop} from "./document"
11
+ export {LENS_COLOR, lensElements, lensLayout, serializeRows, statusLine, textWrapWidth, usesBrackets} from "./lens"
12
+ export type {LensDisplay, LensFrame, LensHighlight, LensLayout} from "./lens"
@@ -0,0 +1,250 @@
1
+ /**
2
+ * The Reader's lens, lifted from Teleprompter's (#51): the text moves up
3
+ * under a fixed marked row just above the middle, lines already read sit
4
+ * dimmed above it, and a status row along the bottom always says the state.
5
+ * Glasses that place elements get a small scene; text-only glasses get one
6
+ * block of rows with the marked row in the same place. Pure: no timers, no I/O.
7
+ */
8
+
9
+ import type {DisplayProfile} from "../display/profiles"
10
+ import type {TextMeasurer} from "../display/measurer"
11
+ import {textBoxLines} from "../display/profiles"
12
+ import type {RenderElement} from "../modules/display"
13
+
14
+ export type LensHighlight = "filled" | "brackets" | "dim"
15
+
16
+ export interface LensDisplay {
17
+ width?: number
18
+ height?: number
19
+ canPosition?: boolean
20
+ maxTextElements?: number
21
+ shapes?: string[]
22
+ }
23
+
24
+ export interface LensLayout {
25
+ mode: "scene" | "text"
26
+ width: number
27
+ height: number
28
+ pitch: number
29
+ rows: number
30
+ marked: number
31
+ wrapWidthPx: number
32
+ rect: boolean
33
+ budget: number
34
+ rowsPerText: number
35
+ }
36
+
37
+ export interface LensFrame {
38
+ rows: string[]
39
+ status: string
40
+ detail: string
41
+ marked: number | null
42
+ highlight: LensHighlight
43
+ dimBefore?: number
44
+ toast?: string | null
45
+ }
46
+
47
+ const DEFAULT_PITCH = 27
48
+ const PAD = 6
49
+ const TEXT_INSET = 4
50
+ const MAX_ROWS = 11
51
+ const MIN_ROWS = 3
52
+ const CANVAS = {width: 576, height: 288}
53
+ const BRACKET_OPEN = "> "
54
+ const BRACKET_CLOSE = " <"
55
+
56
+ export const LENS_COLOR = {read: 5, marked: 15, coming: 12, status: 9, bar: 3} as const
57
+
58
+ function markedRow(rows: number): number {
59
+ return Math.floor((rows - 1) / 2)
60
+ }
61
+
62
+ export function usesBrackets(layout: LensLayout, highlight: LensHighlight): boolean {
63
+ if (highlight === "dim") return false
64
+ return highlight === "brackets" || layout.mode === "text" || !layout.rect
65
+ }
66
+
67
+ /** How the lens fits a model: rows, the marked row, the wrap width, and what it can afford to draw. */
68
+ export function lensLayout(profile: DisplayProfile, display: LensDisplay | null | undefined): LensLayout {
69
+ const budget = display?.maxTextElements ?? 0
70
+ if (display?.canPosition && display.width && display.height && budget >= 4) {
71
+ const pitch = profile.lineHeightPx ?? DEFAULT_PITCH
72
+ const total = Math.max(MIN_ROWS, Math.min(MAX_ROWS, Math.floor(display.height / pitch)))
73
+ return {
74
+ mode: "scene",
75
+ width: display.width,
76
+ height: display.height,
77
+ pitch,
78
+ rows: total - 1,
79
+ marked: markedRow(total - 1),
80
+ wrapWidthPx: Math.floor(Math.min(display.width - 2 * PAD - TEXT_INSET, profile.displayWidthPx)),
81
+ rect: (display.shapes ?? []).includes("rect") && budget >= 5,
82
+ budget,
83
+ rowsPerText: profile.lineHeightPx ? total : profile.maxLines,
84
+ }
85
+ }
86
+ const width = display?.width ?? CANVAS.width
87
+ const height = display?.height ?? CANVAS.height
88
+ const {maxLines} = textBoxLines(profile, {w: width, h: profile.displayHeightPx ?? height})
89
+ const total = Math.max(MIN_ROWS, Math.min(profile.maxLines, maxLines))
90
+ return {
91
+ mode: "text",
92
+ width,
93
+ height,
94
+ pitch: profile.lineHeightPx ?? DEFAULT_PITCH,
95
+ rows: total - 1,
96
+ marked: markedRow(total - 1),
97
+ wrapWidthPx: Math.floor(Math.min(profile.displayWidthPx, width)),
98
+ rect: false,
99
+ budget,
100
+ rowsPerText: total,
101
+ }
102
+ }
103
+
104
+ export function textWrapWidth(layout: LensLayout, highlight: LensHighlight, measurer: TextMeasurer): number {
105
+ if (!usesBrackets(layout, highlight)) return layout.wrapWidthPx
106
+ return layout.wrapWidthPx - measurer.measureText(BRACKET_OPEN + BRACKET_CLOSE)
107
+ }
108
+
109
+ export function serializeRows(rows: string[]): string {
110
+ return rows.map((row) => (row === "" ? "​" : row)).join("\n")
111
+ }
112
+
113
+ function fit(measurer: TextMeasurer, text: string, widthPx: number): string {
114
+ if (measurer.measureText(text) <= widthPx) return text
115
+ let out = text
116
+ while (out && measurer.measureText(`${out}…`) > widthPx) out = out.slice(0, -1)
117
+ return out ? `${out.trimEnd()}…` : ""
118
+ }
119
+
120
+ /** The status row as one line: the state, then the detail when both fit, the state shortened first. */
121
+ export function statusLine(measurer: TextMeasurer, status: string, detail: string, widthPx: number): string {
122
+ if (!detail) return fit(measurer, status, widthPx)
123
+ const both = `${status} ${detail}`
124
+ if (measurer.measureText(both) <= widthPx) return both
125
+ const room = widthPx - measurer.measureText(` ${detail}`)
126
+ const state = room > measurer.measureText("x…") ? fit(measurer, status, room) : ""
127
+ return state ? `${state} ${detail}` : fit(measurer, status, widthPx)
128
+ }
129
+
130
+ function markText(layout: LensLayout, frame: LensFrame, row: string): string {
131
+ return row && usesBrackets(layout, frame.highlight) ? `${BRACKET_OPEN}${row}${BRACKET_CLOSE}` : row
132
+ }
133
+
134
+ export function lensElements(layout: LensLayout, frame: LensFrame, measurer: TextMeasurer): RenderElement[] {
135
+ const rows = Array.from({length: layout.rows}, (_, i) => frame.rows[i] ?? "")
136
+ const marked = frame.marked
137
+ if (layout.mode === "text") {
138
+ const body = rows.map((row, i) => (i === marked ? markText(layout, frame, row) : row))
139
+ const status = frame.toast
140
+ ? fit(measurer, frame.toast.replace(/\n/g, " · "), layout.wrapWidthPx)
141
+ : statusLine(measurer, frame.status, frame.detail, layout.wrapWidthPx)
142
+ return [
143
+ {
144
+ type: "text",
145
+ id: "reader-text",
146
+ box: {x: 0, y: 0, w: layout.width, h: layout.height},
147
+ text: serializeRows([...body, status]),
148
+ },
149
+ ]
150
+ }
151
+
152
+ const {pitch, width} = layout
153
+ const textWidth = width - 2 * PAD
154
+ const statusY = layout.rows * pitch
155
+ const elements: RenderElement[] = []
156
+ const block = (id: string, from: number, to: number, color: number) => {
157
+ for (let at = from, part = 1; at < to; at += layout.rowsPerText, part++) {
158
+ const end = Math.min(to, at + layout.rowsPerText)
159
+ if (part > 1 && rows.slice(at, end).every((row) => row === "")) continue
160
+ elements.push({
161
+ type: "text",
162
+ id: part === 1 ? id : `${id}-${part}`,
163
+ box: {x: PAD, y: at * pitch, w: textWidth, h: (end - at) * pitch},
164
+ text: serializeRows(rows.slice(at, end)),
165
+ style: {color},
166
+ })
167
+ }
168
+ }
169
+
170
+ if (marked === null) {
171
+ const dim = Math.max(0, Math.min(layout.rows, frame.dimBefore ?? 0))
172
+ block("reader-read", 0, dim, LENS_COLOR.read)
173
+ block("reader-next", dim, layout.rows, LENS_COLOR.marked)
174
+ } else {
175
+ block("reader-read", 0, marked, LENS_COLOR.read)
176
+ if (layout.rect && frame.highlight === "filled") {
177
+ elements.push({
178
+ type: "rect",
179
+ id: "reader-bar",
180
+ box: {x: 0, y: marked * pitch, w: width, h: pitch},
181
+ style: {fill: true, color: LENS_COLOR.bar, radius: 4},
182
+ })
183
+ }
184
+ elements.push({
185
+ type: "text",
186
+ id: "reader-mark",
187
+ box: {x: PAD, y: marked * pitch, w: textWidth, h: pitch},
188
+ text: serializeRows([markText(layout, frame, rows[marked] ?? "")]),
189
+ style: {color: LENS_COLOR.marked},
190
+ })
191
+ block("reader-next", marked + 1, layout.rows, LENS_COLOR.coming)
192
+ }
193
+
194
+ const statusBox = {y: statusY, h: layout.height - statusY}
195
+ const inner = textWidth - TEXT_INSET
196
+ const toastFits = layout.budget >= elements.length + 2
197
+ const toast = frame.toast && toastFits ? frame.toast : null
198
+ if (frame.toast && !toastFits) frame = {...frame, status: frame.toast.replace(/\n/g, " · "), detail: ""}
199
+ const split = !frame.toast && layout.budget > elements.length + 1
200
+ const detailWidth = frame.detail ? measurer.measureText(frame.detail) : 0
201
+ if (split && frame.detail && detailWidth + measurer.measureText(frame.status) + 24 <= inner) {
202
+ const detailBox = detailWidth + TEXT_INSET
203
+ elements.push(
204
+ {
205
+ type: "text",
206
+ id: "reader-status",
207
+ box: {x: PAD, ...statusBox, w: textWidth - detailBox},
208
+ text: frame.status || "​",
209
+ style: {color: LENS_COLOR.status},
210
+ },
211
+ {
212
+ type: "text",
213
+ id: "reader-detail",
214
+ box: {x: width - PAD - detailBox, ...statusBox, w: detailBox},
215
+ text: frame.detail,
216
+ style: {color: LENS_COLOR.status},
217
+ },
218
+ )
219
+ } else {
220
+ elements.push({
221
+ type: "text",
222
+ id: "reader-status",
223
+ box: {x: PAD, ...statusBox, w: textWidth},
224
+ text: statusLine(measurer, frame.status, frame.detail, inner) || "​",
225
+ style: {color: LENS_COLOR.status},
226
+ })
227
+ }
228
+
229
+ if (toast) {
230
+ const lines = toast.split("\n").map((line) => fit(measurer, line, textWidth - 2 * PAD))
231
+ const top = layout.rows - lines.length
232
+ for (let i = elements.length - 1; i >= 0; i--) {
233
+ const el = elements[i]
234
+ if (el.type !== "text" || !/^reader-(read|next)/.test(el.id ?? "")) continue
235
+ const from = el.box.y / pitch
236
+ const to = from + el.box.h / pitch
237
+ if (to <= top) continue
238
+ if (from >= top) elements.splice(i, 1)
239
+ else elements[i] = {...el, box: {...el.box, h: (top - from) * pitch}, text: serializeRows(rows.slice(from, top))}
240
+ }
241
+ elements.push({
242
+ type: "text",
243
+ id: "reader-toast",
244
+ box: {x: PAD, y: top * pitch, w: textWidth, h: lines.length * pitch},
245
+ text: serializeRows(lines),
246
+ style: {color: LENS_COLOR.marked, border: 2, radius: 6},
247
+ })
248
+ }
249
+ return elements
250
+ }
@@ -58,3 +58,8 @@ export type _Typed = TypedUiGlobal<{foo: number}>
58
58
  // disappear. The asymmetric exports are enforced by package.json's
59
59
  // `exports` map at consumer time; this file proves the source files
60
60
  // expose what they should.)
61
+
62
+ // ----- @glassly/miniapp/reader -------------------------------------------
63
+ // A real session is a ReaderSession, so apps pass theirs straight in.
64
+ import type {ReaderSession} from "./reader/index"
65
+ export const sessionIsReaderSession = (session: MiniappSession): ReaderSession => session