@vocasync/astro 0.1.0

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 (114) hide show
  1. package/README.md +580 -0
  2. package/dist/api/client.d.ts +77 -0
  3. package/dist/api/client.d.ts.map +1 -0
  4. package/dist/api/client.js +223 -0
  5. package/dist/api/client.js.map +1 -0
  6. package/dist/api/index.d.ts +2 -0
  7. package/dist/api/index.d.ts.map +1 -0
  8. package/dist/api/index.js +2 -0
  9. package/dist/api/index.js.map +1 -0
  10. package/dist/cli.d.ts +3 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +257 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/components/AudioPlayer.astro +1097 -0
  15. package/dist/components/index.d.ts +19 -0
  16. package/dist/components/index.d.ts.map +1 -0
  17. package/dist/components/index.js +4 -0
  18. package/dist/components/index.js.map +1 -0
  19. package/dist/config/index.d.ts +3 -0
  20. package/dist/config/index.d.ts.map +1 -0
  21. package/dist/config/index.js +2 -0
  22. package/dist/config/index.js.map +1 -0
  23. package/dist/config/schema.d.ts +275 -0
  24. package/dist/config/schema.d.ts.map +1 -0
  25. package/dist/config/schema.js +124 -0
  26. package/dist/config/schema.js.map +1 -0
  27. package/dist/core/audio-map.d.ts +52 -0
  28. package/dist/core/audio-map.d.ts.map +1 -0
  29. package/dist/core/audio-map.js +97 -0
  30. package/dist/core/audio-map.js.map +1 -0
  31. package/dist/core/content-loader.d.ts +17 -0
  32. package/dist/core/content-loader.d.ts.map +1 -0
  33. package/dist/core/content-loader.js +141 -0
  34. package/dist/core/content-loader.js.map +1 -0
  35. package/dist/core/hash-manager.d.ts +16 -0
  36. package/dist/core/hash-manager.d.ts.map +1 -0
  37. package/dist/core/hash-manager.js +21 -0
  38. package/dist/core/hash-manager.js.map +1 -0
  39. package/dist/core/index.d.ts +5 -0
  40. package/dist/core/index.d.ts.map +1 -0
  41. package/dist/core/index.js +5 -0
  42. package/dist/core/index.js.map +1 -0
  43. package/dist/core/speech-builder.d.ts +19 -0
  44. package/dist/core/speech-builder.d.ts.map +1 -0
  45. package/dist/core/speech-builder.js +252 -0
  46. package/dist/core/speech-builder.js.map +1 -0
  47. package/dist/index.d.ts +34 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +35 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/integration.d.ts +27 -0
  52. package/dist/integration.d.ts.map +1 -0
  53. package/dist/integration.js +110 -0
  54. package/dist/integration.js.map +1 -0
  55. package/dist/lib/latex-to-speech/index.d.ts +26 -0
  56. package/dist/lib/latex-to-speech/index.d.ts.map +1 -0
  57. package/dist/lib/latex-to-speech/index.js +101 -0
  58. package/dist/lib/latex-to-speech/index.js.map +1 -0
  59. package/dist/rehype/audio-words.d.ts +50 -0
  60. package/dist/rehype/audio-words.d.ts.map +1 -0
  61. package/dist/rehype/audio-words.js +269 -0
  62. package/dist/rehype/audio-words.js.map +1 -0
  63. package/dist/rehype/index.d.ts +6 -0
  64. package/dist/rehype/index.d.ts.map +1 -0
  65. package/dist/rehype/index.js +4 -0
  66. package/dist/rehype/index.js.map +1 -0
  67. package/dist/rehype/rehype-math-speech.d.ts +18 -0
  68. package/dist/rehype/rehype-math-speech.d.ts.map +1 -0
  69. package/dist/rehype/rehype-math-speech.js +117 -0
  70. package/dist/rehype/rehype-math-speech.js.map +1 -0
  71. package/dist/rehype/remark-math-speech.d.ts +13 -0
  72. package/dist/rehype/remark-math-speech.d.ts.map +1 -0
  73. package/dist/rehype/remark-math-speech.js +26 -0
  74. package/dist/rehype/remark-math-speech.js.map +1 -0
  75. package/dist/styles/variables.css +181 -0
  76. package/dist/sync/index.d.ts +3 -0
  77. package/dist/sync/index.d.ts.map +1 -0
  78. package/dist/sync/index.js +2 -0
  79. package/dist/sync/index.js.map +1 -0
  80. package/dist/sync/orchestrator.d.ts +36 -0
  81. package/dist/sync/orchestrator.d.ts.map +1 -0
  82. package/dist/sync/orchestrator.js +166 -0
  83. package/dist/sync/orchestrator.js.map +1 -0
  84. package/dist/types/index.d.ts +196 -0
  85. package/dist/types/index.d.ts.map +1 -0
  86. package/dist/types/index.js +2 -0
  87. package/dist/types/index.js.map +1 -0
  88. package/package.json +89 -0
  89. package/src/api/client.ts +316 -0
  90. package/src/api/index.ts +1 -0
  91. package/src/cli.ts +301 -0
  92. package/src/components/AudioPlayer.astro +1097 -0
  93. package/src/components/index.ts +24 -0
  94. package/src/config/index.ts +30 -0
  95. package/src/config/schema.ts +177 -0
  96. package/src/core/audio-map.ts +107 -0
  97. package/src/core/content-loader.ts +171 -0
  98. package/src/core/hash-manager.ts +22 -0
  99. package/src/core/index.ts +12 -0
  100. package/src/core/speech-builder.ts +307 -0
  101. package/src/data/audio-map.example.json +19 -0
  102. package/src/env.d.ts +13 -0
  103. package/src/index.ts +67 -0
  104. package/src/integration.ts +123 -0
  105. package/src/lib/latex-to-speech/index.ts +126 -0
  106. package/src/rehype/audio-words.ts +345 -0
  107. package/src/rehype/index.ts +6 -0
  108. package/src/rehype/rehype-math-speech.ts +145 -0
  109. package/src/rehype/remark-math-speech.ts +31 -0
  110. package/src/styles/variables.css +181 -0
  111. package/src/sync/index.ts +2 -0
  112. package/src/sync/orchestrator.ts +239 -0
  113. package/src/types/index.ts +211 -0
  114. package/src/types/modules.d.ts +11 -0
