@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,196 @@
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
+ * Processed speech document ready for synthesis
16
+ */
17
+ export interface SpeechDocument {
18
+ /** Unique slug identifier */
19
+ slug: string;
20
+ /** Plain text for TTS synthesis */
21
+ text: string;
22
+ /** Content hash for change detection */
23
+ hash: string;
24
+ /** Source file path */
25
+ source: string;
26
+ }
27
+ /**
28
+ * Word alignment data from VocaSync API
29
+ */
30
+ export interface AlignedWord {
31
+ /** The word text */
32
+ word: string;
33
+ /** Start time in seconds */
34
+ start: number;
35
+ /** End time in seconds */
36
+ end: number;
37
+ }
38
+ /**
39
+ * Audio artifact entry in audio-map.json
40
+ */
41
+ export interface AudioArtifact {
42
+ /** Synthesis project UUID */
43
+ projectUuid: string;
44
+ /** Content hash used for change detection */
45
+ contentHash: string;
46
+ /** Total audio duration in seconds */
47
+ duration: number;
48
+ /** URL to stream audio (stable, non-expiring) */
49
+ audioUrl: string;
50
+ /** URL to fetch alignment JSON (stable, non-expiring) */
51
+ alignmentUrl: string;
52
+ /** When this artifact was created */
53
+ createdAt: string;
54
+ /** When this artifact was last updated */
55
+ updatedAt: string;
56
+ }
57
+ /**
58
+ * The audio-map.json file structure
59
+ */
60
+ export interface AudioMap {
61
+ /** Version of the audio-map schema */
62
+ version: 1;
63
+ /** When the map was last updated */
64
+ updatedAt: string;
65
+ /** Map of slug -> audio artifact */
66
+ entries: Record<string, AudioArtifact>;
67
+ }
68
+ /**
69
+ * VocaSync API synthesis request
70
+ */
71
+ export interface SynthesisRequest {
72
+ name: string;
73
+ text: string;
74
+ voice: string;
75
+ speed?: number;
76
+ quality?: "sd" | "hd";
77
+ align?: boolean;
78
+ language?: string;
79
+ }
80
+ /**
81
+ * VocaSync API synthesis response
82
+ */
83
+ export interface SynthesisResponse {
84
+ projectUuid: string;
85
+ estimatedCost: number;
86
+ }
87
+ /**
88
+ * Job status from VocaSync API
89
+ */
90
+ export type JobStatus = "pending" | "processing" | "completed" | "failed";
91
+ /**
92
+ * API Artifact response
93
+ */
94
+ export interface ArtifactResponse {
95
+ id: string;
96
+ artifactType: "alignment" | "synthesis";
97
+ createdAt: number;
98
+ alignmentFileKey?: string;
99
+ alignmentFileSizeBytes?: number;
100
+ srtFileKey?: string;
101
+ srtFileSizeBytes?: number;
102
+ vttFileKey?: string;
103
+ vttFileSizeBytes?: number;
104
+ synthesisFileKey?: string;
105
+ synthesisFileSizeBytes?: number;
106
+ synthesisFileName?: string;
107
+ }
108
+ /**
109
+ * API Job response
110
+ */
111
+ export interface JobResponse {
112
+ id: string;
113
+ status: JobStatus;
114
+ createdAt: number;
115
+ completedAt: number | null;
116
+ error: string | null;
117
+ }
118
+ /**
119
+ * Linked alignment project status
120
+ */
121
+ export interface LinkedAlignmentStatus {
122
+ projectUuid: string;
123
+ status: JobStatus;
124
+ error?: string | null;
125
+ }
126
+ /**
127
+ * Project response from /projects/{uuid} endpoint
128
+ */
129
+ export interface ProjectResponse {
130
+ id: string;
131
+ projectUuid: string;
132
+ name: string | null;
133
+ description: string | null;
134
+ projectType: "alignment" | "synthesis";
135
+ source: "web" | "api";
136
+ language: string | null;
137
+ status: JobStatus;
138
+ characterCount: number | null;
139
+ audioFileDurationSeconds: number | null;
140
+ createdAt: number;
141
+ completedAt: number | null;
142
+ error: string | null;
143
+ artifacts: ArtifactResponse[];
144
+ latestJob?: JobResponse | null;
145
+ linkedAlignment?: LinkedAlignmentStatus | null;
146
+ }
147
+ /**
148
+ * Project status (internal representation)
149
+ */
150
+ export interface ProjectStatus {
151
+ uuid: string;
152
+ name: string;
153
+ status: JobStatus;
154
+ synthesisJob?: {
155
+ status: JobStatus;
156
+ audioUrl?: string;
157
+ duration?: number;
158
+ error?: string;
159
+ };
160
+ alignmentJob?: {
161
+ status: JobStatus;
162
+ alignmentUrl?: string;
163
+ error?: string;
164
+ };
165
+ }
166
+ /**
167
+ * Alignment data fetched from VocaSync
168
+ */
169
+ export interface AlignmentData {
170
+ words: AlignedWord[];
171
+ duration: number;
172
+ }
173
+ /**
174
+ * Sync status for a content item
175
+ */
176
+ export type SyncStatus = "unchanged" | "new" | "updated" | "error";
177
+ /**
178
+ * Result of syncing a single content item
179
+ */
180
+ export interface SyncResult {
181
+ slug: string;
182
+ status: SyncStatus;
183
+ projectUuid?: string;
184
+ error?: string;
185
+ }
186
+ /**
187
+ * Overall sync summary
188
+ */
189
+ export interface SyncSummary {
190
+ total: number;
191
+ unchanged: number;
192
+ synced: number;
193
+ errors: number;
194
+ results: SyncResult[];
195
+ }
196
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,OAAO,EAAE,MAAM,CAAC;IAChB,uBAAuB;IACvB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,yBAAyB;IACzB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,uBAAuB;IACvB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,oBAAoB;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,4BAA4B;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,0BAA0B;IAC1B,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,6BAA6B;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,sCAAsC;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,yDAAyD;IACzD,YAAY,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,QAA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AK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,YAAY,CAAC,EAAE;QACb,MAAM,EAAE,SAAS,CAAC;QAClB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,GAAG,KAAK,GAAG,SAAS,GAAG,OAAO,CAAC;AAEnE;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,UAAU,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@vocasync/astro",
3
+ "version": "0.1.0",
4
+ "description": "VocaSync integration for Astro - Text-to-speech synthesis and word-level alignment for static sites",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ },
11
+ "./components": {
12
+ "types": "./dist/components/index.d.ts",
13
+ "import": "./dist/components/index.js"
14
+ },
15
+ "./components/AudioPlayer.astro": "./dist/components/AudioPlayer.astro",
16
+ "./rehype": {
17
+ "types": "./dist/rehype/index.d.ts",
18
+ "import": "./dist/rehype/index.js"
19
+ },
20
+ "./styles": "./dist/styles/variables.css",
21
+ "./styles/variables.css": "./dist/styles/variables.css"
22
+ },
23
+ "bin": {
24
+ "vocasync": "./dist/cli.js"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "src"
29
+ ],
30
+ "scripts": {
31
+ "build": "tsc && cp -r src/components/*.astro dist/components/ && cp -r src/styles dist/",
32
+ "dev": "tsc --watch",
33
+ "check": "biome check .",
34
+ "check:fix": "biome check --write .",
35
+ "prepublishOnly": "bun run build"
36
+ },
37
+ "keywords": [
38
+ "astro",
39
+ "astro-integration",
40
+ "tts",
41
+ "text-to-speech",
42
+ "audio",
43
+ "alignment",
44
+ "vocasync",
45
+ "podcast",
46
+ "accessibility"
47
+ ],
48
+ "author": "Sayed Hamid Fatimi <sayedhfatimi@pm.me> (https://github.com/sayedhfatimi)",
49
+ "license": "MIT",
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/sayedhfatimi/vocasync-astro.git"
53
+ },
54
+ "homepage": "https://github.com/sayedhfatimi/vocasync-astro#readme",
55
+ "bugs": {
56
+ "url": "https://github.com/sayedhfatimi/vocasync-astro/issues"
57
+ },
58
+ "peerDependencies": {
59
+ "astro": "^4.0.0 || ^5.0.0",
60
+ "speech-rule-engine": "^4.0.0",
61
+ "mathjax-full": "^3.2.0"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "speech-rule-engine": {
65
+ "optional": true
66
+ },
67
+ "mathjax-full": {
68
+ "optional": true
69
+ }
70
+ },
71
+ "dependencies": {
72
+ "zod": "^3.23.0",
73
+ "unified": "^11.0.0",
74
+ "remark-parse": "^11.0.0",
75
+ "remark-math": "^6.0.0",
76
+ "unist-util-visit-parents": "^6.0.0"
77
+ },
78
+ "devDependencies": {
79
+ "@biomejs/biome": "^1.9.0",
80
+ "@types/hast": "^3.0.4",
81
+ "@types/mdast": "^4.0.4",
82
+ "@types/node": "^22.0.0",
83
+ "astro": "^5.0.0",
84
+ "typescript": "^5.7.0"
85
+ },
86
+ "engines": {
87
+ "node": ">=18.0.0"
88
+ }
89
+ }
@@ -0,0 +1,316 @@
1
+ import type { VocaSyncConfig } from "../config/index.js";
2
+ import type {
3
+ SynthesisResponse,
4
+ ProjectStatus,
5
+ ProjectResponse,
6
+ AlignmentData,
7
+ AlignedWord,
8
+ } from "../types/index.js";
9
+
10
+ /**
11
+ * VocaSync API client for synthesis and alignment operations.
12
+ */
13
+ export class VocaSyncClient {
14
+ private baseUrl: string;
15
+ private apiKey: string;
16
+
17
+ constructor(apiKey: string, baseUrl = "https://vocasync.io/api/v1") {
18
+ this.apiKey = apiKey;
19
+ this.baseUrl = baseUrl;
20
+ }
21
+
22
+ /**
23
+ * Make an authenticated API request.
24
+ */
25
+ private async request<T>(
26
+ method: string,
27
+ endpoint: string,
28
+ body?: unknown
29
+ ): Promise<T> {
30
+ const url = `${this.baseUrl}${endpoint}`;
31
+
32
+ const headers: Record<string, string> = {
33
+ Authorization: `Bearer ${this.apiKey}`,
34
+ "Content-Type": "application/json",
35
+ };
36
+
37
+ const response = await fetch(url, {
38
+ method,
39
+ headers,
40
+ body: body ? JSON.stringify(body) : undefined,
41
+ });
42
+
43
+ if (!response.ok) {
44
+ const errorBody = await response.text();
45
+ throw new VocaSyncAPIError(
46
+ `API request failed: ${response.status} ${response.statusText}`,
47
+ response.status,
48
+ errorBody
49
+ );
50
+ }
51
+
52
+ return response.json() as T;
53
+ }
54
+
55
+ /**
56
+ * Create a synthesis project with alignment.
57
+ *
58
+ * Uses the `align=true` flag to automatically create a linked alignment project
59
+ * that starts processing once synthesis completes.
60
+ */
61
+ async synthesize(params: {
62
+ name: string;
63
+ text: string;
64
+ voice: string;
65
+ quality: "sd" | "hd";
66
+ language: string;
67
+ }): Promise<SynthesisResponse> {
68
+ const url = `${this.baseUrl}/synthesis`;
69
+
70
+ // API expects multipart/form-data
71
+ const formData = new FormData();
72
+ formData.append("textType", "text");
73
+ formData.append("text", params.text);
74
+ formData.append("voice", params.voice);
75
+ formData.append("quality", params.quality);
76
+ formData.append("language", params.language);
77
+ formData.append("projectName", params.name);
78
+ formData.append("align", "true"); // Auto-create alignment project
79
+
80
+ const response = await fetch(url, {
81
+ method: "POST",
82
+ headers: {
83
+ Authorization: `Bearer ${this.apiKey}`,
84
+ // Don't set Content-Type - let fetch set it with boundary for FormData
85
+ },
86
+ body: formData,
87
+ });
88
+
89
+ if (!response.ok) {
90
+ const errorBody = await response.text();
91
+ throw new VocaSyncAPIError(
92
+ `API request failed: ${response.status} ${response.statusText}`,
93
+ response.status,
94
+ errorBody
95
+ );
96
+ }
97
+
98
+ return (await response.json()) as SynthesisResponse;
99
+ }
100
+
101
+ /**
102
+ * Get project status including synthesis and alignment job status.
103
+ */
104
+ async getProjectStatus(projectUuid: string): Promise<ProjectStatus> {
105
+ // Fetch full project from the API
106
+ const project = await this.request<ProjectResponse>("GET", `/projects/${projectUuid}`);
107
+
108
+ // Find synthesis artifact
109
+ const synthesisArtifact = project.artifacts.find(a => a.artifactType === "synthesis");
110
+
111
+ // Map to internal ProjectStatus format
112
+ const status: ProjectStatus = {
113
+ uuid: project.projectUuid,
114
+ name: project.name || "",
115
+ status: project.status,
116
+ };
117
+
118
+ // Add synthesis job info if we have a synthesis artifact or this is a synthesis project
119
+ if (synthesisArtifact || project.projectType === "synthesis") {
120
+ status.synthesisJob = {
121
+ status: project.status,
122
+ duration: project.audioFileDurationSeconds || undefined,
123
+ error: project.error || undefined,
124
+ };
125
+ if (synthesisArtifact?.synthesisFileKey) {
126
+ status.synthesisJob.audioUrl = `/stream/${projectUuid}/synthesis`;
127
+ }
128
+ }
129
+
130
+ // Add alignment job info from linked alignment project (for synthesis with align=true)
131
+ if (project.linkedAlignment) {
132
+ status.alignmentJob = {
133
+ status: project.linkedAlignment.status,
134
+ error: project.linkedAlignment.error || undefined,
135
+ };
136
+ // Alignment URL is on the synthesis project (uses linkedAlignmentProjectId internally)
137
+ if (project.linkedAlignment.status === "completed") {
138
+ status.alignmentJob.alignmentUrl = `/stream/${projectUuid}/alignment`;
139
+ }
140
+ }
141
+
142
+ return status;
143
+ }
144
+
145
+ /**
146
+ * Fetch alignment data (words with timestamps) from completed alignment job.
147
+ */
148
+ async getAlignment(alignmentUrl: string): Promise<AlignmentData> {
149
+ // The alignmentUrl may be a full URL or relative
150
+ const url = alignmentUrl.startsWith("http")
151
+ ? alignmentUrl
152
+ : `${this.baseUrl}${alignmentUrl}`;
153
+
154
+ const response = await fetch(url, {
155
+ headers: {
156
+ Authorization: `Bearer ${this.apiKey}`,
157
+ },
158
+ });
159
+
160
+ if (!response.ok) {
161
+ throw new VocaSyncAPIError(
162
+ `Failed to fetch alignment: ${response.status}`,
163
+ response.status
164
+ );
165
+ }
166
+
167
+ const data = await response.json() as { words: AlignedWord[]; duration: number };
168
+ return data;
169
+ }
170
+
171
+ /**
172
+ * Poll project status until completion or failure.
173
+ *
174
+ * @param projectUuid - The project to poll
175
+ * @param options - Polling options
176
+ * @returns The final project status
177
+ */
178
+ async pollUntilComplete(
179
+ projectUuid: string,
180
+ options: {
181
+ maxAttempts?: number;
182
+ intervalMs?: number;
183
+ onProgress?: (status: ProjectStatus) => void;
184
+ } = {}
185
+ ): Promise<ProjectStatus> {
186
+ const { maxAttempts = 120, intervalMs = 5000, onProgress } = options;
187
+
188
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
189
+ const status = await this.getProjectStatus(projectUuid);
190
+
191
+ onProgress?.(status);
192
+
193
+ // Check if synthesis is complete
194
+ const synthComplete =
195
+ status.synthesisJob?.status === "completed" ||
196
+ status.synthesisJob?.status === "failed";
197
+
198
+ // For alignment: if synthesis is complete but alignment job doesn't exist yet,
199
+ // keep polling because the backend may still be creating the linked alignment project
200
+ const alignComplete =
201
+ status.alignmentJob?.status === "completed" ||
202
+ status.alignmentJob?.status === "failed";
203
+
204
+ // Only exit when synth is complete AND either:
205
+ // 1. Alignment is complete/failed, OR
206
+ // 2. We've polled enough times after synth completion that alignment should have started
207
+ // (alignment job will appear as "pending" or "processing" once linked)
208
+ const shouldWaitForAlignment = synthComplete && !status.alignmentJob;
209
+
210
+ if (synthComplete && alignComplete) {
211
+ // Check for failures
212
+ if (status.synthesisJob?.status === "failed") {
213
+ throw new VocaSyncAPIError(
214
+ `Synthesis failed: ${status.synthesisJob.error || "Unknown error"}`,
215
+ 500
216
+ );
217
+ }
218
+ if (status.alignmentJob?.status === "failed") {
219
+ throw new VocaSyncAPIError(
220
+ `Alignment failed: ${status.alignmentJob.error || "Unknown error"}`,
221
+ 500
222
+ );
223
+ }
224
+
225
+ return status;
226
+ }
227
+
228
+ // If synthesis is complete but no alignment job yet, wait a bit longer
229
+ // The backend creates the alignment project asynchronously after synthesis
230
+ if (shouldWaitForAlignment && attempt > 5) {
231
+ // After 5 additional polls with no alignment appearing, assume it won't be created
232
+ // This handles the case where align=false or alignment language isn't supported
233
+ return status;
234
+ }
235
+
236
+ // Wait before next poll
237
+ await sleep(intervalMs);
238
+ }
239
+
240
+ throw new VocaSyncAPIError("Polling timeout - job did not complete in time", 408);
241
+ }
242
+
243
+ /**
244
+ * Check API key validity and account balance.
245
+ */
246
+ async validateApiKey(): Promise<{ valid: boolean; balance?: number }> {
247
+ try {
248
+ const response = await this.request<{ balance: number }>("GET", "/account");
249
+ return { valid: true, balance: response.balance };
250
+ } catch (error) {
251
+ if (error instanceof VocaSyncAPIError && error.statusCode === 401) {
252
+ return { valid: false };
253
+ }
254
+ throw error;
255
+ }
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Custom error class for VocaSync API errors.
261
+ */
262
+ export class VocaSyncAPIError extends Error {
263
+ statusCode: number;
264
+ responseBody?: string;
265
+
266
+ constructor(message: string, statusCode: number, responseBody?: string) {
267
+ super(message);
268
+ this.name = "VocaSyncAPIError";
269
+ this.statusCode = statusCode;
270
+ this.responseBody = responseBody;
271
+ }
272
+ }
273
+
274
+ /**
275
+ * Sleep utility for polling.
276
+ */
277
+ function sleep(ms: number): Promise<void> {
278
+ return new Promise((resolve) => setTimeout(resolve, ms));
279
+ }
280
+
281
+ /**
282
+ * Create a VocaSync client from configuration.
283
+ */
284
+ export function createClient(_config: VocaSyncConfig): VocaSyncClient {
285
+ const apiKey = process.env.VOCASYNC_API_KEY;
286
+
287
+ if (!apiKey) {
288
+ throw new Error(
289
+ "VOCASYNC_API_KEY environment variable is not set.\n" +
290
+ "Get your API key at https://vocasync.io and add it to your .env file."
291
+ );
292
+ }
293
+
294
+ return new VocaSyncClient(apiKey);
295
+ }
296
+
297
+ /**
298
+ * Get streaming URLs for stable audio/alignment access.
299
+ * These URLs don't expire and redirect to fresh presigned URLs on each request.
300
+ */
301
+ export function getStreamingUrls(
302
+ projectUuid: string,
303
+ baseUrl = "https://vocasync.io/api/v1"
304
+ ): {
305
+ synthesisUrl: string;
306
+ alignmentUrl: string;
307
+ srtUrl: string;
308
+ vttUrl: string;
309
+ } {
310
+ return {
311
+ synthesisUrl: `${baseUrl}/stream/${projectUuid}/synthesis`,
312
+ alignmentUrl: `${baseUrl}/stream/${projectUuid}/alignment`,
313
+ srtUrl: `${baseUrl}/stream/${projectUuid}/subtitles.srt`,
314
+ vttUrl: `${baseUrl}/stream/${projectUuid}/subtitles.vtt`,
315
+ };
316
+ }
@@ -0,0 +1 @@
1
+ export { VocaSyncClient, VocaSyncAPIError, createClient, getStreamingUrls } from "./client.js";