@solidrt/core 0.0.11 → 0.0.14

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,207 @@
1
+ // Headless text-input mechanism. These primitives own the objective parts of
2
+ // an editable single-line field -- the value buffer (text + caret/selection)
3
+ // and the scroll-to-caret geometry -- and nothing with a UI opinion. Caret
4
+ // blink, keybindings, placeholder and styling are policy and belong to the
5
+ // component (the "skin") that composes these.
6
+
7
+ import { createSignal, flush } from "@solidjs/signals"
8
+ import { getBoundingBox, measureText } from "./core"
9
+ import { onLayout } from "./window"
10
+
11
+ /**
12
+ * A text selection as anchor/focus character offsets, following the same model
13
+ * as the platform editors (Flutter's TextSelection, the DOM Selection): the
14
+ * anchor is where the selection started, the focus is the moving end where the
15
+ * caret sits. A collapsed selection (anchor === focus) is a plain caret.
16
+ */
17
+ export type Selection = { anchor: number; focus: number }
18
+
19
+ export type MoveDirection = "left" | "right" | "start" | "end"
20
+
21
+ export type TextBufferOptions = {
22
+ /**
23
+ * Controlled value accessor. When it returns a string, the buffer mirrors it
24
+ * and edits flow out only through onInput (the internal text is bypassed).
25
+ * The selection is always buffer-owned editing state regardless.
26
+ */
27
+ value?: () => string | undefined
28
+ /** Initial value when uncontrolled. */
29
+ defaultValue?: string
30
+ /** Called with the new text after every edit, already clamped to maxLength. */
31
+ onInput?: (value: string) => void
32
+ /** Max length; inserts past it are clamped. */
33
+ maxLength?: () => number | undefined
34
+ }
35
+
36
+ export type TextBuffer = {
37
+ /** Current text: the controlled value if provided, else internal state. */
38
+ value(): string
39
+ /** Current selection, clamped to the text length. Collapsed = a caret. */
40
+ selection(): Selection
41
+ /** The focus offset (where the caret sits). */
42
+ caret(): number
43
+ /** Replace the current selection with text, then collapse the caret after it. */
44
+ insertText(text: string): void
45
+ /** Delete the selection if any, else the character before the caret. */
46
+ deleteBackward(): void
47
+ /** Delete the selection if any, else the character after the caret. */
48
+ deleteForward(): void
49
+ /** Move the caret. `extend` keeps the anchor to grow a selection (else collapses). */
50
+ move(direction: MoveDirection, options?: { extend?: boolean }): void
51
+ /** Set the selection directly (offsets are clamped to the text length). */
52
+ setSelection(anchor: number, focus: number): void
53
+ /** Replace the whole value, caret to the end. */
54
+ setValue(next: string): void
55
+ /** Clear to empty. */
56
+ clear(): void
57
+ }
58
+
59
+ /**
60
+ * An editable text buffer that bridges controlled/uncontrolled use and owns the
61
+ * caret/selection. With a `value` accessor the buffer is controlled: edits do
62
+ * not mutate internal text, they only call `onInput` so the owner can update its
63
+ * source. Without one it holds the text itself. The selection is always
64
+ * buffer-owned state and is clamped to the current text length on read, so an
65
+ * external truncation of a controlled value cannot leave the caret dangling.
66
+ * Every edit is clamped to `maxLength`.
67
+ */
68
+ export function createTextBuffer(options: TextBufferOptions = {}): TextBuffer {
69
+ let initial = options.defaultValue ?? ""
70
+ let [internalValue, setInternalValue] = createSignal(initial)
71
+ let [selectionState, setSelectionState] = createSignal<Selection>({
72
+ anchor: initial.length,
73
+ focus: initial.length,
74
+ })
75
+
76
+ let value = () => options.value?.() ?? internalValue()
77
+
78
+ let selection = (): Selection => {
79
+ let len = value().length
80
+ let s = selectionState()
81
+ return { anchor: Math.min(s.anchor, len), focus: Math.min(s.focus, len) }
82
+ }
83
+
84
+ // Ordered selection bounds [start, end).
85
+ let range = () => {
86
+ let { anchor, focus } = selection()
87
+ return anchor <= focus ? [anchor, focus] : [focus, anchor]
88
+ }
89
+
90
+ let setCaret = (offset: number) => setSelectionState({ anchor: offset, focus: offset })
91
+
92
+ // Apply a text edit and place the caret, clamping to maxLength.
93
+ let apply = (next: string, caret: number) => {
94
+ let max = options.maxLength?.()
95
+ if (max != null && next.length > max) next = next.slice(0, max)
96
+ caret = Math.min(caret, next.length)
97
+ if (options.value?.() == null) setInternalValue(next)
98
+ setCaret(caret)
99
+ options.onInput?.(next)
100
+ }
101
+
102
+ return {
103
+ value,
104
+ selection,
105
+ caret: () => selection().focus,
106
+
107
+ insertText: (text) => {
108
+ let v = value()
109
+ let [start, end] = range()
110
+ apply(v.slice(0, start) + text + v.slice(end), start + text.length)
111
+ },
112
+
113
+ deleteBackward: () => {
114
+ let v = value()
115
+ let [start, end] = range()
116
+ if (start !== end) apply(v.slice(0, start) + v.slice(end), start)
117
+ else if (start > 0) apply(v.slice(0, start - 1) + v.slice(start), start - 1)
118
+ },
119
+
120
+ deleteForward: () => {
121
+ let v = value()
122
+ let [start, end] = range()
123
+ if (start !== end) apply(v.slice(0, start) + v.slice(end), start)
124
+ else if (end < v.length) apply(v.slice(0, end) + v.slice(end + 1), end)
125
+ },
126
+
127
+ move: (direction, opts) => {
128
+ let extend = opts?.extend ?? false
129
+ let { anchor, focus } = selection()
130
+ let len = value().length
131
+ // A non-extending left/right on a range collapses to the near edge.
132
+ if (!extend && anchor !== focus && (direction === "left" || direction === "right")) {
133
+ setCaret(direction === "left" ? Math.min(anchor, focus) : Math.max(anchor, focus))
134
+ return
135
+ }
136
+ let next = focus
137
+ if (direction === "left") next = Math.max(0, focus - 1)
138
+ else if (direction === "right") next = Math.min(len, focus + 1)
139
+ else if (direction === "start") next = 0
140
+ else if (direction === "end") next = len
141
+ setSelectionState({ anchor: extend ? anchor : next, focus: next })
142
+ },
143
+
144
+ setSelection: (anchor, focus) => {
145
+ let len = value().length
146
+ setSelectionState({ anchor: Math.min(anchor, len), focus: Math.min(focus, len) })
147
+ },
148
+
149
+ setValue: (next) => apply(next, next.length),
150
+ clear: () => apply("", 0),
151
+ }
152
+ }
153
+
154
+ export type CaretScrollInput = {
155
+ text: string
156
+ fontSize: number
157
+ /** Caret offset into `text`. Defaults to the text end. */
158
+ caret?: number
159
+ /** Px reserved so the caret stays visible at the viewport edge. Default 0. */
160
+ caretWidth?: number
161
+ }
162
+
163
+ /**
164
+ * Returns the horizontal scroll offset that keeps the caret within the viewport
165
+ * node. The offset is retained between frames and only adjusted when the caret
166
+ * would fall outside the visible range (scrolled left when the caret runs past
167
+ * the right edge, right when it moves before the left edge), so stationary text
168
+ * does not jump. The viewport width and offset are computed in onLayout and the
169
+ * synchronous flush drains the update before paint, so the scroll tracks a caret
170
+ * or width change in the same frame. Pure geometry: no caret rendering and no
171
+ * placeholder/visual policy.
172
+ */
173
+ export function createCaretScroll(
174
+ viewport: () => { id: number } | undefined,
175
+ input: () => CaretScrollInput,
176
+ ): () => number {
177
+ let [scrollX, setScrollX] = createSignal(0)
178
+
179
+ onLayout(() => {
180
+ let node = viewport()
181
+ if (!node) return
182
+ let vw = getBoundingBox(node)?.width ?? 0
183
+ let { text, fontSize, caret, caretWidth = 0 } = input()
184
+ let len = text.length
185
+ let c = caret == null ? len : Math.max(0, Math.min(caret, len))
186
+
187
+ let totalWidth = measureText(text, { fontSize }).width
188
+ let caretX = c >= len ? totalWidth : measureText(text.slice(0, c), { fontSize }).width
189
+ let maxScroll = Math.max(0, totalWidth + caretWidth - vw)
190
+
191
+ let cur = scrollX()
192
+ let next = cur
193
+ if (vw <= 0) {
194
+ next = 0
195
+ } else if (caretX < cur) {
196
+ next = caretX
197
+ } else if (caretX + caretWidth > cur + vw) {
198
+ next = caretX + caretWidth - vw
199
+ }
200
+ next = Math.max(0, Math.min(next, maxScroll))
201
+
202
+ if (next !== cur) setScrollX(next)
203
+ flush()
204
+ })
205
+
206
+ return scrollX
207
+ }
package/src/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference types="@solidrt/flux-types" />
2
2
 
