@lincyaw/kernel 1.0.0-rc.4 → 1.0.0-rc.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,7 @@ Plugins declare it as a peer dependency, so they share the application's copy:
14
14
 
15
15
  ## Entries
16
16
 
17
- - `@lincyaw/kernel`: the contracts (`PresmiumNodePlugin`, `PluginDefinition`, `DirectiveSpec`, `IRPass`, `Renderer`, `RenderContext`, `Check`, the service tokens), the canonical authoring types (`TeachingDocument`, `DocumentEditOperation`), `DOCUMENT_ENTRY`, `DOCUMENT_SCHEMA_VERSION`, `DOCUMENT_JSON_SCHEMA`, `validateDocument`, the derived IR types (`IRDocument` and its nodes), `KERNEL_VERSION`, `IR_VERSION`, and plain helpers such as `docBlocks`, `withProp`, `deckLocale`, `jsonResponse` and `problemResponse`.
17
+ - `@lincyaw/kernel`: the contracts (`PresmiumNodePlugin`, `PluginDefinition`, `IRPass`, `Renderer`, `RenderContext`, `Check`, the service tokens), the canonical authoring types (`TeachingDocument`, `DocumentEditOperation`), `DOCUMENT_ENTRY`, `DOCUMENT_SCHEMA_VERSION`, `DOCUMENT_JSON_SCHEMA`, `validateDocument`, the derived IR types (`IRDocument` and its nodes), `KERNEL_VERSION`, `IR_VERSION`, and plain helpers such as `docBlocks`, `withProp`, `deckLocale`, `jsonResponse` and `problemResponse`.
18
18
  - `@lincyaw/kernel/contracts`, `@lincyaw/kernel/ir`: the contracts (types, service tokens and the preview protocol's parsers) or the IR types alone; the helpers are only in `@lincyaw/kernel`.
19
19
  - `@lincyaw/kernel/reading`: plain functions that read a compiled document, the IR (`chapters`, `blockText`, `narrationAt`, `tokenize`); they do no I/O.
20
20
  - `@lincyaw/kernel/node`: helpers for Node code: `copyPublished` and `isInside` (its other exports are internal).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/kernel",
3
- "version": "1.0.0-rc.4",
3
+ "version": "1.0.0-rc.41",
4
4
  "description": "Presmium's shared kernel: the IR and plugin contracts, the service registry, anchors and checks",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -15,6 +15,7 @@
15
15
  "ir"
16
16
  ],
17
17
  "type": "module",
18
+ "sideEffects": false,
18
19
  "engines": {
19
20
  "node": ">=22.19"
20
21
  },
@@ -39,6 +40,7 @@
39
40
  "json-schema-library": "11.6.16",
40
41
  "parse5": "7.3.0",
41
42
  "semver": "^7.8.5",
42
- "proper-lockfile": "4.1.2"
43
+ "proper-lockfile": "4.1.2",
44
+ "resolve.exports": "2.0.4"
43
45
  }
44
46
  }
@@ -8,7 +8,7 @@ import type { DocumentChangePlan } from './authoring.ts'
8
8
 
9
9
  export const DOCUMENT_ACTION_MAX_REQUEST_CHARS = 4096
10
10
  export const DOCUMENT_ACTION_MAX_RESPONSE_CHARS = 7168
11
- export const DOCUMENT_AUTHORING_ACTION_TYPES = ['comment.create', 'comment.reply', 'comment.update', 'comment.react', 'comment.resolve', 'comment.reopen', 'comment.delete', 'voice.set', 'component.parameters.patch', 'component.step-parameters.patch', 'component.step-parameters.reset', 'component.definition.use', 'caption.timing.set', 'caption.timing.reset'] as const
11
+ export const DOCUMENT_AUTHORING_ACTION_TYPES = ['comment.create', 'comment.reply', 'comment.update', 'comment.react', 'comment.resolve', 'comment.reopen', 'comment.delete', 'voice.set', 'component.parameters.patch', 'component.step-parameters.patch', 'component.step-parameters.reset', 'component.definition.use', 'caption.timing.set', 'caption.timing.reset', 'caption.scale.set'] as const
12
12
  export const DOCUMENT_PARAMETER_MAX_CHANGES = 32
13
13
  export const DOCUMENT_PARAMETER_MAX_PATH_DEPTH = 16
14
14
  /** Paths belong only to this component's parameters; set replaces an array item or appends at its length. */
@@ -39,9 +39,9 @@ export interface DocumentComponentImpactAnswer {
39
39
  collection: DocumentComponentImpactCollection, offset: number, total: number, entries: string[], nextOffset?: number, complete: boolean
40
40
  }
41
41
  /** New recording adoption belongs to the real-media service, never a raw settings action. */
42
- export type DocumentAuthoringAction = DocumentCommentAction | DocumentComponentAction | DocumentCaptionTimingAction | { type: 'voice.set', scope: DocumentVoiceScope, source: Exclude<VoiceSource, { type: 'recording' }> | null, replaceOverrides?: boolean }
42
+ export type DocumentAuthoringAction = DocumentCommentAction | DocumentComponentAction | DocumentCaptionTimingAction | { type: 'caption.scale.set', scale: number } | { type: 'voice.set', scope: DocumentVoiceScope, source: Exclude<VoiceSource, { type: 'recording' }> | null, replaceOverrides?: boolean }
43
43
  export interface DocumentAuthoringActionRequest { id: string, source: CanonicalSource, action: DocumentAuthoringAction }
