@vocalstack/payload 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.
@@ -0,0 +1,20 @@
1
+ import type { GlobalConfig, PayloadRequest } from 'payload';
2
+ import type { VocalStackPluginOptions } from './types.js';
3
+ export declare const SETTINGS_SLUG = "vocalstack";
4
+ /**
5
+ * Where a key connected from the admin lives. Nothing here is readable or
6
+ * writable over the REST/GraphQL APIs: only the plugin's endpoints touch it
7
+ * (with `overrideAccess`), so the key never reaches a browser.
8
+ */
9
+ export declare const settingsGlobal: (options: VocalStackPluginOptions) => GlobalConfig;
10
+ export type StoredSettings = {
11
+ account?: null | string;
12
+ apiKey?: null | string;
13
+ connectExpiresAt?: null | string;
14
+ connectState?: null | string;
15
+ connectVerifier?: null | string;
16
+ };
17
+ export declare function readSettings(req: PayloadRequest): Promise<StoredSettings>;
18
+ export declare function writeSettings(req: PayloadRequest, data: StoredSettings): Promise<void>;
19
+ /** The key to call VocalStack with: the plugin option first, then a connected one. */
20
+ export declare function resolveAPIKey(req: PayloadRequest, options: VocalStackPluginOptions): Promise<string | undefined>;
@@ -0,0 +1,47 @@
1
+ export const SETTINGS_SLUG = 'vocalstack';
2
+ const hidden = { admin: { hidden: true }, access: { read: () => false, update: () => false } };
3
+ /**
4
+ * Where a key connected from the admin lives. Nothing here is readable or
5
+ * writable over the REST/GraphQL APIs: only the plugin's endpoints touch it
6
+ * (with `overrideAccess`), so the key never reaches a browser.
7
+ */
8
+ export const settingsGlobal = (options) => ({
9
+ slug: SETTINGS_SLUG,
10
+ label: 'VocalStack',
11
+ admin: {
12
+ group: 'Settings',
13
+ },
14
+ access: {
15
+ read: ({ req }) => Boolean(req.user),
16
+ update: () => false,
17
+ },
18
+ fields: [
19
+ {
20
+ name: 'connection',
21
+ type: 'ui',
22
+ admin: {
23
+ components: {
24
+ Field: { clientProps: { oneClickConnect: Boolean(options.oneClickConnect) }, path: '@vocalstack/payload/client#ConnectionPanel' },
25
+ },
26
+ },
27
+ },
28
+ { name: 'apiKey', type: 'text', ...hidden },
29
+ { name: 'account', type: 'text', ...hidden },
30
+ // A pending one-click connect: PKCE verifier and state, for ten minutes.
31
+ { name: 'connectState', type: 'text', ...hidden },
32
+ { name: 'connectVerifier', type: 'text', ...hidden },
33
+ { name: 'connectExpiresAt', type: 'date', ...hidden },
34
+ ],
35
+ });
36
+ export async function readSettings(req) {
37
+ return (await req.payload.findGlobal({ slug: SETTINGS_SLUG, overrideAccess: true, req }));
38
+ }
39
+ export async function writeSettings(req, data) {
40
+ await req.payload.updateGlobal({ slug: SETTINGS_SLUG, data, overrideAccess: true, req });
41
+ }
42
+ /** The key to call VocalStack with: the plugin option first, then a connected one. */
43
+ export async function resolveAPIKey(req, options) {
44
+ if (options.apiKey)
45
+ return options.apiKey;
46
+ return (await readSettings(req)).apiKey ?? undefined;
47
+ }
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
+ }
@@ -0,0 +1,123 @@
1
+ import type { CollectionSlug, PayloadRequest, TypeWithID } from 'payload';
2
+ export type VocalStackPluginOptions = {
3
+ /** Turn the plugin off without removing it (fields stay in the schema). */
4
+ enabled?: boolean;
5
+ /**
6
+ * A VocalStack API key (`vs_key_…`, Read + Write). When set it wins over a key
7
+ * connected from the admin panel; leave it out to connect from the admin instead.
8
+ */
9
+ apiKey?: string;
10
+ /**
11
+ * Upload collections that get the Transcribe button. Defaults to every upload
12
+ * collection that accepts audio (or has no MIME type restriction). The button
13
+ * only appears on documents whose `mimeType` is `audio/*`.
14
+ */
15
+ collections?: CollectionSlug[];
16
+ /**
17
+ * Audio that is not an upload document: rows of an array field that each point
18
+ * at a file by address (a filename, an imported link). Every row gets its own
19
+ * Transcribe button and transcript.
20
+ */
21
+ audioFields?: AudioFieldConfig[];
22
+ /** Default https://api.vocalstack.com */
23
+ apiURL?: string;
24
+ /** Default https://app.vocalstack.com */
25
+ dashboardURL?: string;
26
+ /**
27
+ * Show "Connect VocalStack" (opens the dashboard in a new tab; one click on
28
+ * Authenticate there). Needs the dashboard's /connect flow; until then, paste a key.
29
+ */
30
+ oneClickConnect?: boolean;
31
+ /** A Whisper language code (`en`, `ro`, …). Leave out to auto-detect. */
32
+ spokenLanguage?: string;
33
+ /**
34
+ * The public URL VocalStack fetches the audio from. Defaults to the document's
35
+ * `url`, made absolute against `serverURL` or the request's origin. When that
36
+ * address is not public (localhost, a private network), the plugin uploads the
37
+ * file's bytes instead.
38
+ */
39
+ fileURL?: (args: {
40
+ collection: CollectionSlug;
41
+ doc: UploadDoc;
42
+ req: PayloadRequest;
43
+ }) => Promise<string | undefined> | string | undefined;
44
+ /** Who may start a transcription. Default: any signed-in user who can update the document. */
45
+ access?: (args: {
46
+ req: PayloadRequest;
47
+ }) => boolean | Promise<boolean>;
48
+ };
49
+ export type ArrayRow = Record<string, unknown> & {
50
+ id?: null | string;
51
+ vocalstack?: null | VocalStackFieldData;
52
+ };
53
+ export type AudioFieldConfig = {
54
+ collection: CollectionSlug;
55
+ /** An array field at the top level of the collection (or inside tabs, rows or collapsibles). */
56
+ field: string;
57
+ /** The row's audio address, or nothing when the row has no audio. Relative addresses resolve against the server URL. */
58
+ audioURL: (args: {
59
+ doc: Record<string, unknown>;
60
+ req: PayloadRequest;
61
+ row: ArrayRow;
62
+ }) => Promise<string | undefined> | string | undefined;
63
+ /** Show the button and transcript only on rows this accepts (admin condition). Default: every row. */
64
+ condition?: (row: ArrayRow) => boolean;
65
+ /** The transcription's name in VocalStack. Default: the document's title, else collection and id. */
66
+ name?: (args: {
67
+ doc: Record<string, unknown>;
68
+ row: ArrayRow;
69
+ }) => string;
70
+ };
71
+ export type UploadDoc = TypeWithID & {
72
+ filename?: null | string;
73
+ mimeType?: null | string;
74
+ url?: null | string;
75
+ vocalstack?: null | VocalStackFieldData;
76
+ };
77
+ export type TranscriptStatus = 'done' | 'failed' | 'idle' | 'processing';
78
+ export type VocalStackFieldData = {
79
+ error?: null | string;
80
+ language?: null | string;
81
+ recordingId?: null | string;
82
+ sentences?: null | TranscriptSentence[];
83
+ status?: null | TranscriptStatus;
84
+ transcript?: null | string;
85
+ transcriptionId?: null | string;
86
+ transcribedAt?: null | string;
87
+ };
88
+ /** [start, end, text], seconds. */
89
+ export type TranscriptWord = [number, number, string];
90
+ export type TranscriptSentence = {
91
+ /** Live only: words heard after the last settled sentence. */
92
+ draft?: boolean;
93
+ end: number;
94
+ /** VocalStack's sentenceId. */
95
+ id?: number;
96
+ speaker: number;
97
+ start: number;
98
+ text: string;
99
+ words?: TranscriptWord[];
100
+ };
101
+ /** What the status endpoint answers, and what the Transcribe button renders. */
102
+ export type StatusResponse = {
103
+ /** Seconds of audio transcribed so far, and in all (while transcribing). */
104
+ audio?: {
105
+ processedS: number;
106
+ totalS?: number;
107
+ };
108
+ error?: string;
109
+ /** The sentences so far, while the job runs (the polling fallback). */
110
+ partial?: TranscriptSentence[];
111
+ phase?: string;
112
+ progress?: number;
113
+ status: TranscriptStatus;
114
+ /** The job was stopped before it finished; the field is back to how it was before. */
115
+ stopped?: boolean;
116
+ vocalstack?: VocalStackFieldData;
117
+ };
118
+ export type ConnectionResponse = {
119
+ account?: string;
120
+ connected: boolean;
121
+ /** `options`: the key is in the plugin config; `admin`: connected from the admin panel. */
122
+ source?: 'admin' | 'options';
123
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@vocalstack/payload",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "VocalStack for Payload CMS: a Transcribe button on every audio upload.",
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
+ "./client": {
15
+ "types": "./dist/exports/client.d.ts",
16
+ "default": "./dist/exports/client.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.json",
24
+ "typecheck": "tsc -p tsconfig.json --noEmit"
25
+ },
26
+ "peerDependencies": {
27
+ "@payloadcms/ui": "^3.0.0",
28
+ "next": ">=15",
29
+ "payload": "^3.0.0",
30
+ "react": "^19.0.0"
31
+ },
32
+ "devDependencies": {
33
+ "@payloadcms/ui": "3.80.0",
34
+ "@types/node": "^22.0.0",
35
+ "@types/react": "^19.0.0",
36
+ "next": "16.2.1",
37
+ "payload": "3.80.0",
38
+ "react": "19.2.4",
39
+ "react-dom": "19.2.4",
40
+ "typescript": "^5.6.0"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+ssh://git@github.com/vocalstack/payload-plugin.git"
45
+ },
46
+ "engines": {
47
+ "node": ">=20"
48
+ }
49
+ }