3
3
  import type { JSX as SolidJSX } from "@solidjs/signals"
4
+ import type { Gradient } from "./color"
4
5
 
5
6
  // UI event bus (lattice), provided by the runtime as a builtin module.
6
7
  // on/once return an unsubscribe function.
@@ -20,59 +21,26 @@ declare module "srt:dev" {
20
21
  export function stop(): void
21
22
  }
22
23
 
23
- declare global {
24
- function requestAnimationFrame(callback: (time: number) => void): number
25
- function cancelAnimationFrame(id: number): void
26
-
27
- let ffi: {
28
- createRoot(id: number): void
29
- createNode(id: number, kind: string): void
30
- insertNode(parentId: number, nodeId: number, anchorId?: number): void
31
- deleteNode(parentId: number, nodeId: number): void
32
- setProperty(nodeId: number, name: string, value: unknown): void
33
- setTextInputActive(active: boolean): void
34
- requestFrame(): void
35
- measureText(text: string, options?: MeasureTextOptions): { width: number, height: number }
36
- getBoundingBox(id: number): { x: number, y: number, width: number, height: number } | null
37
- }
24
+ // Frame draw (lattice runner). renderFrame() synchronously renders the current
25
+ // frame: layout, the postLayout hook, paint and hover refresh, then builds and
26
+ // submits the display list. To schedule a future frame instead, use
27
+ // requestFrame() from "flux:rendertree". The tree-building surface itself is
28
+ // "flux:rendertree" (from @solidrt/flux-types).
29
+ declare module "srt:render" {
30
+ export function renderFrame(): void
31
+ }
38
32
 
39
- let gpu: {
40
- createTexture(data: Uint8Array, width: number, height: number): number
41
- createMutableTexture(data: Uint8Array, width: number, height: number): number
42
- uploadTexture(textureId: number, offset?: number): void
43
- createShader(
44
- fragmentSrc: string,
45
- width: number,
46
- height: number,
47
- params?: Record<string, number>,
48
- textures?: Record<string, number>,
49
- ): number
50
- setShaderParams(textureId: number, params: Record<string, number>): void
33
+ declare global {
34
+ let image: {
51
35
  decodeImage(bytes: Uint8Array): { data: Uint8Array, width: number, height: number }
52
36
  }
53
37
 
54
- let camera: {
55
- listCameras(): { id: number, name: string, facing: "front" | "back" | "unknown" }[]
56
- open(options: { camera?: number, facing?: "front" | "back", width?: number, height?: number, scan?: string[] }):
57
- Promise<{ handle: number, texture: number, width: number, height: number }>
58
- setBarcodeCallback(handle: number, callback: (result: { data: string, format: "qr" }) => void): void
59
- scanImage(data: Uint8Array, width: number, height: number): { data: string, format: "qr" }[]
60
- close(handle: number): void
61
- }
62
-
63
- let microphone: {
64
- listMicrophones(): { id: number, name: string }[]
65
- open(options: { microphone?: number, sampleRate?: number }): { handle: number, sampleRate: number }
66
- read(handle: number): Float32Array
67
- close(handle: number): void
68
- }
69
-
70
38
  let speech: {
71
39
  start(options: {
72
- model: Uint8Array, vadModel: Uint8Array, language?: string, microphone?: number,
73
- singleUtterance?: boolean, interimResults?: boolean, wakeWord?: Uint8Array | string | string[], wakeThreshold?: number,
40
+ model: Uint8Array, vadModel: Uint8Array, lang?: string, microphone?: number,
41
+ continuous?: boolean, interimResults?: boolean, wakeWord?: Uint8Array | string | string[], wakeThreshold?: number,
74
42
  }): Promise<{ handle: number }>
75
- setResultCallback(handle: number, callback: (result: { text: string, final: boolean }) => void): void
43
+ setResultCallback(handle: number, callback: (result: { transcript: string, isFinal: boolean }) => void): void
76
44
  setSpeechStartCallback(handle: number, callback: () => void): void
77
45
  setSpeechEndCallback(handle: number, callback: () => void): void
78
46
  setWakeCallback(handle: number, callback: () => void): void
@@ -80,14 +48,6 @@ declare global {
80
48
  }
81
49
  }
82
50
 
83
- export interface MeasureTextOptions {
84
- fontFamily?: "sans" | "mono" | (string & {})
85
- fontSize?: number
86
- fontStyle?: "normal" | "italic"
87
- fontWeight?: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
88
- maxLines?: number
89
- }
90
-
91
51
  type Children = SolidJSX.Element
92
52
 
93
53
  interface FlexboxProps {
@@ -136,6 +96,7 @@ export interface LayoutProps extends FlexboxProps, GridProps {
136
96
  minHeight?: Dimension
137
97
  maxWidth?: Dimension
138
98
  maxHeight?: Dimension
99
+ aspectRatio?: number | (string & {})
139
100
 
140
101
  padding?: Dimension
141
102
  paddingTop?: Dimension
@@ -154,11 +115,12 @@ export interface LayoutProps extends FlexboxProps, GridProps {
154
115
  overflowY?: "visible" | "clip" | "hidden" | "scroll"
155
116
  }
156
117
 
157
- // Colors are CSS color strings, parsed to a packed u32 by parseColorToU32.
118
+ /** Colors are CSS color strings, parsed to a packed u32 by `parseColor`. */
158
119
  export type Color = string
159
120
 
160
121
  export interface PaintProps {
161
- color?: Color
122
+ // A solid color, or a gradient from createLinearGradient/createRadialGradient.
123
+ color?: Color | Gradient
162
124
  blendMode?: "clear" | "source" | "destination" | "source-over" | "destination-over" | "source-in" | "destination-in" | "source-out" | "destination-out" | "source-atop" | "destination-atop" | "xor" | "plus" | "modulate" | "screen" | "overlay" | "darken" | "lighten" | "color-dodge" | "color-burn" | "hard-light" | "soft-light" | "difference" | "exclusion" | "multiply" | "hue" | "saturation" | "color" | "luminosity"
163
125
  drawStyle?: "fill" | "stroke" | "stroke-and-fill"
164
126
  strokeCap?: "butt" | "round" | "square"
@@ -191,14 +153,22 @@ export interface TransformProps {
191
153
  scrollY?: number
192
154
  }
193
155
 
156
+ // Window-relative pointer coordinates are reported as clientX/clientY (matching
157
+ // the DOM MouseEvent). pointerType distinguishes mouse from touch; button is the
158
+ // pressed button on down/up (0 = primary); the modifier flags mirror the DOM.
194
159
  export interface PointerEvent {
195
- x: number
196
- y: number
160
+ clientX: number
161
+ clientY: number
162
+ pointerId: number
163
+ pointerType: "mouse" | "touch" | "pen" | (string & {})
164
+ button?: number
165
+ shiftKey: boolean
166
+ ctrlKey: boolean
167
+ altKey: boolean
168
+ metaKey: boolean
197
169
  }
198
170
 
199
- export interface WheelEvent {
200
- x: number
201
- y: number
171
+ export interface WheelEvent extends PointerEvent {
202
172
  deltaX: number
203
173
  deltaY: number
204
174
  }
@@ -296,7 +266,15 @@ export interface LineProps extends PaintProps, PointerProps {
296
266
 
297
267
  export interface PathProps extends Position, PaintProps, PointerProps {
298
268
  d?: string
299
- fillRule?: "nonZero" | "evenOdd"
269
+ fillRule?: "nonzero" | "evenodd"
270
+ }
271
+
272
+ export interface SvgProps extends Position, PointerProps {
273
+ // A whole SVG document as a string (an imported asset, a fetched string, or a
274
+ // template literal). Parsed and rendered as one unit; takes no JSX children.
275
+ src?: string
276
+ // Drives currentColor in the document. Explicit fills/strokes still win.
277
+ color?: Color
300
278
  }
301
279
 
302
280
  export interface TextProps extends PaintProps, PointerProps {
package/src/window.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { onCleanup, onSettled, flush } from "@solidjs/signals"
1
+ import { createSignal, onCleanup, onSettled, flush } from "@solidjs/signals"
2
+ import { requestFrame } from "flux:rendertree"
3
+ import { renderFrame } from "srt:render"
2
4
  import { on, once } from "srt:events"
3
5
  import { getEventHandler, getFocusedNodeId, setFocus } from "./core"
4
6
 
@@ -28,12 +30,12 @@ export function onFrame(fn: (tick: number, frame: number, rate: number) => void)
28
30
  frameId = nextFrameId++
29
31
  animationFrames.set(frameId, extendedFn)
30
32
  // A pending onFrame callback is a standing request for the next frame.
31
- ffi.requestFrame()
33
+ requestFrame()
32
34
  }
33
35
 
34
36
  frameId = nextFrameId++
35
37
  animationFrames.set(frameId, extendedFn)
36
- ffi.requestFrame()
38
+ requestFrame()
37
39
 
38
40
  let cleanup = () => animationFrames.delete(frameId)
39
41
  onCleanup(cleanup)
@@ -42,6 +44,8 @@ export function onFrame(fn: (tick: number, frame: number, rate: number) => void)
42
44
 
43
45
  // ------ Resize ----------------
44
46
 
47
+ // Insets: each value is the distance from the corresponding window edge, like
48
+ // CSS env(safe-area-inset-*).
45
49
  interface SafeArea {
46
50
  top: number
47
51
  left: number
@@ -62,10 +66,86 @@ export function onResize(fn: (data: ResizeEvent) => void) {
62
66
  return unsubscribe
63
67
  }
64
68
 
65
- // Fires after layout has been computed for the current frame but before paint.
66
- // Setting properties that affect layout from this callback will be picked up
67
- // by a re-layout pass before painting (one extra pass; cascades beyond that
68
- // paint stale).
69
+ // ------ Reactive window state ----------------
70
+
71
+ // Singleton accessors over the same events as onResize / onWindowFocus. There
72
+ // is one window, so these are bare accessors rather than a createX instance.
73
+ // Lazily subscribed on first read (resize is sticky, so the first read sees the
74
+ // current value); app-lifetime, so no onCleanup.
75
+
76
+ let sizeAccessor: (() => { width: number; height: number }) | undefined
77
+ let safeAreaAccessor: (() => SafeArea) | undefined
78
+ let displayScaleAccessor: (() => number) | undefined
79
+
80
+ function ensureResizeState() {
81
+ if (sizeAccessor) return
82
+ let [size, setSize] = createSignal({ width: 0, height: 0 })
83
+ let [safe, setSafe] = createSignal<SafeArea>({ top: 0, left: 0, right: 0, bottom: 0 })
84
+ let [scale, setScale] = createSignal(1)
85
+ on("resize", (e: ResizeEvent) => {
86
+ setSize({ width: e.width, height: e.height })
87
+ setSafe(e.safeArea)
88
+ setScale(e.displayScale)
89
+ })
90
+ sizeAccessor = size
91
+ safeAreaAccessor = safe
92
+ displayScaleAccessor = scale
93
+ }
94
+
95
+ /** Current window size, as a reactive accessor. */
96
+ export function windowSize(): { width: number; height: number } {
97
+ ensureResizeState()
98
+ return sizeAccessor!()
99
+ }
100
+
101
+ /** Current safe-area insets, as a reactive accessor. */
102
+ export function safeArea(): SafeArea {
103
+ ensureResizeState()
104
+ return safeAreaAccessor!()
105
+ }
106
+
107
+ /** Current display scale (device pixel ratio), as a reactive accessor. */
108
+ export function displayScale(): number {
109
+ ensureResizeState()
110
+ return displayScaleAccessor!()
111
+ }
112
+
113
+ let focusedAccessor: (() => boolean) | undefined
114
+
115
+ /** Whether the window currently has focus, as a reactive accessor. */
116
+ export function windowFocused(): boolean {
117
+ if (!focusedAccessor) {
118
+ let [focused, setFocused] = createSignal(true)
119
+ on("windowFocus", () => setFocused(true))
120
+ on("windowBlur", () => setFocused(false))
121
+ focusedAccessor = focused
122
+ }
123
+ return focusedAccessor()
124
+ }
125
+
126
+ let keyboardHeightAccessor: (() => number) | undefined
127
+
128
+ /**
129
+ * Height in logical pixels that the on-screen keyboard overlaps the window
130
+ * (0 when hidden or on platforms without a soft keyboard), as a reactive
131
+ * accessor. The window is not resized for the keyboard, so pad or lift content
132
+ * by this much to keep it above the keyboard.
133
+ */
134
+ export function keyboardHeight(): number {
135
+ if (!keyboardHeightAccessor) {
136
+ let [height, setHeight] = createSignal(0)
137
+ on("keyboardVisibility", ({ height: h }: { height: number }) => setHeight(h ?? 0))
138
+ keyboardHeightAccessor = height
139
+ }
140
+ return keyboardHeightAccessor()
141
+ }
142
+
143
+ /**
144
+ * Fires after layout has been computed for the current frame but before paint.
145
+ * Setting properties that affect layout from this callback will be picked up
146
+ * by a re-layout pass before painting (one extra pass; cascades beyond that
147
+ * paint stale).
148
+ */
69
149
  export function onLayout(fn: () => void) {
70
150
  let unsubscribe = on("postLayout", fn)
71
151
  onCleanup(unsubscribe)
@@ -108,7 +188,7 @@ export function attachWindow(_nodeId: number) {
108
188
  for (let fn of frames.values()) fn(t, frame, refreshRate)
109
189
  }
110
190
  flush()
111
- draw()
191
+ renderFrame()
112
192
  }
113
193
 
114
194
  onSettled(() => {
@@ -1,55 +0,0 @@
1
- import { createSignal, onCleanup } from "@solidjs/signals"
2
- import { openCamera, type BarcodeResult, type Camera } from "./camera"
3
-
4
- // Convenience viewfinder over openCamera: opens on mount, renders the stream
5
- // texture, closes on cleanup. Use openCamera directly for anything it does not
6
- // cover; this is just composition, no extra capability.
7
-
8
- export interface CameraViewProps {
9
- /** Explicit device id from listCameras(); takes precedence over facing. */
10
- camera?: number
11
- facing?: "front" | "back"
12
- /**
13
- * Size hint for the stream and explicit size of the view. Omit height to
14
- * follow the stream's aspect ratio, which can flip when a phone rotates.
15
- */
16
- width?: number
17
- height?: number
18
- scan?: "qr"[]
19
- onReady?: (cam: Camera) => void
20
- onError?: (error: Error) => void
21
- onBarcode?: (result: BarcodeResult) => void
22
- }
23
-
24
- export function CameraView(props: CameraViewProps) {
25
- let [texture, setTexture] = createSignal<number | undefined>(undefined)
26
- let cam: Camera | undefined
27
- let disposed = false
28
-
29
- openCamera({
30
- camera: props.camera,
31
- facing: props.facing,
32
- width: props.width,
33
- height: props.height,
34
- scan: props.scan,
35
- })
36
- .then((opened) => {
37
- if (disposed) {
38
- opened.close()
39
- return
40
- }
41
- cam = opened
42
- if (props.onBarcode) opened.onBarcode(props.onBarcode)
43
- setTexture(opened.texture)
44
- props.onReady?.(opened)
45
- })
46
- .catch((e) => props.onError?.(e instanceof Error ? e : new Error(String(e))))
47
-
48
- onCleanup(() => {
49
- disposed = true
50
- cam?.close()
51
- cam = undefined
52
- })
53
-
54
- return <texture src={texture()} width={props.width} height={props.height} />
55
- }
package/src/speech.ts DELETED
@@ -1,67 +0,0 @@
1
- // Speech recognition. A session captures the microphone, segments utterances
2
- // by silence (Silero VAD) and transcribes each one with Whisper, delivering
3
- // final transcripts through onResult. With wakeWord the session starts
4
- // asleep behind an efficient wake word detector (livekit-wakeword) and only
5
- // transcribes after the wake word. startRecognition resolves once the models
6
- // are loaded and listening has begun; it rejects when loading fails.
7
- // Models are passed as bytes so any source composes: flux:fs file(), fetch
8
- // (incl. the dev-server file proxy), or a download cache layered on top.
9
- // Requires a runtime built with speech support.
10
-
11
- export type SpeechOptions = {
12
- /** A ggml Whisper model (file contents, e.g. ggml-tiny.en.bin). */
13
- model: Uint8Array
14
- /** A ggml Silero VAD model (file contents). */
15
- vadModel: Uint8Array
16
- /** Whisper language code; "auto" detects (multilingual models only). Default "en". */
17
- language?: string
18
- /** Explicit microphone device id from listMicrophones(). */
19
- microphone?: number
20
- /** Stop automatically after the first final result (with wakeWord: re-arm instead, one result per wake). */
21
- singleUtterance?: boolean
22
- /** Also deliver snapshot transcripts (final: false) while an utterance is still being spoken. */
23
- interimResults?: boolean
24
- /**
25
- * Wake word: start asleep, fire onWake when it is heard, then transcribe
26
- * the speech that follows. How the wake word is specified depends on the
27
- * engine. The current engine detects with a trained classifier and takes
28
- * the model's bytes (livekit-wakeword ONNX, e.g. the pretrained "hey
29
- * livekit"; custom phrases are trained offline with its toolkit). Phrase
30
- * strings are reserved for engines that match text; passing them to this
31
- * engine rejects with an error.
32
- */
33
- wakeWord?: Uint8Array | string | string[]
34
- /** Detector confidence (0..1) that counts as a wake. Default 0.5. */
35
- wakeThreshold?: number
36
- }
37
-
38
- export type SpeechResult = {
39
- /** Transcript of the utterance (a snapshot of it when final is false). */
40
- text: string
41
- /** True for the completed utterance, false for interim snapshots. */
42
- final: boolean
43
- }
44
-
45
- export type SpeechSession = {
46
- /** Receive transcripts (replaces any previous callback). */
47
- onResult(callback: (result: SpeechResult) => void): void
48
- /** The user started speaking (replaces any previous callback). */
49
- onSpeechStart(callback: () => void): void
50
- /** The utterance ended; its final result follows once transcribed. */
51
- onSpeechEnd(callback: () => void): void
52
- /** The wake word was heard (wakeWordModel sessions only). */
53
- onWake(callback: () => void): void
54
- /** Release the microphone and discard any utterance in progress. */
55
- stop(): void
56
- }
57
-
58
- export async function startRecognition(options: SpeechOptions): Promise<SpeechSession> {
59
- let started = await speech.start(options)
60
- return {
61
- onResult: (callback: (result: SpeechResult) => void) => speech.setResultCallback(started.handle, callback),
62
- onSpeechStart: (callback: () => void) => speech.setSpeechStartCallback(started.handle, callback),
63
- onSpeechEnd: (callback: () => void) => speech.setSpeechEndCallback(started.handle, callback),
64
- onWake: (callback: () => void) => speech.setWakeCallback(started.handle, callback),
65
- stop: () => speech.stop(started.handle),
66
- }
67
- }