@vocalstack/sanity 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +125 -2
- package/dist/action.d.ts +13 -0
- package/dist/action.js +86 -0
- package/dist/api.d.ts +78 -0
- package/dist/api.js +74 -0
- package/dist/components/Brand.d.ts +12 -0
- package/dist/components/Brand.js +21 -0
- package/dist/components/TranscriptInput.d.ts +7 -0
- package/dist/components/TranscriptInput.js +173 -0
- package/dist/components/brandAssets.d.ts +11 -0
- package/dist/components/brandAssets.js +6 -0
- package/dist/core.d.ts +13 -0
- package/dist/core.js +36 -0
- package/dist/handler.d.ts +20 -0
- package/dist/handler.js +115 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +22 -0
- package/dist/schema.d.ts +12 -0
- package/dist/schema.js +51 -0
- package/dist/server/index.d.ts +41 -0
- package/dist/server/index.js +154 -0
- package/dist/server/jobs.d.ts +10 -0
- package/dist/server/jobs.js +53 -0
- package/dist/text.d.ts +22 -0
- package/dist/text.js +52 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.js +1 -0
- package/dist/wire.d.ts +52 -0
- package/dist/wire.js +10 -0
- package/package.json +55 -4
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { VocalStackAPIError } from '../api.js';
|
|
2
|
+
import { transcriptText } from '../text.js';
|
|
3
|
+
/** A transcript's sentences: seconds, speaker, text. */
|
|
4
|
+
export function toSentences(t) {
|
|
5
|
+
return t.sentences
|
|
6
|
+
.filter((s) => !s.suppressed && s.text.trim())
|
|
7
|
+
.map(({ end, sentenceId, speaker, start, text }) => ({ id: sentenceId, end, speaker, start, text: text.trim() }));
|
|
8
|
+
}
|
|
9
|
+
const round = (n) => Math.round(n * 1000) / 1000;
|
|
10
|
+
/** As stored on the document: an array item needs a `_key`. */
|
|
11
|
+
export const toSaved = (sentences) => sentences.map((s, i) => ({ _key: `s${s.id ?? i}`, _type: 'sentence', end: round(s.end), speaker: s.speaker, start: round(s.start), text: s.text }));
|
|
12
|
+
export async function checkJob(api, recordingId) {
|
|
13
|
+
const job = await api.job(recordingId);
|
|
14
|
+
if (job.state === 'stopped' || (job.state === 'failed' && job.errorCode === 'stopped'))
|
|
15
|
+
return { kind: 'stopped' };
|
|
16
|
+
if (job.state === 'failed' || job.state === 'cropping') {
|
|
17
|
+
const error = job.state === 'cropping' ? 'VocalStack is waiting for a crop window; open the transcription in VocalStack.' : (job.error ?? 'The transcription failed.');
|
|
18
|
+
return { kind: 'failed', message: error, save: { error, status: 'failed' } };
|
|
19
|
+
}
|
|
20
|
+
if (job.state !== 'done') {
|
|
21
|
+
const progress = job.state === 'fetching' ? (job.progress.phasePct ?? 0) : job.progress.pct;
|
|
22
|
+
if (job.state !== 'transcribing')
|
|
23
|
+
return { kind: 'processing', phase: job.progress.phase ?? job.state, progress };
|
|
24
|
+
// Sentences are readable while the job runs; send what is there so far.
|
|
25
|
+
const partial = await api.transcript(recordingId).then(toSentences, () => undefined);
|
|
26
|
+
return { audio: { processedS: job.progress.processedS ?? 0, totalS: job.progress.totalS ?? undefined }, kind: 'processing', partial, phase: 'transcribing', progress };
|
|
27
|
+
}
|
|
28
|
+
const transcript = await api.transcript(recordingId);
|
|
29
|
+
const sentences = toSentences(transcript);
|
|
30
|
+
return {
|
|
31
|
+
kind: 'done',
|
|
32
|
+
save: {
|
|
33
|
+
error: null,
|
|
34
|
+
language: transcript.lang ?? transcript.sentences.find((s) => s.language)?.language ?? null,
|
|
35
|
+
sentences: toSaved(sentences),
|
|
36
|
+
status: 'done',
|
|
37
|
+
transcribedAt: new Date().toISOString(),
|
|
38
|
+
transcript: transcriptText(sentences),
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Stop a job. If it ended meanwhile, take its outcome instead. */
|
|
43
|
+
export async function stopJob(api, recordingId) {
|
|
44
|
+
try {
|
|
45
|
+
await api.stop(recordingId, 'Stopped from Sanity');
|
|
46
|
+
return { kind: 'stopped' };
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
if (err instanceof VocalStackAPIError && err.status === 409)
|
|
50
|
+
return checkJob(api, recordingId).catch(() => ({ kind: 'stopped' }));
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TranscriptSentence, TranscriptWord } from './types.js';
|
|
2
|
+
/** Whether a new paragraph starts at this sentence: a new speaker, or a long pause. */
|
|
3
|
+
export declare const breaksParagraph: (s: TranscriptSentence, prev: TranscriptSentence | undefined) => boolean;
|
|
4
|
+
/** Sentences grouped into paragraphs. */
|
|
5
|
+
export declare function paragraphs(sentences: TranscriptSentence[]): TranscriptSentence[][];
|
|
6
|
+
/** The plain text: sentences joined, a blank line between paragraphs. */
|
|
7
|
+
export declare function transcriptText(sentences: TranscriptSentence[]): string;
|
|
8
|
+
/**
|
|
9
|
+
* Give each sentence the words that fall inside it, as compact [start, end, text]
|
|
10
|
+
* tuples. Words come from segments, which do not line up with sentences.
|
|
11
|
+
*/
|
|
12
|
+
export declare function attachWords<T extends TranscriptSentence>(sentences: T[], words: {
|
|
13
|
+
end: number;
|
|
14
|
+
start: number;
|
|
15
|
+
text: string;
|
|
16
|
+
}[]): T[];
|
|
17
|
+
/**
|
|
18
|
+
* The sentence's text as timed tokens. The text wins over the words (it carries
|
|
19
|
+
* VocalStack's later corrections); the words give the timing. When the counts
|
|
20
|
+
* differ, the times are spread evenly across the sentence.
|
|
21
|
+
*/
|
|
22
|
+
export declare function timedTokens(s: TranscriptSentence): TranscriptWord[];
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Whether a new paragraph starts at this sentence: a new speaker, or a long pause. */
|
|
2
|
+
export const breaksParagraph = (s, prev) => !prev || s.speaker !== prev.speaker || s.start - prev.end > 2.5;
|
|
3
|
+
/** Sentences grouped into paragraphs. */
|
|
4
|
+
export function paragraphs(sentences) {
|
|
5
|
+
const out = [];
|
|
6
|
+
sentences.forEach((s, i) => {
|
|
7
|
+
if (breaksParagraph(s, sentences[i - 1]))
|
|
8
|
+
out.push([]);
|
|
9
|
+
out[out.length - 1].push(s);
|
|
10
|
+
});
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
/** The plain text: sentences joined, a blank line between paragraphs. */
|
|
14
|
+
export function transcriptText(sentences) {
|
|
15
|
+
return paragraphs(sentences)
|
|
16
|
+
.map((p) => p.map((s) => s.text.trim()).join(' '))
|
|
17
|
+
.join('\n\n');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Give each sentence the words that fall inside it, as compact [start, end, text]
|
|
21
|
+
* tuples. Words come from segments, which do not line up with sentences.
|
|
22
|
+
*/
|
|
23
|
+
export function attachWords(sentences, words) {
|
|
24
|
+
const sorted = [...words].sort((a, b) => a.start - b.start);
|
|
25
|
+
let i = 0;
|
|
26
|
+
return sentences.map((s) => {
|
|
27
|
+
const own = [];
|
|
28
|
+
while (i < sorted.length && sorted[i].start < s.start - 0.05)
|
|
29
|
+
i++;
|
|
30
|
+
while (i < sorted.length && sorted[i].start < s.end + 0.05) {
|
|
31
|
+
const w = sorted[i++];
|
|
32
|
+
own.push([round(w.start), round(w.end), w.text.trim()]);
|
|
33
|
+
}
|
|
34
|
+
return own.length ? { ...s, words: own } : s;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const round = (n) => Math.round(n * 100) / 100;
|
|
38
|
+
/**
|
|
39
|
+
* The sentence's text as timed tokens. The text wins over the words (it carries
|
|
40
|
+
* VocalStack's later corrections); the words give the timing. When the counts
|
|
41
|
+
* differ, the times are spread evenly across the sentence.
|
|
42
|
+
*/
|
|
43
|
+
export function timedTokens(s) {
|
|
44
|
+
const tokens = s.text.trim().split(/\s+/).filter(Boolean);
|
|
45
|
+
const words = s.words ?? [];
|
|
46
|
+
if (words.length === tokens.length)
|
|
47
|
+
return tokens.map((t, i) => [words[i][0], words[i][1], t]);
|
|
48
|
+
if (!tokens.length)
|
|
49
|
+
return words;
|
|
50
|
+
const span = (s.end - s.start) / tokens.length;
|
|
51
|
+
return tokens.map((t, i) => [round(s.start + i * span), round(s.start + (i + 1) * span), t]);
|
|
52
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type VocalStackPluginOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Where the server handler (`@vocalstack/sanity/server`) is mounted, e.g.
|
|
4
|
+
* `https://www.example.com/api/vocalstack`. The Studio calls only this URL;
|
|
5
|
+
* the VocalStack API key stays on that server.
|
|
6
|
+
*/
|
|
7
|
+
handlerURL: string;
|
|
8
|
+
};
|
|
9
|
+
/** Options on a `vocalstack.transcript` field itself. */
|
|
10
|
+
export type TranscriptFieldOptions = {
|
|
11
|
+
/** The sibling `file` field holding the audio. Default: the first sibling file whose asset is `audio/*`. */
|
|
12
|
+
audioField?: string;
|
|
13
|
+
};
|
|
14
|
+
/** What the plugin reads from a `sanity.fileAsset`. */
|
|
15
|
+
export type AudioAsset = {
|
|
16
|
+
_id: string;
|
|
17
|
+
mimeType?: null | string;
|
|
18
|
+
originalFilename?: null | string;
|
|
19
|
+
url?: null | string;
|
|
20
|
+
};
|
|
21
|
+
export type TranscriptStatus = 'done' | 'failed' | 'idle' | 'processing';
|
|
22
|
+
/** A sentence as stored on the document: seconds, and the speaker's number. */
|
|
23
|
+
export type SavedSentence = {
|
|
24
|
+
_key: string;
|
|
25
|
+
_type?: string;
|
|
26
|
+
end: number;
|
|
27
|
+
speaker: number;
|
|
28
|
+
start: number;
|
|
29
|
+
text: string;
|
|
30
|
+
};
|
|
31
|
+
/** The `vocalstack.transcript` value. */
|
|
32
|
+
export type VocalStackTranscript = {
|
|
33
|
+
_type?: 'vocalstack.transcript';
|
|
34
|
+
error?: null | string;
|
|
35
|
+
language?: null | string;
|
|
36
|
+
recordingId?: null | string;
|
|
37
|
+
sentences?: null | SavedSentence[];
|
|
38
|
+
status?: null | TranscriptStatus;
|
|
39
|
+
transcript?: null | string;
|
|
40
|
+
transcriptionId?: null | string;
|
|
41
|
+
transcribedAt?: null | string;
|
|
42
|
+
};
|
|
43
|
+
/** [start, end, text], seconds. */
|
|
44
|
+
export type TranscriptWord = [number, number, string];
|
|
45
|
+
export type TranscriptSentence = {
|
|
46
|
+
/** Live only: words heard after the last settled sentence. */
|
|
47
|
+
draft?: boolean;
|
|
48
|
+
end: number;
|
|
49
|
+
/** VocalStack's sentenceId. */
|
|
50
|
+
id?: number;
|
|
51
|
+
speaker: number;
|
|
52
|
+
start: number;
|
|
53
|
+
text: string;
|
|
54
|
+
words?: TranscriptWord[];
|
|
55
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/wire.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the Studio and the server handler say to each other. No imports: both
|
|
3
|
+
* the Studio bundle and the server use it.
|
|
4
|
+
*/
|
|
5
|
+
import type { TranscriptSentence, VocalStackTranscript } from './types.js';
|
|
6
|
+
/** Where a job stands. `save` carries the fields to write on the document once it is over. */
|
|
7
|
+
export type JobOutcome = {
|
|
8
|
+
audio?: {
|
|
9
|
+
processedS: number;
|
|
10
|
+
totalS?: number;
|
|
11
|
+
};
|
|
12
|
+
kind: 'processing';
|
|
13
|
+
partial?: TranscriptSentence[];
|
|
14
|
+
phase: string;
|
|
15
|
+
progress: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'done' | 'failed';
|
|
18
|
+
message?: string;
|
|
19
|
+
save: VocalStackTranscript;
|
|
20
|
+
}
|
|
21
|
+
/** Stopped before it finished: the Studio puts the field back to how it was. */
|
|
22
|
+
| {
|
|
23
|
+
kind: 'stopped';
|
|
24
|
+
};
|
|
25
|
+
/** `POST {handlerURL}/transcriptions` */
|
|
26
|
+
export type StartRequest = {
|
|
27
|
+
/** The `sanity.fileAsset` id of the audio. The handler reads its URL and type itself. */
|
|
28
|
+
assetId: string;
|
|
29
|
+
documentId: string;
|
|
30
|
+
/** The transcript field's path in the document, for the job's idempotency key. */
|
|
31
|
+
fieldPath: string;
|
|
32
|
+
/** The transcription's name in VocalStack. Default: the file's name. */
|
|
33
|
+
name?: string;
|
|
34
|
+
};
|
|
35
|
+
export type StartResponse = Required<Pick<VocalStackTranscript, 'recordingId' | 'status' | 'transcriptionId'>>;
|
|
36
|
+
/** `GET {handlerURL}/connection` */
|
|
37
|
+
export type ConnectionResponse = {
|
|
38
|
+
account?: string;
|
|
39
|
+
connected: boolean;
|
|
40
|
+
};
|
|
41
|
+
/** Every error: `{ code?, error }`. `session` means the Studio should prove itself again. */
|
|
42
|
+
export type ErrorResponse = {
|
|
43
|
+
code?: string;
|
|
44
|
+
error: string;
|
|
45
|
+
};
|
|
46
|
+
/** The document type of a Studio session (see `SESSION_PREFIX`). */
|
|
47
|
+
export declare const SESSION_TYPE = "vocalstack.session";
|
|
48
|
+
/** Session ids are `vocalstack-session.<sha256 hex of the secret>`: dotted, so never public. */
|
|
49
|
+
export declare const SESSION_PREFIX = "vocalstack-session.";
|
|
50
|
+
/** How long a Studio session lasts, from the session document's `_updatedAt`. */
|
|
51
|
+
export declare const SESSION_MAX_AGE_MS: number;
|
|
52
|
+
export declare function sha256Hex(text: string): Promise<string>;
|
package/dist/wire.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The document type of a Studio session (see `SESSION_PREFIX`). */
|
|
2
|
+
export const SESSION_TYPE = 'vocalstack.session';
|
|
3
|
+
/** Session ids are `vocalstack-session.<sha256 hex of the secret>`: dotted, so never public. */
|
|
4
|
+
export const SESSION_PREFIX = 'vocalstack-session.';
|
|
5
|
+
/** How long a Studio session lasts, from the session document's `_updatedAt`. */
|
|
6
|
+
export const SESSION_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
7
|
+
export async function sha256Hex(text) {
|
|
8
|
+
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)));
|
|
9
|
+
return Array.from(digest, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vocalstack/sanity",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "VocalStack for Sanity Studio: a Transcribe button next to every audio file.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server/index.d.ts",
|
|
16
|
+
"default": "./dist/server/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
25
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@sanity/ui": "^2.0.0 || ^3.0.0 || ^4.0.0",
|
|
29
|
+
"react": "^18 || ^19",
|
|
30
|
+
"sanity": "^3 || ^4 || ^5 || ^6"
|
|
31
|
+
},
|
|
32
|
+
"peerDependenciesMeta": {
|
|
33
|
+
"@sanity/ui": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@sanity/mutator": "4.22.1",
|
|
39
|
+
"@sanity/ui": "^3.1.11",
|
|
40
|
+
"@types/react": "^19.0.0",
|
|
41
|
+
"groq-js": "1.30.3",
|
|
42
|
+
"mendoza": "3.0.8",
|
|
43
|
+
"playwright-core": "1.64.1",
|
|
44
|
+
"react": "^19.1.0",
|
|
45
|
+
"react-dom": "^19.1.0",
|
|
46
|
+
"sanity": "4.22.1",
|
|
47
|
+
"styled-components": "^6.1.15",
|
|
48
|
+
"typescript": "^5.6.0"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+ssh://git@github.com/vocalstack/sanity-plugin.git"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=20"
|
|
56
|
+
}
|
|
57
|
+
}
|