44
- export type DocumentAuthoringActionEffect = { kind: 'caption', value: { stepId: string, segments: number, changed: boolean, mode: 'manual' | 'automatic' } } | { kind: 'comment', value: DocumentCommentEffect } | { kind: 'voice', scope: DocumentVoiceScope['kind'], value: DocumentVoiceImpact, recordings: { preserved: number, removed: number } } | { kind: 'component', value: DocumentComponentEffect }
44
+ export type DocumentAuthoringActionEffect = { kind: 'caption-scale', value: { scale: number, changed: boolean } } | { kind: 'caption', value: { stepId: string, segments: number, changed: boolean, mode: 'manual' | 'automatic' } } | { kind: 'comment', value: DocumentCommentEffect } | { kind: 'voice', scope: DocumentVoiceScope['kind'], value: DocumentVoiceImpact, recordings: { preserved: number, removed: number } } | { kind: 'component', value: DocumentComponentEffect }
45
45
  export interface DocumentAuthoringActionPlan extends DocumentChangePlan {
46
46
  action: DocumentAuthoringAction
47
47
  effect: DocumentAuthoringActionEffect
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Pictures of a document as its presentation shows it, taken by the workspace service in a headless browser: the rest
3
+ * frame of each step, or a moment into a step's animation, with the boxes of the elements an agent asks about. Agents
4
+ * (over MCP) check animations and layout with it instead of scripting a browser.
5
+ */
6
+
7
+ /** At most this many pictures a request: each is one image in a tool result. */
8
+ export const CAPTURE_MAX_FRAMES = 12
9
+ /** At most this many boxes a picture reports for `measure`. */
10
+ export const CAPTURE_MAX_BOXES = 200
11
+
12
+ export interface SlideCaptureRequest {
13
+ /** The page (section) id, or its number from 1. */
14
+ section: string
15
+ /** The steps to show, from 0; every step of the page when omitted. */
16
+ steps?: number[]
17
+ /** Milliseconds into each step's animation; each step's rest frame (its final state) when omitted. */
18
+ at?: number[]
19
+ appearance?: 'light' | 'dark'
20
+ /** Device pixels per slide pixel. */
21
+ scale?: 1 | 2
22
+ /** A region of the slide, in slide pixels. */
23
+ clip?: { x: number, y: number, width: number, height: number }
24
+ /** A CSS selector: the boxes of the matching elements in the slide are reported, in slide pixels. */
25
+ measure?: string
26
+ /**
27
+ * How the presentation reaches each step: `direct` (default) draws it from its step and moment; `backward` shows the
28
+ * step after it first and goes back as a presenter does, with the page's own clock, to check that going back works.
29
+ */
30
+ arrive?: 'direct' | 'backward'
31
+ /** Draw the presentation's captions over the slide (default true); false shows the whole slide, the narration being in `narration`. */
32
+ captions?: boolean
33
+ }
34
+
35
+ export interface ReadingCaptureRequest {
36
+ /** The page (section) id, or its number from 1. */
37
+ section: string
38
+ /** The window's width in CSS pixels (default 1280; 390 for a phone). */
39
+ width?: number
40
+ appearance?: 'light' | 'dark'
41
+ /** Device pixels per CSS pixel. */
42
+ scale?: 1 | 2
43
+ /** A CSS selector: the boxes of the matching elements in the page's section are reported, in CSS pixels. */
44
+ measure?: string
45
+ }
46
+
47
+ export interface CapturedReading {
48
+ section: string
49
+ /** The PNG file of the picture, in the service's state directory. */
50
+ file: string
51
+ /** The section's size in CSS pixels; the picture is cut at `CAPTURE_MAX_HEIGHT`. */
52
+ width: number
53
+ height: number
54
+ cut: boolean
55
+ boxes?: CapturedBox[]
56
+ }
57
+
58
+ export interface ReadingCaptureResult {
59
+ document: string
60
+ width: number
61
+ frames: CapturedReading[]
62
+ problems: string[]
63
+ }
64
+
65
+ /** The tallest picture of a reading page's section, in CSS pixels. */
66
+ export const CAPTURE_MAX_HEIGHT = 8000
67
+
68
+ export interface CapturedBox {
69
+ x: number
70
+ y: number
71
+ width: number
72
+ height: number
73
+ visible: boolean
74
+ /** The start of the element's text. */
75
+ text: string
76
+ /** Its `id`, `class` and `data-*` attributes. */
77
+ attributes: Record<string, string>
78
+ }
79
+
80
+ export interface CapturedSlide {
81
+ section: string
82
+ slide: number
83
+ step: number
84
+ /** Milliseconds into the step, or null for its rest frame. */
85
+ at: number | null
86
+ /** The step's narration (its script), as text: what the captions show. */
87
+ narration: string
88
+ /** The PNG file of the picture, in the service's state directory. */
89
+ file: string
90
+ width: number
91
+ height: number
92
+ boxes?: CapturedBox[]
93
+ }
94
+
95
+ export interface SlideCaptureResult {
96
+ document: string
97
+ /** The slide's size in slide pixels. */
98
+ shape: { width: number, height: number }
99
+ frames: CapturedSlide[]
100
+ /** What failed in the page while it was drawn: component errors, page errors. */
101
+ problems: string[]
102
+ }
103
+
104
+ export interface OverviewCaptureRequest {
105
+ /** Each page's last step (default), or every step of every page. */
106
+ steps?: 'last' | 'all'
107
+ appearance?: 'light' | 'dark'
108
+ /** Thumbnails a row on a sheet (default 4). */
109
+ columns?: number
110
+ /** Draw the presentation's captions over the slides (default true). */
111
+ captions?: boolean
112
+ }
113
+
114
+ export interface OverviewCaptureResult {
115
+ document: string
116
+ /** The directory of the pictures, named so that they sort in reading order (`03-12-page.png`: page 3, step 12 from 0). */
117
+ dir: string
118
+ frames: { section: string, slide: number, step: number, file: string, narration: string }[]
119
+ /** `narration.md` beside the pictures: each picture's narration under its file name. */
120
+ narrationFile: string
121
+ /** Sheets of thumbnails, at most `CAPTURE_SHEET_SIZE` each, in order. */
122
+ sheets: string[]
123
+ problems: string[]
124
+ }
125
+
126
+ /** Thumbnails on one overview sheet; and the most pictures an overview takes. */
127
+ export const CAPTURE_SHEET_SIZE = 20
128
+ export const CAPTURE_MAX_OVERVIEW = 400
129
+
130
+ export interface DocumentCapture {
131
+ /** The pictures, with their PNG bytes in the order of `frames`. */
132
+ slides(document: string, request: SlideCaptureRequest, signal?: AbortSignal): Promise<SlideCaptureResult & { images: Uint8Array[] }>
133
+ /** A page of the reading version (the web page), with its PNG bytes. */
134
+ reading(document: string, request: ReadingCaptureRequest, signal?: AbortSignal): Promise<ReadingCaptureResult & { images: Uint8Array[] }>
135
+ /** Every page's rest frames as the presentation shows them, as files and as sheets of thumbnails (the images). */
136
+ overview(document: string, request: OverviewCaptureRequest, signal?: AbortSignal): Promise<OverviewCaptureResult & { images: Uint8Array[] }>
137
+ }
138
+
139
+ /** The hook a viewer page opened with `?capture` installs, which the capturing browser drives. */
140
+ export interface CaptureHook {
141
+ /** A slide at a step: drawn at the moment `at` (its rest frame when omitted), or reached going back from the step after. */
142
+ show(position: { section: string, step: number, at?: number, arrive?: 'direct' | 'backward' }): Promise<{ problems: string[] }>
143
+ /** A section of the reading page, scrolled through so that its components load, at rest. */
144
+ read(section: string): Promise<{ problems: string[] }>
145
+ }
146
+
147
+ export const CAPTURE_HOOK = 'presmiumCapture'
148
+ export const CAPTURE_PARAM = 'capture'
@@ -194,11 +194,24 @@ export interface Production {
194
194
  cueTimings?: Record<string, { pauseMs: number }>
195
195
  /** Stale mappings remain inspectable but never retime audio or replace changed narration. */
196
196
  captionTimings?: Record<StepId, CaptionTiming>
197
+ /** How large the captions are, in the presentation and burned into a lecture video; absent is the default size. */
198
+ captions?: CaptionStyle
197
199
  mediaClips: MediaClip[]
198
200
  }
201
+ /** `scale` multiplies the captions' default size, from `CAPTION_SCALE_MIN` to `CAPTION_SCALE_MAX` (Studio offers tenths). */
202
+ export interface CaptionStyle { scale: number }
203
+ export const CAPTION_SCALE_MIN = 0.7
204
+ export const CAPTION_SCALE_MAX = 1.6
199
205
 
200
206
  /** A stable rich-text/code ID, or `page:${sectionId}` reserved for an existing section title. */
201
207
  export interface DocumentTextPoint { blockId: string, offset: number }
208
+ export interface DocumentTextAnchor { from: DocumentTextPoint, to: DocumentTextPoint }
209
+ /** A stable block, optionally at one step that renders it; the step belongs to the block's section. */
210
+ export interface DocumentPlaceAnchor { blockId: BlockId, stepId?: StepId }
211
+ export type DocumentThreadAnchor = DocumentTextAnchor | DocumentPlaceAnchor
212
+ /** A text range and a place are disjoint shapes; read the shape instead of guessing from offsets. */
213
+ export const isDocumentTextAnchor = (anchor: DocumentThreadAnchor | undefined): anchor is DocumentTextAnchor => Boolean(anchor && 'from' in anchor && 'to' in anchor)
214
+ export const isDocumentPlaceAnchor = (anchor: DocumentThreadAnchor | undefined): anchor is DocumentPlaceAnchor => Boolean(anchor && 'blockId' in anchor && !('from' in anchor))
202
215
  export interface DocumentComment {
203
216
  id: string
204
217
  type: 'comment'
@@ -220,7 +233,7 @@ export interface DocumentThread {
220
233
  resolvedBy?: string
221
234
  metadata?: JsonObject
222
235
  deletedAt?: string
223
- anchor?: { from: DocumentTextPoint, to: DocumentTextPoint }
236
+ anchor?: DocumentThreadAnchor
224
237
  comments: DocumentComment[]
225
238
  }
226
239
 
@@ -1,4 +1,4 @@
1
- import type { CanonicalSource, DirectiveSpec, IRDocument, IRPass, Renderer, RendererContribution, RoleSpec } from '../ir.ts'
1
+ import type { CanonicalSource, IRDocument, IRPass, Renderer, RendererContribution } from '../ir.ts'
2
2
  import type { AgentTool } from './agent.ts'
3
3
  import type { AnchorResolver, AnchorString } from './anchors.ts'
4
4
  import type { Check, CheckContext } from './checks.ts'
@@ -159,7 +159,6 @@ export interface PresmiumNodePlugin {
159
159
  resolvers?: AnchorResolver[]
160
160
  checks?: Check[]
161
161
  anchors?(ctx: CheckContext): AnchorString[] | Promise<AnchorString[]>
162
- directives?: DirectiveSpec[]
163
162
  passes?: IRPass[]
164
163
  executors?: Executor[]
165
164
  renderer?: Renderer
@@ -177,7 +176,6 @@ export interface PresmiumNodePlugin {
177
176
  services?(ctx: NodeServiceContext): [ServiceToken<unknown>, unknown][]
178
177
  environments?: Record<string, EnvironmentProvider>
179
178
  server?(ctx: ServerContext): ServerContribution | Promise<ServerContribution>
180
- roles?: RoleSpec[]
181
179
  tools?: AgentTool[]
182
180
  operator?: boolean
183
181
  }
@@ -10,6 +10,9 @@ export const PREVIEW_PARAM = 'presmium-preview'
10
10
  /** The workbench's appearance when it loads a preview (`dark` or `light`), so that the page's first paint matches it. */
11
11
  export const PREVIEW_APPEARANCE_PARAM = 'presmium-appearance'
12
12
 
13
+ /** `?captions=off` starts a presentation with its captions off (a reader turns them on again with the captions button). */
14
+ export const CAPTIONS_PARAM = 'captions'
15
+
13
16
  export const PREVIEW_REVISION = 'presmiumRevision'
14
17
 
15
18
  export const AUTHOR_MARKS_CLASS = 'pm-author-marks'
@@ -201,8 +204,6 @@ export type PreviewReport =
201
204
  | { type: 'selection', selection: (PreviewAnchor & { text: string, rect?: PreviewRect, obstacles?: PreviewRect[] }) | null }
202
205
  /** Where the picked block or the selection is after the page scrolled or resized; null when it is out of sight. */
203
206
  | { type: 'rect', rect: PreviewRect | null, obstacles?: PreviewRect[] }
204
- /** Where the blocks the AI changed are in the preview's viewport while they blink, and again as the page scrolls (at most 16). */
205
- | { type: 'flashed', rects: PreviewRect[] }
206
207
  | { type: 'editing', target: PreviewAnchor, active: boolean }
207
208
  /** The text of a block edited in place, before and after (its `textContent`); sent only when it changed. */
208
209
  | { type: 'edited', target: PreviewAnchor, before: string, after: string, request?: number, revision?: string, binding?: string }
@@ -371,7 +372,6 @@ export function toPreviewReport(data: unknown, nonce: string): PreviewReport | u
371
372
  ? { type: 'selection', selection: { ...toAnchor(selected), text: selected.text, ...selected.rect ? { rect: toRect(selected.rect as PreviewRect) } : {}, ...selected.obstacles !== undefined ? { obstacles: (selected.obstacles as PreviewRect[]).map(toRect) } : {} } }
372
373
  : undefined
373
374
  }
374
- case 'flashed': return Array.isArray(m.rects) && m.rects.length <= 16 && m.rects.every(rect) ? { type: 'flashed', rects: (m.rects as PreviewRect[]).map(toRect) } : undefined
375
375
  case 'rect': return (m.rect === null || rect(m.rect)) && optional(m.obstacles, obstacles) ? { type: 'rect', rect: m.rect === null ? null : toRect(m.rect), ...m.obstacles !== undefined ? { obstacles: (m.obstacles as PreviewRect[]).map(toRect) } : {} } : undefined
376
376
  case 'editing': return anchor(m.target) && typeof m.active === 'boolean' ? { type: 'editing', target: toAnchor(m.target), active: m.active } : undefined
377
377
  case 'edited': return anchor(m.target) && text(m.before) && text(m.after) && m.before.length <= MAX_EDITED && m.after.length <= MAX_EDITED && editedRequest(m) ? { type: 'edited', target: toAnchor(m.target), before: m.before, after: m.after, ...m.request === undefined ? {} : { request: m.request as number, revision: m.revision as string, binding: m.binding as string } } : undefined
@@ -103,6 +103,15 @@ export interface VoiceCloneStatus {
103
103
  message?: string
104
104
  }
105
105
 
106
+ export interface SpeechCachePrune {
107
+ /** Entries some request uses. */
108
+ kept: number
109
+ /** Entries no request uses, with the bytes of their files. */
110
+ unused: { key: string, bytes: number }[]
111
+ /** Whether the unused entries were deleted. */
112
+ removed: boolean
113
+ }
114
+
106
115
  export interface TranscriptionRequest {
107
116
  engine?: string
108
117
  audio: Blob
@@ -124,6 +133,13 @@ export interface SpeechService {
124
133
  cached?(key: string, signal?: AbortSignal): Promise<Synthesis | undefined>
125
134
  alignWords?(key: string, options: { lang: string, transcribe?: boolean }, signal?: AbortSignal): Promise<SpeechWord[] | undefined>
126
135
  pruneLock?(keep: ReadonlySet<string>): Promise<number>
136
+ /**
137
+ * The document's cached audio that no request of `keep` (every sentence the document says) uses: kept is any entry
138
+ * made for the same request under any configuration of the voice service. With `remove`, deletes the unused entries
139
+ * with their alignments and speech lock entries. Throws `speech-prune-engine-unknown` when a request names no engine
140
+ * and no default engine is configured, since its entries cannot be told apart then.
141
+ */
142
+ pruneCache?(keep: readonly SynthesisRequest[], options?: { remove?: boolean, signal?: AbortSignal }): Promise<SpeechCachePrune>
127
143
  transcribe(request: TranscriptionRequest, signal?: AbortSignal): Promise<Transcript>
128
144
  /** Registers the consent, then starts training; only for the document's owner. Absent: the service cannot clone. */
129
145
  cloneVoice?(request: VoiceCloneRequest, signal?: AbortSignal): Promise<VoiceCloneStatus>
@@ -1,9 +1,31 @@
1
1
  import type { CanonicalSource } from '../ir.ts'
2
2
  import type { Member } from './server.ts'
3
- import type { VoiceoverCancellation, VoiceoverJob, VoiceoverRequest, VoiceoverSnapshot, VoicePreviewAnswer, VoicePreviewQuery, VoicePreviewQuote, VoicePreviewRequest } from './voice.ts'
3
+ import type { SpeechVoice, SynthesisRequest } from './speech.ts'
4
+ import type { VoiceoverCancellation, VoiceoverJob, VoiceoverRequest, VoiceoverSnapshot, VoicePreviewAnswer, VoicePreviewQuery, VoicePreviewQuote, VoicePreviewRequest, VoiceTarget } from './voice.ts'
4
5
  import { defineService } from './service.ts'
5
6
 
6
7
  export interface VoiceoverStatusQuery { source?: CanonicalSource, revision?: string }
8
+ export interface VoiceoverSentencesQuery extends VoiceoverStatusQuery {
9
+ /** Answer as if this were the document's default voice: sentences that inherit the default, or have no voice, use it. */
10
+ defaultVoice?: SpeechVoice
11
+ }
12
+ /** One sentence that a document-scope generation processes, in reading order. */
13
+ export interface VoiceoverSentence {
14
+ section: string
15
+ step: number
16
+ stepId?: string
17
+ /** The step binding for a `steps` scope; absent when the sentence cannot be generated by step (it crosses steps). */
18
+ target?: VoiceTarget
19
+ /** The spoken text, which the voice service receives (and a cloud provider charges for). */
20
+ text: string
21
+ /** The exact synthesis request; absent when the sentence has no usable voice. */
22
+ request?: SynthesisRequest
23
+ /** Exact audio for the request is in the speech cache. */
24
+ cached: boolean
25
+ /** Why it cannot be generated now: no voice, or the voice service or voice is unavailable. */
26
+ reason?: string
27
+ }
28
+ export interface VoiceoverSentences { source: CanonicalSource, revision: string, sentences: VoiceoverSentence[] }
7
29
  export type VoiceoverStart = Omit<VoiceoverRequest, 'confirmed'>
8
30
  export type VoicePreviewStart = Omit<VoicePreviewRequest, 'confirmed'>
9
31
  export type VoicePreviewCheck = VoicePreviewQuery & { source?: CanonicalSource }
@@ -23,6 +45,8 @@ export interface VoicePreviewMedia extends Omit<VoicePreviewAnswer, 'audio'> { b
23
45
  /** Stateless plugin execution. The host owns intents and jobs; discovery creates neither. */
24
46
  export interface VoiceoverExecution {
25
47
  status(query: VoiceoverStatusQuery, signal: AbortSignal): Promise<Omit<VoiceoverSnapshot, 'jobs'>>
48
+ /** Every sentence with its request and cache state; reads the cache and the voice catalog, never generates. */
49
+ sentences?(query: VoiceoverSentencesQuery, signal: AbortSignal): Promise<VoiceoverSentences>
26
50
  prepare(request: VoiceoverStart, signal: AbortSignal): Promise<PreparedVoiceover>
27
51
  quote(query: VoicePreviewCheck, signal: AbortSignal): Promise<VoicePreviewQuote>
28
52
  preview(request: VoicePreviewStart, signal: AbortSignal): Promise<VoicePreviewMedia>
@@ -94,6 +94,8 @@ export interface DocumentSummary {
94
94
  description?: string
95
95
  lang?: string
96
96
  tags?: string[]
97
+ /** Whether its metadata asks for a presentation (`renderers.slides`), which a site serves below its pages. */
98
+ slides?: boolean
97
99
  }
98
100
 
99
101
  /** @experimental The workspace service's answer to `documents`. */
package/src/contracts.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './contracts/agent.ts'
2
2
  export * from './contracts/authoring.ts'
3
+ export * from './contracts/capture.ts'
3
4
  export * from './contracts/document-reads.ts'
4
5
  export * from './contracts/anchors.ts'
5
6
  export * from './contracts/checks.ts'
@@ -1,6 +1,8 @@
1
1
  import { applyDocumentCaptionTiming } from './document-caption-timings.ts'
2
+ import { captionScale, changeDocumentCaptionScale } from './document-caption-scale.ts'
2
3
  import { hasRole } from './access.ts'
3
- import { applyDocumentCommentAction, DocumentCommentError, validateDocumentCommentAnchor } from './document-comments.ts'
4
+ import { applyDocumentCommentAction, degradeDocumentCommentAnchor, DocumentCommentError, validateDocumentCommentAnchor } from './document-comments.ts'
5
+ import { documentAnchorKey } from './document-places.ts'
4
6
  import { changeDocumentVoice, documentVoiceStepIds, previewDocumentVoiceChange } from './document-voices.ts'
5
7
  import { applyDocumentComponentAction } from './document-components.ts'
6
8
  import type { DocumentAuthoringAction, DocumentAuthoringActionEffect } from './contracts/authoring-actions.ts'
@@ -10,6 +12,10 @@ import type { TeachingDocument } from './contracts/document.ts'
10
12
  /** Fixed context is supplied by the owning adapter exactly once per semantic plan. */
11
13
  export function applyDocumentAuthoringAction(document: TeachingDocument, action: DocumentAuthoringAction, context: DocumentCommentContext): { content: TeachingDocument, effect: DocumentAuthoringActionEffect } {
12
14
  if (!context.member || !hasRole(context.member, 'editor')) throw new DocumentCommentError(403, 'authoring-permission-denied')
15
+ if (action.type === 'caption.scale.set') {
16
+ const content = changeDocumentCaptionScale(document, action.scale)
17
+ return { content, effect: { kind: 'caption-scale', value: { scale: captionScale(content.production), changed: content !== document } } }
18
+ }
13
19
  if (action.type === 'component.parameters.patch' || action.type === 'component.step-parameters.patch' || action.type === 'component.step-parameters.reset' || action.type === 'component.definition.use') {
14
20
  const { content, effect } = applyDocumentComponentAction(document, action)
15
21
  return { content, effect: { kind: 'component', value: effect } }
@@ -29,17 +35,20 @@ export function applyDocumentAuthoringAction(document: TeachingDocument, action:
29
35
  return { content, effect: { kind: 'voice', scope: action.scope.kind, value: previewDocumentVoiceChange(document, action.scope, action.source, { replaceOverrides: action.replaceOverrides }), recordings } }
30
36
  }
31
37
  if (action.type === 'comment.create') validateDocumentCommentAnchor(document, action.anchor ?? undefined)
32
- let source = document.comments ?? {}, detached = false
38
+ let source = document.comments ?? {}, detached = false, degraded = false
33
39
  if (action.type !== 'comment.create' && Object.hasOwn(source, action.threadId)) {
34
40
  const thread = source[action.threadId]!
35
- try { validateDocumentCommentAnchor(document, thread.anchor) } catch (error) {
36
- if (!(error instanceof DocumentCommentError) || error.status !== 422) throw error
41
+ const anchor = degradeDocumentCommentAnchor(document, thread.anchor)
42
+ if (thread.anchor && !anchor) {
37
43
  const { anchor: _anchor, ...unanchored } = thread
38
44
  source = { ...source, [thread.id]: unanchored }; detached = true
45
+ } else if (anchor && documentAnchorKey(anchor) !== documentAnchorKey(thread.anchor)) {
46
+ source = { ...source, [thread.id]: { ...thread, anchor } }; degraded = true
39
47
  }
40
48
  }
41
49
  const { threads, effect } = applyDocumentCommentAction(source, action, context)
42
50
  if (detached) { effect.anchorDetached = true; effect.changed = true }
51
+ else if (degraded) effect.changed = true
43
52
  const content = { ...document }
44
53
  if (Object.keys(threads).length) content.comments = threads
45
54
  else delete content.comments
@@ -0,0 +1,17 @@
1
+ import { CAPTION_SCALE_MAX, CAPTION_SCALE_MIN } from './contracts/document.ts'
2
+ import type { Production, TeachingDocument } from './contracts/document.ts'
3
+
4
+ /** The caption size a document sets (`production.captions.scale`); 1 when it sets none or one out of range. */
5
+ export function captionScale(production: Pick<Production, 'captions'> | undefined): number {
6
+ const scale = production?.captions?.scale
7
+ return typeof scale === 'number' && scale >= CAPTION_SCALE_MIN && scale <= CAPTION_SCALE_MAX ? scale : 1
8
+ }
9
+
10
+ /** Sets the caption size, kept in range and rounded to a tenth; the default size 1 removes the setting. */
11
+ export function changeDocumentCaptionScale(document: TeachingDocument, scale: number): TeachingDocument {
12
+ if (!Number.isFinite(scale)) throw new Error('document-caption-scale')
13
+ const value = Math.round(Math.min(CAPTION_SCALE_MAX, Math.max(CAPTION_SCALE_MIN, scale)) * 10) / 10
14
+ if (value === captionScale(document.production)) return document
15
+ const { captions: _captions, ...production } = document.production
16
+ return { ...document, production: value === 1 ? production : { ...production, captions: { scale: value } } }
17
+ }
@@ -1,6 +1,10 @@
1
1
  import { hasRole } from './access.ts'
2
2
  import type { DocumentCommentAction, DocumentCommentContext, DocumentCommentEffect } from './contracts/document-comments.ts'
3
- import type { DocumentComment, DocumentThread, InlineNode, JsonObject, ListItemNode, RichTextNode, TableCellNode, TableRowNode, TeachingDocument } from './contracts/document.ts'
3
+ import { isDocumentPlaceAnchor, isDocumentTextAnchor } from './contracts/document.ts'
4
+ import { documentBlockSection, documentCommentTargets } from './document-places.ts'
5
+ import type { DocumentComment, DocumentThread, DocumentThreadAnchor, JsonObject, ListItemNode, RichTextNode, TableCellNode, TableRowNode, TeachingDocument } from './contracts/document.ts'
6
+
7
+ export { documentCommentTargets, isDocumentTextLink } from './document-places.ts'
4
8
 
5
9
  export class DocumentCommentError extends Error {
6
10
  readonly status: number
@@ -8,45 +12,19 @@ export class DocumentCommentError extends Error {
8
12
  }
9
13
  function fail(status: number, code: string): never { throw new DocumentCommentError(status, code) }
10
14
 
11
- /** Canonical anchor v1 expands only nonempty styled-text links; other inline values are atoms. */
12
- export function isDocumentTextLink(node: InlineNode): node is Extract<InlineNode, { type: 'link' }> & { children: Extract<InlineNode, { type: 'text' | 'break' }>[] } {
13
- return node.type === 'link' && node.children.length > 0 && node.children.every(child => child.type === 'text' || child.type === 'break')
14
- }
15
- function inlineText(nodes: readonly InlineNode[]): string {
16
- return nodes.map(node => node.type === 'text' ? node.text : node.type === 'break' ? '\n' : isDocumentTextLink(node) ? inlineText(node.children) : '\uFFFC').join('')
17
- }
18
-
19
- /** Only addressable canonical text identities, in native document order; opaque data is never traversed. */
20
- export function documentCommentTargets(document: TeachingDocument): Map<string, { order: number, text: string }> {
21
- const targets = new Map<string, { order: number, text: string }>()
22
- const add = (id: string, text: string) => { targets.set(id, { order: targets.size, text }) }
23
- const rich = (node: RichTextNode): void => {
24
- if (node.type === 'paragraph' || node.type === 'heading') add(node.id, inlineText(node.children))
25
- else if (node.type === 'code') add(node.id, node.value)
26
- else if (node.type === 'list') for (const item of node.children) {
27
- const [first, ...rest] = item.children
28
- add(item.id, first?.type === 'paragraph' ? inlineText(first.children) : '')
29
- ;(first?.type === 'paragraph' ? rest : item.children).forEach(rich)
30
- }
31
- else if (node.type === 'blockquote') {
32
- const [first, ...rest] = node.children
33
- if (first?.type === 'paragraph') { add(node.id, inlineText(first.children)); rest.forEach(rich) }
34
- else node.children.forEach(rich)
35
- } else if (node.type === 'table') for (const row of node.children) for (const cell of row.children) add(cell.id, inlineText(cell.children))
36
- }
37
- const block = (id: string): void => {
38
- const value = document.blocks[id]
39
- if (value?.type === 'text') value.content.forEach(rich)
40
- else if (value?.type === 'code' && value.value !== undefined) add(id, value.value)
41
- else if (value?.type === 'container' || value?.type === 'component') value.children?.forEach(block)
42
- }
43
- for (const id of document.sectionOrder) { const page = document.sections[id]!; add(`page:${id}`, page.title); page.children.forEach(block) }
44
- return targets
45
- }
46
-
47
15
  /** No ordinal, offset clamping, surrogate splitting or guessed fallback identity. */
48
16
  export function validateDocumentCommentAnchor(document: TeachingDocument, anchor: DocumentThread['anchor']): void {
49
17
  if (!anchor) return
18
+ if (isDocumentPlaceAnchor(anchor)) {
19
+ if (!Object.hasOwn(document.blocks, anchor.blockId)) fail(422, 'comment-anchor-target-unavailable')
20
+ if (anchor.stepId !== undefined) {
21
+ const step = Object.hasOwn(document.steps, anchor.stepId) ? document.steps[anchor.stepId] : undefined
22
+ const section = step ? document.sections[step.sectionId] : undefined
23
+ if (!step || !section || !section.stepOrder.includes(anchor.stepId) || step.sectionId !== documentBlockSection(document, anchor.blockId)) fail(422, 'comment-anchor-target-unavailable')
24
+ }
25
+ return
26
+ }
27
+ if (!isDocumentTextAnchor(anchor)) fail(422, 'comment-anchor-target-unavailable')
50
28
  const targets = documentCommentTargets(document), a = targets.get(anchor.from.blockId), b = targets.get(anchor.to.blockId)
51
29
  if (!a || !b) fail(422, 'comment-anchor-target-unavailable')
52
30
  for (const [point, target] of [[anchor.from, a], [anchor.to, b]] as const) {
@@ -57,6 +35,21 @@ export function validateDocumentCommentAnchor(document: TeachingDocument, anchor
57
35
  if (a.order > b.order || a.order === b.order && anchor.from.offset >= anchor.to.offset) fail(422, 'comment-anchor-range-invalid')
58
36
  }
59
37
 
38
+ /**
39
+ * The strongest still-valid anchor. A place whose step no longer exists keeps the block; a missing block or an
40
+ * unmappable text range leaves the discussion explicitly unanchored rather than silently pointing elsewhere.
41
+ */
42
+ export function degradeDocumentCommentAnchor(document: TeachingDocument, anchor: DocumentThreadAnchor | undefined): DocumentThreadAnchor | undefined {
43
+ if (!anchor) return undefined
44
+ try { validateDocumentCommentAnchor(document, anchor); return anchor }
45
+ catch (error) { if (!(error instanceof DocumentCommentError) || error.status !== 422) throw error }
46
+ if (isDocumentPlaceAnchor(anchor) && anchor.stepId !== undefined && Object.hasOwn(document.blocks, anchor.blockId)) {
47
+ const place: DocumentThreadAnchor = { blockId: anchor.blockId }
48
+ try { validateDocumentCommentAnchor(document, place); return place } catch { /* The block itself is not addressable. */ }
49
+ }
50
+ return undefined
51
+ }
52
+
60
53
  /** Shared native/editor permissions: editors may delete any discussion, but edit their own comment; a document owner may manage earlier authors. */
61
54
  export function applyDocumentCommentAction(source: NonNullable<TeachingDocument['comments']>, action: DocumentCommentAction, context: DocumentCommentContext): { threads: NonNullable<TeachingDocument['comments']>, effect: DocumentCommentEffect } {
62
55
  const member = context.member
@@ -1,6 +1,7 @@
1
1
  import jsonPatch from 'fast-json-patch'
2
2
  import type { Operation } from 'fast-json-patch'
3
3
  import { validateDocument } from './document.ts'
4
+ import { documentBlockSection } from './document-places.ts'
4
5
  import { documentParameterAccess } from './parameters.ts'
5
6
  import { DOCUMENT_PARAMETER_MAX_CHANGES, DOCUMENT_PARAMETER_MAX_PATH_DEPTH } from './contracts/authoring-actions.ts'
6
7
  import type { DocumentComponentAction, DocumentComponentEffect, DocumentParameterChange } from './contracts/authoring-actions.ts'
@@ -26,18 +27,9 @@ function component(document: TeachingDocument, id: string): ComponentBlock {
26
27
  }
27
28
  export function documentComponentSection(document: TeachingDocument, blockId: string): string {
28
29
  component(document, blockId)
29
- for (const sectionId of document.sectionOrder) {
30
- const pending = [...document.sections[sectionId]!.children], seen = new Set<string>()
31
- while (pending.length) {
32
- const id = pending.pop()!
33
- if (id === blockId) return sectionId
34
- if (seen.has(id)) continue
35
- seen.add(id)
36
- const block = document.blocks[id]
37
- if (block?.type === 'container' || block?.type === 'component') pending.push(...block.children ?? [])
38
- }
39
- }
40
- return fail(422, 'component-block-owner-missing', { blockId })
30
+ const section = documentBlockSection(document, blockId)
31
+ if (!section) return fail(422, 'component-block-owner-missing', { blockId })
32
+ return section
41
33
  }
42
34
  function stepOf(document: TeachingDocument, stepId: string, sectionId: string) {
43
35
  const step = own(document.steps, stepId) ? document.steps[stepId] : undefined
@@ -0,0 +1,57 @@
1
+ import { Validator } from '@cfworker/json-schema'
2
+ import { DOCUMENT_JSON_SCHEMA } from './document-schema.ts'
3
+ import type { Narration, TeachingDocument } from './contracts/document.ts'
4
+
5
+ const schema = new Validator({ $id: 'urn:presmium:step-narrations:1', $defs: structuredClone(DOCUMENT_JSON_SCHEMA.$defs), type: 'array', items: { $ref: '#/$defs/Narration' } }, '2020-12', false)
6
+
7
+ /** Replace one step's ordered script blocks, retaining entities still used by other steps. */
8
+ export function replaceStepNarrations(source: TeachingDocument, stepId: string, narrations: readonly Narration[]): TeachingDocument {
9
+ const step = source.steps[stepId]
10
+ if (!step || !source.sections[step.sectionId]?.stepOrder.includes(stepId)) throw new Error('document-narration-target')
11
+ if (!schema.validate(narrations).valid) throw new Error('document-narration-content')
12
+ const ids = narrations.map(narration => narration.id)
13
+ if (new Set(ids).size !== ids.length) throw new Error('document-narration-target')
14
+ const content = { ...source,
15
+ steps: { ...source.steps, [stepId]: { ...step, narrationIds: ids } },
16
+ narrations: { ...source.narrations },
17
+ production: { ...source.production,
18
+ ...source.production.narrationOverrides ? { narrationOverrides: { ...source.production.narrationOverrides } } : {},
19
+ ...source.production.cueTimings ? { cueTimings: { ...source.production.cueTimings } } : {},
20
+ },
21
+ }
22
+ for (const narration of narrations) {
23
+ content.narrations[narration.id] = structuredClone(narration)
24
+ }
25
+ const used = new Set(Object.values(content.steps).flatMap(step => step.narrationIds))
26
+ for (const id of step.narrationIds) if (!used.has(id)) {
27
+ delete content.narrations[id]
28
+ if (content.production.narrationOverrides) delete content.production.narrationOverrides[id]
29
+ }
30
+ const cues = new Set(Object.values(content.narrations).flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
31
+ for (const id of Object.keys(content.production.cueTimings ?? {})) if (!cues.has(id)) delete content.production.cueTimings![id]
32
+ return content
33
+ }
34
+
35
+ /** An exact, unique text edit keeps the existing pronunciation and cue atoms intact. */
36
+ export function replaceStepNarrationText(source: TeachingDocument, stepId: string, find: string, replacement: string): TeachingDocument {
37
+ const step = source.steps[stepId]
38
+ if (!step || !find) throw new Error('document-narration-target')
39
+ const narrations = step.narrationIds.map(id => source.narrations[id]!)
40
+ const matches: { narration: string, node: number, offset: number }[] = []
41
+ for (const narration of narrations) for (const [node, value] of narration.content.entries()) {
42
+ if (value.type !== 'text') continue
43
+ for (let at = value.text.indexOf(find); at !== -1; at = value.text.indexOf(find, at + 1)) {
44
+ matches.push({ narration: narration.id, node, offset: at })
45
+ if (matches.length > 1) throw new Error('document-narration-text-ambiguous')
46
+ }
47
+ }
48
+ const found = matches[0]
49
+ if (!found) throw new Error('document-narration-text-missing')
50
+ return replaceStepNarrations(source, stepId, narrations.map(narration => narration.id !== found.narration ? narration : {
51
+ ...narration, content: narration.content.flatMap((node, index) => {
52
+ if (index !== found.node || node.type !== 'text') return [node]
53
+ const text = node.text.slice(0, found.offset) + replacement + node.text.slice(found.offset + find.length)
54
+ return text ? [{ ...node, text }] : []
55
+ }),
56
+ }).filter(narration => narration.content.length > 0))
57
+ }