@vocalstack/sanity 0.2.0 → 0.3.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 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 `recordingId` 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.
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` and `transcribedAt`.
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 …/recordings/:id/job` and `POST …/recordings/:id/stop`.
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
 
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}.recordingId`]: fields.recordingId, [`${at}.transcriptionId`]: fields.transcriptionId } },
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
@@ -7,7 +7,7 @@ export declare const DEFAULT_API_URL = "https://api.vocalstack.com";
7
7
  /** Sent as the User-Agent of every call to VocalStack (they run in the
8
8
  * handler, on the server): VocalStack lists the transcriptions it makes
9
9
  * as made in Sanity. Keep it to package.json's version. */
10
- export declare const PLUGIN_VERSION = "0.2.0";
10
+ export declare const PLUGIN_VERSION = "0.3.0";
11
11
  export declare const DEFAULT_DASHBOARD_URL = "https://app.vocalstack.com";
12
12
  export type JobState = 'cropping' | 'done' | 'failed' | 'fetching' | 'queued' | 'stopped' | 'transcribing';
13
13
  export type Job = {
@@ -21,7 +21,6 @@ export type Job = {
21
21
  processedS?: number;
22
22
  totalS?: null | number;
23
23
  };
24
- recordingId: string;
25
24
  state: JobState;
26
25
  transcriptionId: string;
27
26
  };
@@ -74,10 +73,10 @@ export declare class VocalStackAPI {
74
73
  name: string;
75
74
  spokenLanguage?: string;
76
75
  }): Promise<Transcription>;
77
- job(recordingId: string): Promise<Job>;
76
+ job(transcriptionId: string): Promise<Job>;
78
77
  /** Stop a job before it is done. Its job fails with `stopped`; what was transcribed so far stays in VocalStack. */
79
- stop(recordingId: string, reason?: string): Promise<unknown>;
80
- transcript(recordingId: string): Promise<Transcript>;
78
+ stop(transcriptionId: string, reason?: string): Promise<unknown>;
79
+ transcript(transcriptionId: string): Promise<Transcript>;
81
80
  }
82
81
  /** A short label for the connected account, from whatever `/v1/me` carries. */
83
82
  export declare function accountLabel(me: Record<string, unknown>): string | undefined;
package/dist/api.js CHANGED
@@ -6,7 +6,7 @@ export const DEFAULT_API_URL = 'https://api.vocalstack.com';
6
6
  /** Sent as the User-Agent of every call to VocalStack (they run in the
7
7
  * handler, on the server): VocalStack lists the transcriptions it makes
8
8
  * as made in Sanity. Keep it to package.json's version. */
9
- export const PLUGIN_VERSION = '0.2.0';
9
+ export const PLUGIN_VERSION = '0.3.0';
10
10
  export const DEFAULT_DASHBOARD_URL = 'https://app.vocalstack.com';
11
11
  export class VocalStackAPIError extends Error {
12
12
  code;
@@ -52,15 +52,15 @@ export class VocalStackAPI {
52
52
  form.append('file', file, fileName);
53
53
  return this.request('POST', '/v1/transcriptions', form, { 'idempotency-key': opts.idempotencyKey });
54
54
  }
55
- job(recordingId) {
56
- return this.request('GET', `/v1/recordings/${encodeURIComponent(recordingId)}/job`);
55
+ job(transcriptionId) {
56
+ return this.request('GET', `/v1/transcriptions/${encodeURIComponent(transcriptionId)}/job`);
57
57
  }
58
58
  /** Stop a job before it is done. Its job fails with `stopped`; what was transcribed so far stays in VocalStack. */
59
- stop(recordingId, reason) {
60
- return this.request('POST', `/v1/recordings/${encodeURIComponent(recordingId)}/stop`, { reason: reason ?? null });
59
+ stop(transcriptionId, reason) {
60
+ return this.request('POST', `/v1/transcriptions/${encodeURIComponent(transcriptionId)}/stop`, { reason: reason ?? null });
61
61
  }
62
- transcript(recordingId) {
63
- return this.request('GET', `/v1/recordings/${encodeURIComponent(recordingId)}/transcript`);
62
+ transcript(transcriptionId) {
63
+ return this.request('GET', `/v1/transcriptions/${encodeURIComponent(transcriptionId)}/transcript`);
64
64
  }
65
65
  }
66
66
  /** A short label for the connected account, from whatever `/v1/me` carries. */
@@ -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 recording this field was following, when the connected workspace does not have it:
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
- // recordingId and status are saved on the document.
83
+ // transcriptionId and status are saved on the document.
84
84
  const status = value?.status ?? 'idle';
85
- const recordingId = value?.recordingId;
85
+ const transcriptionId = value?.transcriptionId;
86
86
  useEffect(() => {
87
- if (status !== 'processing' || !recordingId)
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(`/recordings/${encodeURIComponent(recordingId)}/job`);
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(recordingId);
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, recordingId, status]);
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 (!recordingId)
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(`/recordings/${encodeURIComponent(recordingId)}/stop`, { method: 'POST' });
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(recordingId);
155
+ setLost(transcriptionId);
156
156
  setError(err.message);
157
157
  }
158
158
  finally {
159
159
  setStopping(false);
160
160
  }
161
161
  };
162
- const isLost = status === 'processing' && Boolean(recordingId) && lost === recordingId;
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);
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
  ],
@@ -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
- // …/recordings/:id/job, …/recordings/:id/stop.
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(/\/recordings\/([^/]+)\/(job|stop)$/)?.slice(1).join(' ');
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 recordingRoute = route !== 'connection' && route !== 'transcriptions';
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({ recordingId: tx.job.recordingId, status: 'processing', transcriptionId: tx.id }, 202);
147
+ return json({ status: 'processing', transcriptionId: tx.id }, 202);
148
148
  }
149
- const [recordingId, action] = route.split(' ');
149
+ const [transcriptionId, action] = route.split(' ');
150
150
  if (action === 'job' && request.method === 'GET')
151
- return json(await checkJob(api, decodeURIComponent(recordingId)));
151
+ return json(await checkJob(api, decodeURIComponent(transcriptionId)));
152
152
  if (action === 'stop' && request.method === 'POST')
153
- return json(await stopJob(api, decodeURIComponent(recordingId)));
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
- // recording made with the old key is not found (as is one deleted in VocalStack).
162
- if (err.status === 404 && recordingRoute) {
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);
@@ -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, recordingId: string): Promise<JobOutcome>;
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, recordingId: string): Promise<JobOutcome>;
10
+ export declare function stopJob(api: VocalStackAPI, transcriptionId: string): Promise<JobOutcome>;
@@ -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, recordingId) {
13
- const job = await api.job(recordingId);
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(recordingId).then(toSentences, () => undefined);
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(recordingId);
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, recordingId) {
43
+ export async function stopJob(api, transcriptionId) {
44
44
  try {
45
- await api.stop(recordingId, 'Stopped from Sanity');
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, recordingId).catch(() => ({ kind: 'stopped' }));
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, 'recordingId' | 'status' | 'transcriptionId'>>;
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 recording is not in the workspace its key belongs to. */
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 recording is not in the workspace its key belongs to. */
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.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "VocalStack for Sanity Studio: a Transcribe button next to every audio file.",
5
5
  "license": "MIT",
6
6
  "type": "module",