@vocalstack/sanity 0.2.0 → 0.4.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/README.md +4 -4
- package/dist/action.js +2 -2
- package/dist/api.d.ts +19 -20
- package/dist/api.js +49 -35
- package/dist/components/TranscriptInput.js +15 -14
- package/dist/schema.d.ts +10 -6
- package/dist/schema.js +1 -1
- package/dist/server/index.js +9 -9
- package/dist/server/jobs.d.ts +2 -2
- package/dist/server/jobs.js +7 -7
- package/dist/types.d.ts +1 -0
- package/dist/wire.d.ts +2 -2
- package/dist/wire.js +1 -1
- package/package.json +14 -11
package/README.md
CHANGED
|
@@ -66,13 +66,13 @@ defineType({
|
|
|
66
66
|
|
|
67
67
|
- **A `vocalstack.transcript` field type**, to put next to a `file` field (at the top level of a document, or inside an object). Its input has:
|
|
68
68
|
- the **workspace** the Studio is connected to, with its icon, at the top of the field (see [Workspace](#workspace));
|
|
69
|
-
- a **Transcribe** button, shown when the sibling file is `audio/*`. It shows progress and survives a reload mid-job, because `
|
|
69
|
+
- a **Transcribe** button, shown when the sibling file is `audio/*`. It shows progress and survives a reload mid-job, because `transcriptionId` and `status` are saved on the document. **Stop** ends a job you no longer want: the field goes back to how it was (an earlier transcript stays), and VocalStack keeps what it transcribed before the stop.
|
|
70
70
|
- the stored value:
|
|
71
71
|
- `transcript`: text you can edit; paragraphs break on a speaker change or a pause.
|
|
72
72
|
- `sentences`: `[{ start, end, speaker, text }]` in seconds.
|
|
73
|
-
- `status` (`idle`, `processing`, `done` or `failed`), `language`, `error`, `recordingId`, `transcriptionId
|
|
73
|
+
- `status` (`idle`, `processing`, `done` or `failed`), `language`, `error`, `transcriptionId` and `transcribedAt`. Documents saved by 0.2.x also carry a hidden `recordingId`, which is no longer read: a job left running under it is followed by its `transcriptionId`, and one with no `transcriptionId` offers Transcribe again.
|
|
74
74
|
- **A "Transcribe audio" document action** on every document type with a transcript field. It starts a job for each transcript field next to an audio file; the field follows the job from there.
|
|
75
|
-
- **The handler** (`@vocalstack/sanity/server`): `GET …/connection` (`{ connected, account, workspace: { id, name, personal, iconUrl } }`), `POST …/transcriptions`, `GET …/
|
|
75
|
+
- **The handler** (`@vocalstack/sanity/server`): `GET …/connection` (`{ connected, account, workspace: { id, name, personal, iconUrl } }`), `POST …/transcriptions`, `GET …/transcriptions/:id/job` and `POST …/transcriptions/:id/stop`.
|
|
76
76
|
|
|
77
77
|
Like any edit in the Studio, the transcript lands in the draft; publish to make it public. Transcripts are plain fields, so GROQ reads them like any other:
|
|
78
78
|
|
|
@@ -105,7 +105,7 @@ The handler, `createVocalStackHandler({ … })`:
|
|
|
105
105
|
| `allowedOrigins` | any | The Studio origins allowed to call the handler (CORS), e.g. `['https://example.sanity.studio']`. |
|
|
106
106
|
| `apiURL` | `https://api.vocalstack.com` | |
|
|
107
107
|
|
|
108
|
-
**How the audio reaches VocalStack.** The Studio sends the handler the file's asset id; the handler reads the asset from your dataset, checks that it is `audio/*`, and VocalStack fetches it from Sanity's CDN (`POST /v1/transcriptions { url }`). VocalStack does not fetch loopback or private addresses, so for those the handler reads the file and uploads it. The job runs on VocalStack, and the Studio polls the handler every 3 s. The key's workspace is billed per second, with a 5-minute minimum per transcription.
|
|
108
|
+
**How the audio reaches VocalStack.** The Studio sends the handler the file's asset id; the handler reads the asset from your dataset, checks that it is `audio/*`, and VocalStack fetches it from Sanity's CDN (`POST /v1/transcriptions { url }`). VocalStack does not fetch loopback or private addresses, so for those the handler reads the file and uploads it. The handler calls VocalStack through the [VocalStack JS SDK](https://www.npmjs.com/package/@vocalstack/js-sdk), which installs with the plugin; the Studio's bundle does not include it. The job runs on VocalStack, and the Studio polls the handler every 3 s. The key's workspace is billed per second, with a 5-minute minimum per transcription.
|
|
109
109
|
|
|
110
110
|
## How the handler knows the caller
|
|
111
111
|
|
package/dist/action.js
CHANGED
|
@@ -66,8 +66,8 @@ export const transcribeAction = (options) => {
|
|
|
66
66
|
const fields = await call('/transcriptions', { body, method: 'POST' });
|
|
67
67
|
patch.execute([
|
|
68
68
|
{ setIfMissing: { [at]: { _type: TRANSCRIPT_TYPE } } },
|
|
69
|
-
{ set: { [`${at}.status`]: fields.status, [`${at}.
|
|
70
|
-
{ unset: [`${at}.error`] },
|
|
69
|
+
{ set: { [`${at}.status`]: fields.status, [`${at}.transcriptionId`]: fields.transcriptionId } },
|
|
70
|
+
{ unset: [`${at}.error`, `${at}.recordingId`] },
|
|
71
71
|
]);
|
|
72
72
|
started++;
|
|
73
73
|
}
|
package/dist/api.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The handful of VocalStack API calls the plugin needs,
|
|
3
|
-
* SDK
|
|
2
|
+
* The handful of VocalStack API calls the plugin needs, made through the VocalStack
|
|
3
|
+
* JS SDK (`@vocalstack/js-sdk`). Only the server handler uses this: the API key, and
|
|
4
|
+
* the SDK, stay out of the Studio's bundle.
|
|
5
|
+
* Contract: https://api.vocalstack.com/v1/openapi.json
|
|
4
6
|
*/
|
|
7
|
+
import { VocalStack } from '@vocalstack/js-sdk';
|
|
5
8
|
import type { ConnectionWorkspace } from './wire.js';
|
|
6
9
|
export declare const DEFAULT_API_URL = "https://api.vocalstack.com";
|
|
7
10
|
/** Sent as the User-Agent of every call to VocalStack (they run in the
|
|
8
11
|
* handler, on the server): VocalStack lists the transcriptions it makes
|
|
9
12
|
* as made in Sanity. Keep it to package.json's version. */
|
|
10
|
-
export declare const PLUGIN_VERSION = "0.
|
|
13
|
+
export declare const PLUGIN_VERSION = "0.4.0";
|
|
14
|
+
export declare const USER_AGENT = "VocalStack-Sanity/0.4.0";
|
|
11
15
|
export declare const DEFAULT_DASHBOARD_URL = "https://app.vocalstack.com";
|
|
12
16
|
export type JobState = 'cropping' | 'done' | 'failed' | 'fetching' | 'queued' | 'stopped' | 'transcribing';
|
|
13
17
|
export type Job = {
|
|
@@ -21,7 +25,6 @@ export type Job = {
|
|
|
21
25
|
processedS?: number;
|
|
22
26
|
totalS?: null | number;
|
|
23
27
|
};
|
|
24
|
-
recordingId: string;
|
|
25
28
|
state: JobState;
|
|
26
29
|
transcriptionId: string;
|
|
27
30
|
};
|
|
@@ -58,26 +61,21 @@ export declare class VocalStackAPIError extends Error {
|
|
|
58
61
|
status: number;
|
|
59
62
|
constructor(status: number, code: string, message: string);
|
|
60
63
|
}
|
|
64
|
+
type JobOptions = {
|
|
65
|
+
idempotencyKey: string;
|
|
66
|
+
name: string;
|
|
67
|
+
spokenLanguage?: string;
|
|
68
|
+
};
|
|
61
69
|
export declare class VocalStackAPI {
|
|
62
|
-
|
|
63
|
-
private baseURL;
|
|
70
|
+
readonly sdk: VocalStack;
|
|
64
71
|
constructor(apiKey: string, baseURL?: string);
|
|
65
|
-
private request;
|
|
66
72
|
me(): Promise<Record<string, unknown>>;
|
|
67
|
-
transcribeURL(url: string, opts:
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
spokenLanguage?: string;
|
|
71
|
-
}): Promise<Transcription>;
|
|
72
|
-
transcribeFile(file: Blob, fileName: string, opts: {
|
|
73
|
-
idempotencyKey: string;
|
|
74
|
-
name: string;
|
|
75
|
-
spokenLanguage?: string;
|
|
76
|
-
}): Promise<Transcription>;
|
|
77
|
-
job(recordingId: string): Promise<Job>;
|
|
73
|
+
transcribeURL(url: string, opts: JobOptions): Promise<Transcription>;
|
|
74
|
+
transcribeFile(file: Blob, fileName: string, opts: JobOptions): Promise<Transcription>;
|
|
75
|
+
job(transcriptionId: string): Promise<Job>;
|
|
78
76
|
/** Stop a job before it is done. Its job fails with `stopped`; what was transcribed so far stays in VocalStack. */
|
|
79
|
-
stop(
|
|
80
|
-
transcript(
|
|
77
|
+
stop(transcriptionId: string, reason?: string): Promise<unknown>;
|
|
78
|
+
transcript(transcriptionId: string): Promise<Transcript>;
|
|
81
79
|
}
|
|
82
80
|
/** A short label for the connected account, from whatever `/v1/me` carries. */
|
|
83
81
|
export declare function accountLabel(me: Record<string, unknown>): string | undefined;
|
|
@@ -87,3 +85,4 @@ export declare function accountLabel(me: Record<string, unknown>): string | unde
|
|
|
87
85
|
* when the API does not say (an older VocalStack).
|
|
88
86
|
*/
|
|
89
87
|
export declare function workspaceOf(me: Record<string, unknown>, baseURL?: string): ConnectionWorkspace | undefined;
|
|
88
|
+
export {};
|
package/dist/api.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The handful of VocalStack API calls the plugin needs,
|
|
3
|
-
* SDK
|
|
2
|
+
* The handful of VocalStack API calls the plugin needs, made through the VocalStack
|
|
3
|
+
* JS SDK (`@vocalstack/js-sdk`). Only the server handler uses this: the API key, and
|
|
4
|
+
* the SDK, stay out of the Studio's bundle.
|
|
5
|
+
* Contract: https://api.vocalstack.com/v1/openapi.json
|
|
4
6
|
*/
|
|
7
|
+
import { VocalStack, VocalStackError } from '@vocalstack/js-sdk';
|
|
5
8
|
export const DEFAULT_API_URL = 'https://api.vocalstack.com';
|
|
6
9
|
/** Sent as the User-Agent of every call to VocalStack (they run in the
|
|
7
10
|
* handler, on the server): VocalStack lists the transcriptions it makes
|
|
8
11
|
* as made in Sanity. Keep it to package.json's version. */
|
|
9
|
-
export const PLUGIN_VERSION = '0.
|
|
12
|
+
export const PLUGIN_VERSION = '0.4.0';
|
|
13
|
+
export const USER_AGENT = `VocalStack-Sanity/${PLUGIN_VERSION}`;
|
|
10
14
|
export const DEFAULT_DASHBOARD_URL = 'https://app.vocalstack.com';
|
|
11
15
|
export class VocalStackAPIError extends Error {
|
|
12
16
|
code;
|
|
@@ -17,50 +21,60 @@ export class VocalStackAPIError extends Error {
|
|
|
17
21
|
this.status = status;
|
|
18
22
|
}
|
|
19
23
|
}
|
|
24
|
+
/** The SDK's errors as the handler reads them: VocalStack's status, code and message. */
|
|
25
|
+
function toAPIError(err) {
|
|
26
|
+
if (!(err instanceof VocalStackError))
|
|
27
|
+
return err;
|
|
28
|
+
// VocalStack was not reached: fetch's own error says why.
|
|
29
|
+
if (err.status === undefined)
|
|
30
|
+
return err.cause ?? err;
|
|
31
|
+
// An answer that is not VocalStack's error body (a proxy's page, say).
|
|
32
|
+
if (err.code === 'internal' && /^unexpected error \(status \d+\)$/.test(err.message))
|
|
33
|
+
return new VocalStackAPIError(err.status, 'http_error', `VocalStack answered ${err.status}`);
|
|
34
|
+
return new VocalStackAPIError(err.status, err.code, err.message);
|
|
35
|
+
}
|
|
36
|
+
const call = async (run) => {
|
|
37
|
+
try {
|
|
38
|
+
return (await run());
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
throw toAPIError(err);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const createOptions = (opts) => ({ name: opts.name, settings: opts.spokenLanguage ? { spokenLanguage: opts.spokenLanguage } : undefined });
|
|
20
45
|
export class VocalStackAPI {
|
|
21
|
-
|
|
22
|
-
baseURL;
|
|
46
|
+
sdk;
|
|
23
47
|
constructor(apiKey, baseURL = DEFAULT_API_URL) {
|
|
24
|
-
this.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
const res = await fetch(`${this.baseURL}${path}`, init);
|
|
36
|
-
const data = await res.json().catch(() => undefined);
|
|
37
|
-
if (!res.ok) {
|
|
38
|
-
const err = (data?.error ?? data);
|
|
39
|
-
throw new VocalStackAPIError(res.status, err?.code ?? 'http_error', err?.message ?? `VocalStack answered ${res.status}`);
|
|
40
|
-
}
|
|
41
|
-
return data;
|
|
48
|
+
this.sdk = new VocalStack({
|
|
49
|
+
apiKey,
|
|
50
|
+
baseUrl: baseURL,
|
|
51
|
+
// The Studio asks again by itself (it polls), so a call is made once.
|
|
52
|
+
maxRetries: 0,
|
|
53
|
+
fetch: (input, init) => {
|
|
54
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
|
|
55
|
+
headers.set('user-agent', USER_AGENT);
|
|
56
|
+
return fetch(input, { ...init, headers });
|
|
57
|
+
},
|
|
58
|
+
});
|
|
42
59
|
}
|
|
43
60
|
me() {
|
|
44
|
-
return this.
|
|
61
|
+
return call(() => this.sdk.me.get());
|
|
45
62
|
}
|
|
46
63
|
transcribeURL(url, opts) {
|
|
47
|
-
return
|
|
64
|
+
return call(() => this.sdk.transcriptions.create({ ...createOptions(opts), url }, { idempotencyKey: opts.idempotencyKey }));
|
|
48
65
|
}
|
|
49
66
|
transcribeFile(file, fileName, opts) {
|
|
50
|
-
|
|
51
|
-
form.append('options', JSON.stringify({ name: opts.name, settings: opts.spokenLanguage ? { spokenLanguage: opts.spokenLanguage } : undefined }));
|
|
52
|
-
form.append('file', file, fileName);
|
|
53
|
-
return this.request('POST', '/v1/transcriptions', form, { 'idempotency-key': opts.idempotencyKey });
|
|
67
|
+
return call(() => this.sdk.transcriptions.create({ ...createOptions(opts), file, fileName }, { idempotencyKey: opts.idempotencyKey }));
|
|
54
68
|
}
|
|
55
|
-
job(
|
|
56
|
-
return
|
|
69
|
+
job(transcriptionId) {
|
|
70
|
+
return call(() => this.sdk.transcriptions.job(transcriptionId));
|
|
57
71
|
}
|
|
58
72
|
/** Stop a job before it is done. Its job fails with `stopped`; what was transcribed so far stays in VocalStack. */
|
|
59
|
-
stop(
|
|
60
|
-
return
|
|
73
|
+
stop(transcriptionId, reason) {
|
|
74
|
+
return call(() => this.sdk.transcriptions.stop(transcriptionId, reason));
|
|
61
75
|
}
|
|
62
|
-
transcript(
|
|
63
|
-
return
|
|
76
|
+
transcript(transcriptionId) {
|
|
77
|
+
return call(() => this.sdk.transcriptions.transcript(transcriptionId));
|
|
64
78
|
}
|
|
65
79
|
}
|
|
66
80
|
/** A short label for the connected account, from whatever `/v1/me` carries. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { Box, Button, Card, Flex,
|
|
2
|
+
import { Box, Button, Card, Flex, Text, TextArea } from '@sanity/ui';
|
|
3
3
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
4
|
import { set, setIfMissing, unset, useFormValue } from 'sanity';
|
|
5
5
|
import { findAudio, jobName, POLL_MS, restored, siblingFileRefs, TRANSCRIPT_TYPE } from '../core.js';
|
|
@@ -48,7 +48,7 @@ export function TranscriptInput(props) {
|
|
|
48
48
|
const [stopping, setStopping] = useState(false);
|
|
49
49
|
const [progress, setProgress] = useState({});
|
|
50
50
|
const [error, setError] = useState();
|
|
51
|
-
// The
|
|
51
|
+
// The transcription this field was following, when the connected workspace does not have it:
|
|
52
52
|
// VOCALSTACK_API_KEY now belongs to another workspace. The field offers Transcribe again.
|
|
53
53
|
const [lost, setLost] = useState();
|
|
54
54
|
const [changing, setChanging] = useState(false);
|
|
@@ -80,17 +80,17 @@ export function TranscriptInput(props) {
|
|
|
80
80
|
setNotice('Transcript ready.');
|
|
81
81
|
}, [apply]);
|
|
82
82
|
// Follow a running job, also one started before a reload or from the document action:
|
|
83
|
-
//
|
|
83
|
+
// transcriptionId and status are saved on the document.
|
|
84
84
|
const status = value?.status ?? 'idle';
|
|
85
|
-
const
|
|
85
|
+
const transcriptionId = value?.transcriptionId;
|
|
86
86
|
useEffect(() => {
|
|
87
|
-
if (status !== 'processing' || !
|
|
87
|
+
if (status !== 'processing' || !transcriptionId)
|
|
88
88
|
return;
|
|
89
89
|
let cancelled = false;
|
|
90
90
|
let timer;
|
|
91
91
|
const tick = async () => {
|
|
92
92
|
try {
|
|
93
|
-
const outcome = await call(`/
|
|
93
|
+
const outcome = await call(`/transcriptions/${encodeURIComponent(transcriptionId)}/job`);
|
|
94
94
|
if (cancelled)
|
|
95
95
|
return;
|
|
96
96
|
setError(undefined);
|
|
@@ -105,7 +105,7 @@ export function TranscriptInput(props) {
|
|
|
105
105
|
if (cancelled)
|
|
106
106
|
return;
|
|
107
107
|
if (err instanceof HandlerError && err.code === NOT_IN_WORKSPACE)
|
|
108
|
-
setLost(
|
|
108
|
+
setLost(transcriptionId);
|
|
109
109
|
// A network blip or a server hiccup should not end the wait; anything refused should.
|
|
110
110
|
if (err instanceof HandlerError && err.status && err.status < 500)
|
|
111
111
|
return setError(err.message);
|
|
@@ -119,7 +119,7 @@ export function TranscriptInput(props) {
|
|
|
119
119
|
cancelled = true;
|
|
120
120
|
clearTimeout(timer);
|
|
121
121
|
};
|
|
122
|
-
}, [call, finish,
|
|
122
|
+
}, [call, finish, status, transcriptionId]);
|
|
123
123
|
const transcribe = async () => {
|
|
124
124
|
if (!asset)
|
|
125
125
|
return;
|
|
@@ -129,7 +129,7 @@ export function TranscriptInput(props) {
|
|
|
129
129
|
setProgress({});
|
|
130
130
|
try {
|
|
131
131
|
const body = { assetId: asset._id, documentId: document._id, fieldPath: pathString(path), name: jobName(document, asset) };
|
|
132
|
-
apply({ ...(await call('/transcriptions', { body, method: 'POST' })), error: null });
|
|
132
|
+
apply({ ...(await call('/transcriptions', { body, method: 'POST' })), error: null, recordingId: null });
|
|
133
133
|
}
|
|
134
134
|
catch (err) {
|
|
135
135
|
setError(err.message);
|
|
@@ -140,26 +140,27 @@ export function TranscriptInput(props) {
|
|
|
140
140
|
};
|
|
141
141
|
/** Stop the job. The field goes back to how it was; VocalStack keeps what it heard so far. */
|
|
142
142
|
const stop = async () => {
|
|
143
|
-
if (!
|
|
143
|
+
if (!transcriptionId)
|
|
144
144
|
return;
|
|
145
145
|
if (!window.confirm('Stop transcribing? What VocalStack has transcribed so far stays in your VocalStack dashboard.'))
|
|
146
146
|
return;
|
|
147
147
|
setStopping(true);
|
|
148
148
|
try {
|
|
149
|
-
const outcome = await call(`/
|
|
149
|
+
const outcome = await call(`/transcriptions/${encodeURIComponent(transcriptionId)}/stop`, { method: 'POST' });
|
|
150
150
|
if (outcome.kind !== 'processing')
|
|
151
151
|
finish(outcome);
|
|
152
152
|
}
|
|
153
153
|
catch (err) {
|
|
154
154
|
if (err instanceof HandlerError && err.code === NOT_IN_WORKSPACE)
|
|
155
|
-
setLost(
|
|
155
|
+
setLost(transcriptionId);
|
|
156
156
|
setError(err.message);
|
|
157
157
|
}
|
|
158
158
|
finally {
|
|
159
159
|
setStopping(false);
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
-
|
|
162
|
+
// A job saved without a transcription id cannot be followed: offer Transcribe again.
|
|
163
|
+
const isLost = status === 'processing' && (!transcriptionId || lost === transcriptionId);
|
|
163
164
|
const busy = status === 'processing' && !isLost;
|
|
164
165
|
const workspace = connection?.connected ? connection.workspace : undefined;
|
|
165
166
|
const shownError = error ?? (status === 'failed' ? (value?.error ?? undefined) : undefined);
|
|
@@ -178,7 +179,7 @@ export function TranscriptInput(props) {
|
|
|
178
179
|
else {
|
|
179
180
|
action = (_jsxs(Flex, { gap: 2, wrap: "wrap", children: [_jsx(Button, { disabled: busy || starting || readOnly || !asset, onClick: transcribe, text: busy || starting ? 'Transcribing…' : status === 'done' || isLost ? 'Transcribe again' : 'Transcribe', tone: "primary" }), busy && _jsx(Button, { disabled: stopping || readOnly, mode: "ghost", onClick: stop, text: stopping ? 'Stopping…' : 'Stop' })] }));
|
|
180
181
|
}
|
|
181
|
-
return (_jsx(Card, { border: true, padding: 3, radius: 2, children: _jsxs(
|
|
182
|
+
return (_jsx(Card, { border: true, padding: 3, radius: 2, children: _jsxs(Flex, { direction: "column", gap: 3, children: [_jsxs(Flex, { align: "center", gap: 3, justify: "space-between", wrap: "wrap", children: [_jsx(Logo, { height: 14 }), workspace && (_jsxs(Flex, { align: "center", "data-testid": "vocalstack-workspace", gap: 2, style: { minWidth: 0 }, children: [_jsx(Text, { ...muted, children: "Workspace" }), _jsx(WorkspaceMark, { size: 18, workspace: workspace }), _jsx(Text, { size: 1, textOverflow: "ellipsis", weight: "medium", children: workspace.name }), _jsx(Button, { "aria-expanded": changing, fontSize: 1, mode: "bleed", onClick: () => setChanging((c) => !c), padding: 2, text: "Change" })] }))] }), workspace && changing && (_jsx(Card, { border: true, padding: 2, radius: 2, children: _jsxs(Text, { ...muted, children: ["Transcriptions made here go to the workspace this Studio\u2019s VocalStack API key was made in. To use another workspace, set ", _jsx("code", { children: "VOCALSTACK_API_KEY" }), ' ', "where the VocalStack handler runs to a key made in that workspace: in the VocalStack Dashboard, switch to the workspace, then Developers \u2192 API keys."] }) })), action, busy && (_jsxs(Flex, { direction: "column", gap: 2, children: [_jsxs(Text, { ...muted, children: [PHASES[progress.phase ?? 'queued'] ?? progress.phase, progress.progress ? ` · ${progress.progress}%` : '…', progress.audio?.totalS ? ` · ${clock(progress.audio.processedS)} of ${clock(progress.audio.totalS)}` : ''] }), _jsx(Card, { radius: 1, style: { height: 4, overflow: 'hidden' }, tone: "transparent", children: _jsx("div", { style: { background: accent, height: '100%', transition: 'width .4s', width: `${progress.progress || 2}%` } }) })] })), notice && (_jsx(Card, { padding: 2, radius: 2, tone: "positive", children: _jsx(Text, { size: 1, children: notice }) })), !busy && status === 'done' && !shownError && (_jsxs(Text, { ...muted, children: ["Transcribed", value?.language ? ` (${value.language})` : '', value?.sentences?.length ? ` · ${value.sentences.length} sentences with timings` : '', "."] })), shownError && (_jsx(Card, { padding: 2, radius: 2, tone: "critical", children: _jsx(Text, { size: 1, children: shownError }) })), (partialText || value?.transcript) && (_jsx(Box, { children: _jsx(TextArea, { onChange: (e) => {
|
|
182
183
|
const text = e.currentTarget.value;
|
|
183
184
|
onChange([setIfMissing({ _type: TRANSCRIPT_TYPE }), text ? set(text, ['transcript']) : unset(['transcript'])]);
|
|
184
185
|
}, readOnly: readOnly || Boolean(partialText), rows: 10, value: partialText ?? value?.transcript ?? '' }) }))] }) }));
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import type { ObjectDefinition, PreviewConfig } from 'sanity';
|
|
1
2
|
import type { VocalStackPluginOptions } from './types.js';
|
|
3
|
+
import { TRANSCRIPT_TYPE } from './core.js';
|
|
4
|
+
/** Spelled out, so the published types read the same whichever Sanity major built them. */
|
|
5
|
+
export type TranscriptTypeDefinition = {
|
|
6
|
+
name: typeof TRANSCRIPT_TYPE;
|
|
7
|
+
type: 'object';
|
|
8
|
+
} & Omit<ObjectDefinition, 'preview'> & {
|
|
9
|
+
preview?: PreviewConfig<Record<string, string>, Record<never, any>>;
|
|
10
|
+
};
|
|
2
11
|
/**
|
|
3
12
|
* `vocalstack.transcript`: an object field to put next to a `file` field. Its input
|
|
4
13
|
* renders the Transcribe button, the progress and the editable transcript; the rest
|
|
5
14
|
* is written by the plugin and readable with GROQ like any field.
|
|
6
15
|
*/
|
|
7
|
-
export declare const transcriptType: (options: VocalStackPluginOptions) =>
|
|
8
|
-
type: "object";
|
|
9
|
-
name: "vocalstack.transcript";
|
|
10
|
-
} & Omit<import("sanity").ObjectDefinition, "preview"> & {
|
|
11
|
-
preview?: import("sanity").PreviewConfig<Record<string, string>, Record<never, any>> | undefined;
|
|
12
|
-
};
|
|
16
|
+
export declare const transcriptType: (options: VocalStackPluginOptions) => TranscriptTypeDefinition;
|
package/dist/schema.js
CHANGED
|
@@ -41,7 +41,7 @@ export const transcriptType = (options) => defineType({
|
|
|
41
41
|
}),
|
|
42
42
|
defineField({ name: 'language', type: 'string', readOnly: true }),
|
|
43
43
|
defineField({ name: 'error', type: 'string', readOnly: true }),
|
|
44
|
-
defineField({ name: 'recordingId', title: 'Recording ID', type: 'string', readOnly: true }),
|
|
44
|
+
defineField({ name: 'recordingId', title: 'Recording ID', type: 'string', readOnly: true, hidden: true }),
|
|
45
45
|
defineField({ name: 'transcriptionId', title: 'Transcription ID', type: 'string', readOnly: true }),
|
|
46
46
|
defineField({ name: 'transcribedAt', type: 'datetime', readOnly: true }),
|
|
47
47
|
],
|
package/dist/server/index.js
CHANGED
|
@@ -97,9 +97,9 @@ export function createVocalStackHandler(options) {
|
|
|
97
97
|
if (request.method === 'OPTIONS')
|
|
98
98
|
return new Response(null, { headers, status: 204 });
|
|
99
99
|
// The route's tail, wherever the handler is mounted: …/connection, …/transcriptions,
|
|
100
|
-
// …/
|
|
100
|
+
// …/transcriptions/:id/job, …/transcriptions/:id/stop.
|
|
101
101
|
const path = new URL(request.url).pathname.replace(/\/+$/, '');
|
|
102
|
-
const route = path.match(/\/(connection|transcriptions)$/)?.[1] ?? path.match(/\/
|
|
102
|
+
const route = path.match(/\/(connection|transcriptions)$/)?.[1] ?? path.match(/\/transcriptions\/([^/]+)\/(job|stop)$/)?.slice(1).join(' ');
|
|
103
103
|
if (!route)
|
|
104
104
|
return fail(404, 'Not a VocalStack route.');
|
|
105
105
|
try {
|
|
@@ -113,7 +113,7 @@ export function createVocalStackHandler(options) {
|
|
|
113
113
|
return fail(503, 'VOCALSTACK_API_KEY is not set where the VocalStack handler runs.', 'not_connected');
|
|
114
114
|
const apiURL = options.apiURL ?? DEFAULT_API_URL;
|
|
115
115
|
const api = new VocalStackAPI(apiKey, apiURL);
|
|
116
|
-
const
|
|
116
|
+
const jobRoute = route !== 'connection' && route !== 'transcriptions';
|
|
117
117
|
try {
|
|
118
118
|
if (route === 'connection' && request.method === 'GET') {
|
|
119
119
|
if (!connection || connection.until <= Date.now()) {
|
|
@@ -144,13 +144,13 @@ export function createVocalStackHandler(options) {
|
|
|
144
144
|
const tx = isPublicURL(url) ? await api.transcribeURL(url, job) : await api.transcribeFile(await fetchFile(url), asset.originalFilename || 'audio', job);
|
|
145
145
|
if (!tx.job)
|
|
146
146
|
return fail(502, 'VocalStack did not start a job for this file.');
|
|
147
|
-
return json({
|
|
147
|
+
return json({ status: 'processing', transcriptionId: tx.id }, 202);
|
|
148
148
|
}
|
|
149
|
-
const [
|
|
149
|
+
const [transcriptionId, action] = route.split(' ');
|
|
150
150
|
if (action === 'job' && request.method === 'GET')
|
|
151
|
-
return json(await checkJob(api, decodeURIComponent(
|
|
151
|
+
return json(await checkJob(api, decodeURIComponent(transcriptionId)));
|
|
152
152
|
if (action === 'stop' && request.method === 'POST')
|
|
153
|
-
return json(await stopJob(api, decodeURIComponent(
|
|
153
|
+
return json(await stopJob(api, decodeURIComponent(transcriptionId)));
|
|
154
154
|
return fail(405, 'Method not allowed.');
|
|
155
155
|
}
|
|
156
156
|
catch (err) {
|
|
@@ -158,8 +158,8 @@ export function createVocalStackHandler(options) {
|
|
|
158
158
|
if (err.status === 401)
|
|
159
159
|
return fail(502, 'VocalStack refused the API key in VOCALSTACK_API_KEY.', 'not_connected');
|
|
160
160
|
// A key sees only its own workspace: after VOCALSTACK_API_KEY moved to another one, a
|
|
161
|
-
//
|
|
162
|
-
if (err.status === 404 &&
|
|
161
|
+
// transcription made with the old key is not found (as is one deleted in VocalStack).
|
|
162
|
+
if (err.status === 404 && jobRoute) {
|
|
163
163
|
return fail(404, 'This transcription is not in the workspace VocalStack is connected to now: it was made in another workspace, or deleted. Transcribe the audio again to make it here.', NOT_IN_WORKSPACE);
|
|
164
164
|
}
|
|
165
165
|
return fail(err.status >= 500 ? 502 : err.status, err.message, err.code);
|
package/dist/server/jobs.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ import { type Transcript, type VocalStackAPI } from '../api.js';
|
|
|
5
5
|
export declare function toSentences(t: Pick<Transcript, 'sentences'>): TranscriptSentence[];
|
|
6
6
|
/** As stored on the document: an array item needs a `_key`. */
|
|
7
7
|
export declare const toSaved: (sentences: TranscriptSentence[]) => SavedSentence[];
|
|
8
|
-
export declare function checkJob(api: VocalStackAPI,
|
|
8
|
+
export declare function checkJob(api: VocalStackAPI, transcriptionId: string): Promise<JobOutcome>;
|
|
9
9
|
/** Stop a job. If it ended meanwhile, take its outcome instead. */
|
|
10
|
-
export declare function stopJob(api: VocalStackAPI,
|
|
10
|
+
export declare function stopJob(api: VocalStackAPI, transcriptionId: string): Promise<JobOutcome>;
|
package/dist/server/jobs.js
CHANGED
|
@@ -9,8 +9,8 @@ export function toSentences(t) {
|
|
|
9
9
|
const round = (n) => Math.round(n * 1000) / 1000;
|
|
10
10
|
/** As stored on the document: an array item needs a `_key`. */
|
|
11
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,
|
|
13
|
-
const job = await api.job(
|
|
12
|
+
export async function checkJob(api, transcriptionId) {
|
|
13
|
+
const job = await api.job(transcriptionId);
|
|
14
14
|
if (job.state === 'stopped' || (job.state === 'failed' && job.errorCode === 'stopped'))
|
|
15
15
|
return { kind: 'stopped' };
|
|
16
16
|
if (job.state === 'failed' || job.state === 'cropping') {
|
|
@@ -22,10 +22,10 @@ export async function checkJob(api, recordingId) {
|
|
|
22
22
|
if (job.state !== 'transcribing')
|
|
23
23
|
return { kind: 'processing', phase: job.progress.phase ?? job.state, progress };
|
|
24
24
|
// Sentences are readable while the job runs; send what is there so far.
|
|
25
|
-
const partial = await api.transcript(
|
|
25
|
+
const partial = await api.transcript(transcriptionId).then(toSentences, () => undefined);
|
|
26
26
|
return { audio: { processedS: job.progress.processedS ?? 0, totalS: job.progress.totalS ?? undefined }, kind: 'processing', partial, phase: 'transcribing', progress };
|
|
27
27
|
}
|
|
28
|
-
const transcript = await api.transcript(
|
|
28
|
+
const transcript = await api.transcript(transcriptionId);
|
|
29
29
|
const sentences = toSentences(transcript);
|
|
30
30
|
return {
|
|
31
31
|
kind: 'done',
|
|
@@ -40,14 +40,14 @@ export async function checkJob(api, recordingId) {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
/** Stop a job. If it ended meanwhile, take its outcome instead. */
|
|
43
|
-
export async function stopJob(api,
|
|
43
|
+
export async function stopJob(api, transcriptionId) {
|
|
44
44
|
try {
|
|
45
|
-
await api.stop(
|
|
45
|
+
await api.stop(transcriptionId, 'Stopped from Sanity');
|
|
46
46
|
return { kind: 'stopped' };
|
|
47
47
|
}
|
|
48
48
|
catch (err) {
|
|
49
49
|
if (err instanceof VocalStackAPIError && err.status === 409)
|
|
50
|
-
return checkJob(api,
|
|
50
|
+
return checkJob(api, transcriptionId).catch(() => ({ kind: 'stopped' }));
|
|
51
51
|
throw err;
|
|
52
52
|
}
|
|
53
53
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ export type VocalStackTranscript = {
|
|
|
33
33
|
_type?: 'vocalstack.transcript';
|
|
34
34
|
error?: null | string;
|
|
35
35
|
language?: null | string;
|
|
36
|
+
/** Written by versions before 0.3.0; no longer read (VocalStack's API has no recordings). */
|
|
36
37
|
recordingId?: null | string;
|
|
37
38
|
sentences?: null | SavedSentence[];
|
|
38
39
|
status?: null | TranscriptStatus;
|
package/dist/wire.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export type StartRequest = {
|
|
|
32
32
|
/** The transcription's name in VocalStack. Default: the file's name. */
|
|
33
33
|
name?: string;
|
|
34
34
|
};
|
|
35
|
-
export type StartResponse = Required<Pick<VocalStackTranscript, '
|
|
35
|
+
export type StartResponse = Required<Pick<VocalStackTranscript, 'status' | 'transcriptionId'>>;
|
|
36
36
|
/** `GET {handlerURL}/connection` */
|
|
37
37
|
export type ConnectionResponse = {
|
|
38
38
|
/** The workspace's name; against an older VocalStack, the key's label. */
|
|
@@ -51,7 +51,7 @@ export type ConnectionWorkspace = {
|
|
|
51
51
|
name: string;
|
|
52
52
|
personal: boolean;
|
|
53
53
|
};
|
|
54
|
-
/** The handler's `code` when a
|
|
54
|
+
/** The handler's `code` when a transcription is not in the workspace its key belongs to. */
|
|
55
55
|
export declare const NOT_IN_WORKSPACE = "not_in_workspace";
|
|
56
56
|
/** Every error: `{ code?, error }`. `session` means the Studio should prove itself again. */
|
|
57
57
|
export type ErrorResponse = {
|
package/dist/wire.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** The handler's `code` when a
|
|
1
|
+
/** The handler's `code` when a transcription is not in the workspace its key belongs to. */
|
|
2
2
|
export const NOT_IN_WORKSPACE = 'not_in_workspace';
|
|
3
3
|
/** The document type of a Studio session (see `SESSION_PREFIX`). */
|
|
4
4
|
export const SESSION_TYPE = 'vocalstack.session';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vocalstack/sanity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "VocalStack for Sanity Studio: a Transcribe button next to every audio file.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@sanity/mutator": "
|
|
40
|
-
"@sanity/ui": "^
|
|
41
|
-
"@types/react": "^19.
|
|
42
|
-
"groq-js": "
|
|
43
|
-
"mendoza": "
|
|
44
|
-
"playwright-core": "1.
|
|
45
|
-
"react": "^19.1
|
|
46
|
-
"react-dom": "^19.1
|
|
47
|
-
"sanity": "
|
|
48
|
-
"styled-components": "^6.
|
|
39
|
+
"@sanity/mutator": "6.25.0",
|
|
40
|
+
"@sanity/ui": "^4.4.14",
|
|
41
|
+
"@types/react": "^19.3.3",
|
|
42
|
+
"groq-js": "2.0.4",
|
|
43
|
+
"mendoza": "6.0.0",
|
|
44
|
+
"playwright-core": "1.65.1",
|
|
45
|
+
"react": "^19.3.1",
|
|
46
|
+
"react-dom": "^19.3.1",
|
|
47
|
+
"sanity": "6.25.0",
|
|
48
|
+
"styled-components": "^6.7.0",
|
|
49
49
|
"typescript": "^5.6.0"
|
|
50
50
|
},
|
|
51
51
|
"repository": {
|
|
@@ -54,5 +54,8 @@
|
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=20"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@vocalstack/js-sdk": "2.3.1"
|
|
57
60
|
}
|
|
58
61
|
}
|