@@ -0,0 +1,2 @@
1
+ export { sync, checkConfig } from "./orchestrator.js";
2
+ export type { SyncOptions } from "./orchestrator.js";
@@ -0,0 +1,239 @@
1
+ import type { VocaSyncConfig } from "../config/index.js";
2
+ import type {
3
+ ContentItem,
4
+ AudioArtifact,
5
+ AudioMap,
6
+ SyncResult,
7
+ SyncSummary,
8
+ SyncStatus,
9
+ } from "../types/index.js";
10
+ import { loadContent } from "../core/content-loader.js";
11
+ import { buildSpeechDocument } from "../core/speech-builder.js";
12
+ import { loadAudioMap, saveAudioMap, getAudioEntry, setAudioEntry } from "../core/audio-map.js";
13
+ import { hasChanged } from "../core/hash-manager.js";
14
+ import { VocaSyncClient, getStreamingUrls } from "../api/client.js";
15
+
16
+ /**
17
+ * Options for the sync operation.
18
+ */
19
+ export interface SyncOptions {
20
+ /** Only sync this specific slug */
21
+ only?: string;
22
+ /** Force reprocessing even if content unchanged */
23
+ force?: boolean;
24
+ /** Dry run - don't make API calls */
25
+ dryRun?: boolean;
26
+ /** Progress callback */
27
+ onProgress?: (message: string, type?: "info" | "success" | "warn" | "error") => void;
28
+ }
29
+
30
+ /**
31
+ * Orchestrate the full sync process.
32
+ *
33
+ * 1. Load content from collection
34
+ * 2. Build speech documents
35
+ * 3. Compare hashes to detect changes
36
+ * 4. Submit synthesis jobs (with align=true)
37
+ * 5. Poll until complete
38
+ * 6. Update audio-map.json
39
+ */
40
+ export async function sync(
41
+ config: VocaSyncConfig,
42
+ client: VocaSyncClient,
43
+ options: SyncOptions = {}
44
+ ): Promise<SyncSummary> {
45
+ const { only, force = false, dryRun = false, onProgress = () => {} } = options;
46
+
47
+ const results: SyncResult[] = [];
48
+ let unchanged = 0;
49
+ let synced = 0;
50
+ let errors = 0;
51
+
52
+ // Load existing audio map
53
+ onProgress("Loading audio map...", "info");
54
+ const audioMap = await loadAudioMap(config.output.audioMapPath);
55
+
56
+ // Load content from collection
57
+ onProgress(`Loading content from ${config.collection.name}...`, "info");
58
+ let items = await loadContent(config.collection, config.frontmatterField);
59
+
60
+ // Filter by slug if specified
61
+ if (only) {
62
+ items = items.filter((item) => item.slug === only);
63
+ if (items.length === 0) {
64
+ throw new Error(`No content found with slug: ${only}`);
65
+ }
66
+ }
67
+
68
+ onProgress(`Found ${items.length} content items`, "info");
69
+
70
+ // Process each item
71
+ for (const item of items) {
72
+ const result = await processItem(
73
+ item,
74
+ config,
75
+ client,
76
+ audioMap,
77
+ { force, dryRun, onProgress }
78
+ );
79
+
80
+ results.push(result);
81
+
82
+ if (result.status === "unchanged") {
83
+ unchanged++;
84
+ } else if (result.status === "error") {
85
+ errors++;
86
+ } else {
87
+ synced++;
88
+ }
89
+ }
90
+
91
+ // Save updated audio map
92
+ if (!dryRun && synced > 0) {
93
+ onProgress("Saving audio map...", "info");
94
+ await saveAudioMap(config.output.audioMapPath, audioMap);
95
+ }
96
+
97
+ return {
98
+ total: items.length,
99
+ unchanged,
100
+ synced,
101
+ errors,
102
+ results,
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Process a single content item.
108
+ */
109
+ async function processItem(
110
+ item: ContentItem,
111
+ config: VocaSyncConfig,
112
+ client: VocaSyncClient,
113
+ audioMap: AudioMap,
114
+ options: {
115
+ force: boolean;
116
+ dryRun: boolean;
117
+ onProgress: (message: string, type?: "info" | "success" | "warn" | "error") => void;
118
+ }
119
+ ): Promise<SyncResult> {
120
+ const { force, dryRun, onProgress } = options;
121
+
122
+ try {
123
+ // Build speech document
124
+ const speechDoc = await buildSpeechDocument(item, { math: config.math });
125
+
126
+ // Check if content has changed
127
+ const existingEntry = getAudioEntry(audioMap, item.slug);
128
+ const contentChanged = hasChanged(existingEntry?.contentHash, speechDoc.hash);
129
+
130
+ if (!force && !contentChanged && existingEntry) {
131
+ onProgress(`⏭ ${item.slug} - unchanged`, "info");
132
+ return { slug: item.slug, status: "unchanged" };
133
+ }
134
+
135
+ const status: SyncStatus = existingEntry ? "updated" : "new";
136
+ onProgress(`📤 ${item.slug} - ${status}, submitting...`, "info");
137
+
138
+ if (dryRun) {
139
+ onProgress(`🔍 ${item.slug} - would ${status} (dry run)`, "info");
140
+ return { slug: item.slug, status };
141
+ }
142
+
143
+ // Submit synthesis with align=true
144
+ const response = await client.synthesize({
145
+ name: `astro-${item.slug}`,
146
+ text: speechDoc.text,
147
+ voice: config.synthesis.voice,
148
+ quality: config.synthesis.quality,
149
+ language: config.language,
150
+ });
151
+
152
+ onProgress(`⏳ ${item.slug} - processing (${response.projectUuid})...`, "info");
153
+
154
+ // Poll until complete
155
+ const finalStatus = await client.pollUntilComplete(response.projectUuid, {
156
+ onProgress: (status) => {
157
+ const synthStatus = status.synthesisJob?.status || "pending";
158
+ const alignStatus = status.alignmentJob?.status || "pending";
159
+ onProgress(` synth: ${synthStatus}, align: ${alignStatus}`, "info");
160
+ },
161
+ });
162
+
163
+ // Fetch alignment data
164
+ if (!finalStatus.alignmentJob?.alignmentUrl) {
165
+ throw new Error("No alignment URL returned");
166
+ }
167
+
168
+ const alignmentData = await client.getAlignment(finalStatus.alignmentJob.alignmentUrl);
169
+
170
+ // Get stable streaming URLs
171
+ const streamingUrls = getStreamingUrls(response.projectUuid);
172
+
173
+ // Create audio artifact (alignment data fetched at build time from alignmentUrl)
174
+ const artifact: AudioArtifact = {
175
+ projectUuid: response.projectUuid,
176
+ contentHash: speechDoc.hash,
177
+ duration: alignmentData.duration,
178
+ audioUrl: streamingUrls.synthesisUrl,
179
+ alignmentUrl: streamingUrls.alignmentUrl,
180
+ createdAt: existingEntry?.createdAt || new Date().toISOString(),
181
+ updatedAt: new Date().toISOString(),
182
+ };
183
+
184
+ // Update audio map
185
+ setAudioEntry(audioMap, item.slug, artifact);
186
+
187
+ onProgress(`✅ ${item.slug} - ${status} complete`, "success");
188
+
189
+ return {
190
+ slug: item.slug,
191
+ status,
192
+ projectUuid: response.projectUuid,
193
+ };
194
+ } catch (error) {
195
+ const message = error instanceof Error ? error.message : String(error);
196
+ onProgress(`❌ ${item.slug} - error: ${message}`, "error");
197
+
198
+ return {
199
+ slug: item.slug,
200
+ status: "error",
201
+ error: message,
202
+ };
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Check if the configuration and API key are valid.
208
+ */
209
+ export async function checkConfig(
210
+ config: VocaSyncConfig,
211
+ client: VocaSyncClient
212
+ ): Promise<{ valid: boolean; message: string; balance?: number }> {
213
+ try {
214
+ // Validate API key
215
+ const validation = await client.validateApiKey();
216
+
217
+ if (!validation.valid) {
218
+ return {
219
+ valid: false,
220
+ message: "Invalid API key. Check your VOCASYNC_API_KEY environment variable.",
221
+ };
222
+ }
223
+
224
+ // Check content collection
225
+ const items = await loadContent(config.collection, config.frontmatterField);
226
+
227
+ return {
228
+ valid: true,
229
+ message: `Configuration valid. Found ${items.length} content items. Balance: $${validation.balance?.toFixed(2) ?? "?"}.`,
230
+ balance: validation.balance,
231
+ };
232
+ } catch (error) {
233
+ const message = error instanceof Error ? error.message : String(error);
234
+ return {
235
+ valid: false,
236
+ message: `Configuration error: ${message}`,
237
+ };
238
+ }
239
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Content item loaded from a collection
3
+ */
4
+ export interface ContentItem {
5
+ /** Unique slug identifier */
6
+ slug: string;
7
+ /** Original markdown/mdx content */
8
+ content: string;
9
+ /** Frontmatter data */
10
+ frontmatter: Record<string, unknown>;
11
+ /** Relative file path */
12
+ filePath: string;
13
+ }
14
+
15
+ /**
16
+ * Processed speech document ready for synthesis
17
+ */
18
+ export interface SpeechDocument {
19
+ /** Unique slug identifier */
20
+ slug: string;
21
+ /** Plain text for TTS synthesis */
22
+ text: string;
23
+ /** Content hash for change detection */
24
+ hash: string;
25
+ /** Source file path */
26
+ source: string;
27
+ }
28
+
29
+ /**
30
+ * Word alignment data from VocaSync API
31
+ */
32
+ export interface AlignedWord {
33
+ /** The word text */
34
+ word: string;
35
+ /** Start time in seconds */
36
+ start: number;
37
+ /** End time in seconds */
38
+ end: number;
39
+ }
40
+
41
+ /**
42
+ * Audio artifact entry in audio-map.json
43
+ */
44
+ export interface AudioArtifact {
45
+ /** Synthesis project UUID */
46
+ projectUuid: string;
47
+ /** Content hash used for change detection */
48
+ contentHash: string;
49
+ /** Total audio duration in seconds */
50
+ duration: number;
51
+ /** URL to stream audio (stable, non-expiring) */
52
+ audioUrl: string;
53
+ /** URL to fetch alignment JSON (stable, non-expiring) */
54
+ alignmentUrl: string;
55
+ /** When this artifact was created */
56
+ createdAt: string;
57
+ /** When this artifact was last updated */
58
+ updatedAt: string;
59
+ }
60
+
61
+ /**
62
+ * The audio-map.json file structure
63
+ */
64
+ export interface AudioMap {
65
+ /** Version of the audio-map schema */
66
+ version: 1;
67
+ /** When the map was last updated */
68
+ updatedAt: string;
69
+ /** Map of slug -> audio artifact */
70
+ entries: Record<string, AudioArtifact>;
71
+ }
72
+
73
+ /**
74
+ * VocaSync API synthesis request
75
+ */
76
+ export interface SynthesisRequest {
77
+ name: string;
78
+ text: string;
79
+ voice: string;
80
+ speed?: number;
81
+ quality?: "sd" | "hd";
82
+ align?: boolean;
83
+ language?: string;
84
+ }
85
+
86
+ /**
87
+ * VocaSync API synthesis response
88
+ */
89
+ export interface SynthesisResponse {
90
+ projectUuid: string;
91
+ estimatedCost: number;
92
+ }
93
+
94
+ /**
95
+ * Job status from VocaSync API
96
+ */
97
+ export type JobStatus = "pending" | "processing" | "completed" | "failed";
98
+
99
+ /**
100
+ * API Artifact response
101
+ */
102
+ export interface ArtifactResponse {
103
+ id: string;
104
+ artifactType: "alignment" | "synthesis";
105
+ createdAt: number;
106
+ alignmentFileKey?: string;
107
+ alignmentFileSizeBytes?: number;
108
+ srtFileKey?: string;
109
+ srtFileSizeBytes?: number;
110
+ vttFileKey?: string;
111
+ vttFileSizeBytes?: number;
112
+ synthesisFileKey?: string;
113
+ synthesisFileSizeBytes?: number;
114
+ synthesisFileName?: string;
115
+ }
116
+
117
+ /**
118
+ * API Job response
119
+ */
120
+ export interface JobResponse {
121
+ id: string;
122
+ status: JobStatus;
123
+ createdAt: number;
124
+ completedAt: number | null;
125
+ error: string | null;
126
+ }
127
+
128
+ /**
129
+ * Linked alignment project status
130
+ */
131
+ export interface LinkedAlignmentStatus {
132
+ projectUuid: string;
133
+ status: JobStatus;
134
+ error?: string | null;
135
+ }
136
+
137
+ /**
138
+ * Project response from /projects/{uuid} endpoint
139
+ */
140
+ export interface ProjectResponse {
141
+ id: string;
142
+ projectUuid: string;
143
+ name: string | null;
144
+ description: string | null;
145
+ projectType: "alignment" | "synthesis";
146
+ source: "web" | "api";
147
+ language: string | null;
148
+ status: JobStatus;
149
+ characterCount: number | null;
150
+ audioFileDurationSeconds: number | null;
151
+ createdAt: number;
152
+ completedAt: number | null;
153
+ error: string | null;
154
+ artifacts: ArtifactResponse[];
155
+ latestJob?: JobResponse | null;
156
+ linkedAlignment?: LinkedAlignmentStatus | null;
157
+ }
158
+
159
+ /**
160
+ * Project status (internal representation)
161
+ */
162
+ export interface ProjectStatus {
163
+ uuid: string;
164
+ name: string;
165
+ status: JobStatus;
166
+ synthesisJob?: {
167
+ status: JobStatus;
168
+ audioUrl?: string;
169
+ duration?: number;
170
+ error?: string;
171
+ };
172
+ alignmentJob?: {
173
+ status: JobStatus;
174
+ alignmentUrl?: string;
175
+ error?: string;
176
+ };
177
+ }
178
+
179
+ /**
180
+ * Alignment data fetched from VocaSync
181
+ */
182
+ export interface AlignmentData {
183
+ words: AlignedWord[];
184
+ duration: number;
185
+ }
186
+
187
+ /**
188
+ * Sync status for a content item
189
+ */
190
+ export type SyncStatus = "unchanged" | "new" | "updated" | "error";
191
+
192
+ /**
193
+ * Result of syncing a single content item
194
+ */
195
+ export interface SyncResult {
196
+ slug: string;
197
+ status: SyncStatus;
198
+ projectUuid?: string;
199
+ error?: string;
200
+ }
201
+
202
+ /**
203
+ * Overall sync summary
204
+ */
205
+ export interface SyncSummary {
206
+ total: number;
207
+ unchanged: number;
208
+ synced: number;
209
+ errors: number;
210
+ results: SyncResult[];
211
+ }
@@ -0,0 +1,11 @@
1
+ // Type declarations for modules without types
2
+
3
+ declare module "speech-rule-engine" {
4
+ export function engineReady(): Promise<void>;
5
+ export function setupEngine(options: {
6
+ locale?: string;
7
+ modality?: string;
8
+ style?: string;
9
+ }): void;
10
+ export function toSpeech(latex: string): string;
11
+ }