@conversation/spec 0.0.0-stage → 0.1.2
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 +22 -0
- package/README.md +132 -2
- package/errors.d.ts +9 -0
- package/errors.js +36 -0
- package/esm/errors.js +31 -0
- package/esm/include.js +15 -0
- package/esm/index.js +7 -0
- package/esm/parse.js +469 -0
- package/esm/plugin.js +50 -0
- package/esm/plugins/effects.js +17 -0
- package/esm/plugins/index.js +10 -0
- package/esm/plugins/slides.js +48 -0
- package/esm/plugins/speakers.js +52 -0
- package/esm/plugins/video.js +39 -0
- package/esm/schema.js +100 -0
- package/esm/segments.js +60 -0
- package/esm/suggest.js +51 -0
- package/esm/types.js +1 -0
- package/esm/values.js +228 -0
- package/include.d.ts +23 -0
- package/include.js +18 -0
- package/index.d.ts +8 -0
- package/index.js +48 -0
- package/package.json +42 -4
- package/parse.d.ts +15 -0
- package/parse.js +472 -0
- package/plugin.d.ts +52 -0
- package/plugin.js +54 -0
- package/plugins/effects.d.ts +7 -0
- package/plugins/effects.js +20 -0
- package/plugins/index.d.ts +7 -0
- package/plugins/index.js +18 -0
- package/plugins/slides.d.ts +6 -0
- package/plugins/slides.js +51 -0
- package/plugins/speakers.d.ts +9 -0
- package/plugins/speakers.js +56 -0
- package/plugins/video.d.ts +6 -0
- package/plugins/video.js +42 -0
- package/schema.d.ts +56 -0
- package/schema.js +113 -0
- package/segments.d.ts +9 -0
- package/segments.js +63 -0
- package/suggest.d.ts +7 -0
- package/suggest.js +56 -0
- package/types.d.ts +93 -0
- package/types.js +2 -0
- package/values.d.ts +36 -0
- package/values.js +236 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `[video CLIP]` / `[video CLIP start:2, for:4]` — a clip takes the screen and the
|
|
3
|
+
* scene waits for it. `video` is a built-in track; the sound must be a `file:`.
|
|
4
|
+
*/
|
|
5
|
+
export function video() {
|
|
6
|
+
const allowed = ['hold', 'start', 'for', 'volume'];
|
|
7
|
+
return {
|
|
8
|
+
name: 'video',
|
|
9
|
+
tracks: ['video'],
|
|
10
|
+
trackRules: {
|
|
11
|
+
video(item, ctx) {
|
|
12
|
+
const ov = item.overrides;
|
|
13
|
+
const bad = Object.keys(ov).filter((k) => !allowed.includes(k));
|
|
14
|
+
if (item.action !== 'play' || !item.sound) {
|
|
15
|
+
ctx.error('`[video …]` plays one clip, e.g. `[video COVER]` — it can\'t be stopped or set; a clip ' +
|
|
16
|
+
'plays its length (trim with `start:`/`for:`)');
|
|
17
|
+
}
|
|
18
|
+
else if (ov.at !== undefined) {
|
|
19
|
+
ctx.error('a video takes the screen, so it can\'t overlap anything — drop `at:`');
|
|
20
|
+
}
|
|
21
|
+
else if (bad.length) {
|
|
22
|
+
ctx.error(`\`${bad.join(', ')}\` doesn't apply to a video clip — it takes \`start:\`, \`for:\` and \`vol:\``);
|
|
23
|
+
}
|
|
24
|
+
ov.hold = true;
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
finalize(scene, ctx) {
|
|
28
|
+
for (const item of scene.items) {
|
|
29
|
+
if (item.type !== 'track' || item.track !== 'video' || !item.sound)
|
|
30
|
+
continue;
|
|
31
|
+
const sound = scene.sounds[item.sound];
|
|
32
|
+
if (sound && !sound.file) {
|
|
33
|
+
ctx.problem(item, `\`${item.sound}\` is generated (\`prompt:\`), but \`[video …]\` needs a video file: ` +
|
|
34
|
+
`\`${item.sound}: { file: path/to/clip.mp4 }\``);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
package/esm/schema.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keys and the values they take, per context. A plugin adds keys to any
|
|
3
|
+
* context; the parser validates and coerces every key against the result.
|
|
4
|
+
*/
|
|
5
|
+
export const KEY_CONTEXTS = [
|
|
6
|
+
'character',
|
|
7
|
+
'line',
|
|
8
|
+
'span',
|
|
9
|
+
'sound',
|
|
10
|
+
'track',
|
|
11
|
+
'play',
|
|
12
|
+
'off',
|
|
13
|
+
'set',
|
|
14
|
+
];
|
|
15
|
+
export const str = () => ({ kind: 'string' });
|
|
16
|
+
export const int = () => ({ kind: 'int' });
|
|
17
|
+
export const num = (min, max) => ({ kind: 'number', min, max });
|
|
18
|
+
export const bool = () => ({ kind: 'bool' });
|
|
19
|
+
export const time = () => ({ kind: 'time' });
|
|
20
|
+
export const oneOf = (...values) => ({ kind: 'enum', values });
|
|
21
|
+
export const named = (name) => ({ kind: 'enum', values: { named: name } });
|
|
22
|
+
export const track = () => ({ kind: 'track' });
|
|
23
|
+
export const tracks = () => ({ kind: 'tracks' });
|
|
24
|
+
/** The core grammar's keys. Everything else comes from plugins. */
|
|
25
|
+
export const CORE_SCHEMAS = {
|
|
26
|
+
character: {
|
|
27
|
+
persona: str(),
|
|
28
|
+
voice: str(),
|
|
29
|
+
speaker: str(),
|
|
30
|
+
emotion: str(),
|
|
31
|
+
track: track(),
|
|
32
|
+
seed: int(),
|
|
33
|
+
gain: num(-60, 24),
|
|
34
|
+
},
|
|
35
|
+
line: { emotion: str(), seed: int(), track: track(), gain: num(-60, 24) },
|
|
36
|
+
span: { emotion: str(), seed: int(), gain: num(-60, 24) },
|
|
37
|
+
sound: {
|
|
38
|
+
prompt: str(),
|
|
39
|
+
file: str(),
|
|
40
|
+
duration: num(0.05, 3600),
|
|
41
|
+
seed: int(),
|
|
42
|
+
loop: bool(),
|
|
43
|
+
level: num(-80, 0),
|
|
44
|
+
start: time(),
|
|
45
|
+
layer: track(),
|
|
46
|
+
track: track(),
|
|
47
|
+
},
|
|
48
|
+
track: {
|
|
49
|
+
kind: oneOf('bed', 'oneshot'),
|
|
50
|
+
level: num(-80, 0),
|
|
51
|
+
volume: num(0, 4),
|
|
52
|
+
gain: num(-60, 24),
|
|
53
|
+
pan: num(-1, 1),
|
|
54
|
+
duck: num(-60, 0),
|
|
55
|
+
duck_under: tracks(),
|
|
56
|
+
fade_in: num(0, 60),
|
|
57
|
+
fade_out: num(0, 60),
|
|
58
|
+
xfade: num(0, 30),
|
|
59
|
+
tail: num(0, 60),
|
|
60
|
+
loop: bool(),
|
|
61
|
+
exclusive: bool(),
|
|
62
|
+
},
|
|
63
|
+
play: {
|
|
64
|
+
at: num(-3600, 36000),
|
|
65
|
+
volume: num(0, 4),
|
|
66
|
+
pan: num(-1, 1),
|
|
67
|
+
fade_in: num(0, 60),
|
|
68
|
+
fade_out: num(0, 60),
|
|
69
|
+
duration: num(0.05, 3600),
|
|
70
|
+
seed: int(),
|
|
71
|
+
start: time(),
|
|
72
|
+
for: time(),
|
|
73
|
+
hold: bool(),
|
|
74
|
+
loop: bool(),
|
|
75
|
+
noloop: bool(),
|
|
76
|
+
level: num(-80, 0),
|
|
77
|
+
xfade: num(0, 30),
|
|
78
|
+
},
|
|
79
|
+
off: { fade_out: num(0, 60) },
|
|
80
|
+
set: { volume: num(0, 4), ramp: num(0, 600), at: num(-3600, 36000) },
|
|
81
|
+
};
|
|
82
|
+
/** Tracks every scene has without declaring them. */
|
|
83
|
+
export const CORE_TRACKS = ['dialogue', 'foreground', 'ambience', 'music', 'transition'];
|
|
84
|
+
/** Lowercase words that can never name a track. */
|
|
85
|
+
export const CORE_RESERVED = ['pause', 'off', 'track'];
|
|
86
|
+
/** Other spellings an author may use, mapped to the canonical key. */
|
|
87
|
+
export const ALIASES = {
|
|
88
|
+
vol: 'volume',
|
|
89
|
+
fadein: 'fade_in',
|
|
90
|
+
fadeout: 'fade_out',
|
|
91
|
+
dur: 'duration',
|
|
92
|
+
from: 'start',
|
|
93
|
+
length: 'for',
|
|
94
|
+
};
|
|
95
|
+
/** How a canonical key is shown back to an author. */
|
|
96
|
+
export function shown(key) {
|
|
97
|
+
if (key === 'volume')
|
|
98
|
+
return 'vol';
|
|
99
|
+
return key.replace(/_/g, '-');
|
|
100
|
+
}
|
package/esm/segments.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markers inside one line of dialogue:
|
|
3
|
+
*
|
|
4
|
+
* DAN: calm about it <break 0.5> and then it hit me.
|
|
5
|
+
* DAN: I hear you, {excited: but this changes everything!}
|
|
6
|
+
* DAN: {whisper, reverb:cathedral: come here} <sfx DOOR> {firm: we need to talk}.
|
|
7
|
+
*/
|
|
8
|
+
// A span's prefix is a list of `key` / `key:value` tokens ended by ONE colon
|
|
9
|
+
// before the text. It is matched token by token: `reverb:cathedral` is itself
|
|
10
|
+
// a key:value, so "everything up to the first colon" would cut it in half.
|
|
11
|
+
const TOKEN = String.raw `[a-zA-Z_][\w.-]*(?::[a-zA-Z0-9_.+-]+)?`;
|
|
12
|
+
const PREFIX = String.raw `(?:${TOKEN}\s*,\s*)*${TOKEN}`;
|
|
13
|
+
const INLINE = String.raw `<break\s+([\d.]+)>|<([\d.]+)>|<sfx\s+([A-Z][A-Z0-9_-]*)(?:\s+([^>]*))?>|\{(${PREFIX}):([^{}]*)\}`;
|
|
14
|
+
export function splitSegments(text) {
|
|
15
|
+
const re = new RegExp(INLINE, 'g');
|
|
16
|
+
const out = [];
|
|
17
|
+
let pos = 0;
|
|
18
|
+
let m;
|
|
19
|
+
let any = false;
|
|
20
|
+
while ((m = re.exec(text))) {
|
|
21
|
+
any = true;
|
|
22
|
+
const before = text.slice(pos, m.index);
|
|
23
|
+
if (before.trim())
|
|
24
|
+
out.push({ type: 'text', text: before.trim() });
|
|
25
|
+
const [, breakA, breakB, sfx, sfxArgs, spanKeys, spanText] = m;
|
|
26
|
+
if (breakA !== undefined || breakB !== undefined) {
|
|
27
|
+
out.push({ type: 'break', seconds: Number(breakA ?? breakB) });
|
|
28
|
+
}
|
|
29
|
+
else if (sfx !== undefined) {
|
|
30
|
+
out.push({ type: 'sfx', sound: sfx, raw: sfxArgs ?? '' });
|
|
31
|
+
}
|
|
32
|
+
else if (spanText.trim()) {
|
|
33
|
+
out.push({ type: 'text', text: spanText.trim(), raw: spanKeys });
|
|
34
|
+
}
|
|
35
|
+
pos = m.index + m[0].length;
|
|
36
|
+
}
|
|
37
|
+
if (!any)
|
|
38
|
+
return [{ type: 'text', text }];
|
|
39
|
+
const rest = text.slice(pos);
|
|
40
|
+
if (rest.trim())
|
|
41
|
+
out.push({ type: 'text', text: rest.trim() });
|
|
42
|
+
const folded = foldPunctuation(out);
|
|
43
|
+
return folded.length ? folded : [{ type: 'text', text }];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A fragment of bare punctuation beside a span — the "." in `{firm: talk}.` —
|
|
47
|
+
* joins the text before it rather than being spoken on its own.
|
|
48
|
+
*/
|
|
49
|
+
function foldPunctuation(segments) {
|
|
50
|
+
const out = [];
|
|
51
|
+
for (const seg of segments) {
|
|
52
|
+
const prev = out[out.length - 1];
|
|
53
|
+
if (seg.type === 'text' && !/[\p{L}\p{N}]/u.test(seg.text) && prev?.type === 'text') {
|
|
54
|
+
out[out.length - 1] = { ...prev, text: prev.text + seg.text };
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
out.push(seg);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
package/esm/suggest.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Did you mean" — closest option by Ratcliff/Obershelp similarity, the same
|
|
3
|
+
* measure as Python's difflib, so suggestions match across implementations.
|
|
4
|
+
*/
|
|
5
|
+
function matching(a, b) {
|
|
6
|
+
if (!a.length || !b.length)
|
|
7
|
+
return 0;
|
|
8
|
+
let best = 0;
|
|
9
|
+
let ai = 0;
|
|
10
|
+
let bi = 0;
|
|
11
|
+
let prev = new Array(b.length + 1).fill(0);
|
|
12
|
+
for (let i = 1; i <= a.length; i++) {
|
|
13
|
+
const cur = new Array(b.length + 1).fill(0);
|
|
14
|
+
for (let j = 1; j <= b.length; j++) {
|
|
15
|
+
if (a[i - 1] === b[j - 1]) {
|
|
16
|
+
cur[j] = prev[j - 1] + 1;
|
|
17
|
+
if (cur[j] > best) {
|
|
18
|
+
best = cur[j];
|
|
19
|
+
ai = i - best;
|
|
20
|
+
bi = j - best;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
prev = cur;
|
|
25
|
+
}
|
|
26
|
+
if (!best)
|
|
27
|
+
return 0;
|
|
28
|
+
return (best +
|
|
29
|
+
matching(a.slice(0, ai), b.slice(0, bi)) +
|
|
30
|
+
matching(a.slice(ai + best), b.slice(bi + best)));
|
|
31
|
+
}
|
|
32
|
+
export function similarity(a, b) {
|
|
33
|
+
const total = a.length + b.length;
|
|
34
|
+
return total ? (2 * matching(a, b)) / total : 1;
|
|
35
|
+
}
|
|
36
|
+
export function closest(word, options, cutoff = 0.6) {
|
|
37
|
+
let best = null;
|
|
38
|
+
let score = cutoff;
|
|
39
|
+
for (const o of options) {
|
|
40
|
+
const s = similarity(word, o);
|
|
41
|
+
if (s >= score && (best === null || s > score)) {
|
|
42
|
+
best = o;
|
|
43
|
+
score = s;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return best;
|
|
47
|
+
}
|
|
48
|
+
export function didYouMean(word, options, show = (s) => s) {
|
|
49
|
+
const m = closest(word, options);
|
|
50
|
+
return m ? `. Did you mean \`${show(m)}\`?` : '';
|
|
51
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/values.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading `key: value` lists and checking them against a schema.
|
|
3
|
+
*
|
|
4
|
+
* Three surface styles share one validator:
|
|
5
|
+
* config `{ persona: dan, prompt: "rain, distant thunder" }` commas, quotes allowed
|
|
6
|
+
* overrides `[excited, seed:303]` commas; a bareword is an emotion
|
|
7
|
+
* play `[GLASS vol:0.9, pan:0.2 hold]` commas or spaces; a bareword is a flag
|
|
8
|
+
*/
|
|
9
|
+
import { enumValues } from './plugin';
|
|
10
|
+
import { ALIASES, shown } from './schema';
|
|
11
|
+
import { didYouMean } from './suggest';
|
|
12
|
+
const canonical = (k) => {
|
|
13
|
+
const key = k.trim().toLowerCase().replace(/-/g, '_');
|
|
14
|
+
return ALIASES[key] ?? key;
|
|
15
|
+
};
|
|
16
|
+
function pair(part) {
|
|
17
|
+
const i = part.indexOf(':');
|
|
18
|
+
if (i < 0)
|
|
19
|
+
return { key: canonical(part), typed: part.trim(), value: true };
|
|
20
|
+
const typed = part.slice(0, i).trim();
|
|
21
|
+
return { key: canonical(typed), typed, value: part.slice(i + 1).trim() };
|
|
22
|
+
}
|
|
23
|
+
/** Split on commas outside quotes, dropping the quote characters themselves. */
|
|
24
|
+
export function splitTop(raw) {
|
|
25
|
+
const out = [];
|
|
26
|
+
let buf = '';
|
|
27
|
+
let quote = null;
|
|
28
|
+
for (const ch of raw) {
|
|
29
|
+
if (quote) {
|
|
30
|
+
if (ch === quote)
|
|
31
|
+
quote = null;
|
|
32
|
+
else
|
|
33
|
+
buf += ch;
|
|
34
|
+
}
|
|
35
|
+
else if (ch === '"' || ch === "'") {
|
|
36
|
+
quote = ch;
|
|
37
|
+
}
|
|
38
|
+
else if (ch === ',') {
|
|
39
|
+
out.push(buf);
|
|
40
|
+
buf = '';
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
buf += ch;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
out.push(buf);
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
export function readConfig(raw, errs) {
|
|
50
|
+
const pairs = [];
|
|
51
|
+
for (const part of splitTop(raw)) {
|
|
52
|
+
if (!part.trim())
|
|
53
|
+
continue;
|
|
54
|
+
const p = pair(part);
|
|
55
|
+
if (p.value === true) {
|
|
56
|
+
errs.push(`\`${p.typed}\` needs a value, e.g. \`${p.typed}: …\``);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
pairs.push(p);
|
|
60
|
+
}
|
|
61
|
+
return pairs;
|
|
62
|
+
}
|
|
63
|
+
export function readOverrides(raw, errs) {
|
|
64
|
+
const pairs = [];
|
|
65
|
+
const barewords = [];
|
|
66
|
+
for (const part of raw.split(',')) {
|
|
67
|
+
if (!part.trim())
|
|
68
|
+
continue;
|
|
69
|
+
const p = pair(part);
|
|
70
|
+
if (p.value === true)
|
|
71
|
+
barewords.push(p.typed.toLowerCase());
|
|
72
|
+
else
|
|
73
|
+
pairs.push(p);
|
|
74
|
+
}
|
|
75
|
+
if (barewords.length > 1) {
|
|
76
|
+
errs.push(`\`[${raw}]\` names ${barewords.length} emotions (${barewords.join(', ')}) — a line has one. ` +
|
|
77
|
+
`Pick one, e.g. \`[${barewords[0]}]\``);
|
|
78
|
+
}
|
|
79
|
+
if (barewords.length) {
|
|
80
|
+
const typed = barewords[barewords.length - 1];
|
|
81
|
+
pairs.push({ key: 'emotion', typed: 'emotion', value: typed });
|
|
82
|
+
}
|
|
83
|
+
return pairs;
|
|
84
|
+
}
|
|
85
|
+
export function readPlay(raw) {
|
|
86
|
+
return (raw || '')
|
|
87
|
+
.split(/[,\s]+/)
|
|
88
|
+
.filter((p) => p.trim())
|
|
89
|
+
.map(pair);
|
|
90
|
+
}
|
|
91
|
+
export function seconds(v) {
|
|
92
|
+
const s = String(v).trim();
|
|
93
|
+
if (!s)
|
|
94
|
+
return NaN;
|
|
95
|
+
if (s.includes(':')) {
|
|
96
|
+
return s.split(':').reduce((total, part) => {
|
|
97
|
+
return part.trim() === '' ? NaN : total * 60 + Number(part);
|
|
98
|
+
}, 0);
|
|
99
|
+
}
|
|
100
|
+
return Number(s);
|
|
101
|
+
}
|
|
102
|
+
const BOOLS = {
|
|
103
|
+
true: true,
|
|
104
|
+
yes: true,
|
|
105
|
+
on: true,
|
|
106
|
+
false: false,
|
|
107
|
+
no: false,
|
|
108
|
+
off: false,
|
|
109
|
+
};
|
|
110
|
+
function coerce(p, spec, opts) {
|
|
111
|
+
const k = p.typed;
|
|
112
|
+
const raw = p.value;
|
|
113
|
+
const text = raw === true ? 'true' : raw;
|
|
114
|
+
switch (spec.kind) {
|
|
115
|
+
case 'string':
|
|
116
|
+
return text.trim() ? { value: text } : { error: `\`${shown(p.key)}\` is empty` };
|
|
117
|
+
case 'bool': {
|
|
118
|
+
const b = BOOLS[text.toLowerCase()];
|
|
119
|
+
return b === undefined
|
|
120
|
+
? { error: `\`${k}:${text}\` — use true or false (yes/no also work)` }
|
|
121
|
+
: { value: b };
|
|
122
|
+
}
|
|
123
|
+
case 'int':
|
|
124
|
+
if (raw === true)
|
|
125
|
+
break;
|
|
126
|
+
if (/^-?\d+$/.test(text))
|
|
127
|
+
return { value: parseInt(text, 10) };
|
|
128
|
+
return {
|
|
129
|
+
error: p.key === 'seed'
|
|
130
|
+
? `\`${k}:${text}\` — a seed must be a whole number, e.g. \`seed:303\``
|
|
131
|
+
: `\`${k}:${text}\` — ${k} must be a whole number`,
|
|
132
|
+
};
|
|
133
|
+
case 'time': {
|
|
134
|
+
if (raw === true)
|
|
135
|
+
break;
|
|
136
|
+
const s = seconds(text);
|
|
137
|
+
return Number.isFinite(s)
|
|
138
|
+
? { value: s }
|
|
139
|
+
: { error: `\`${k}:${text}\` — ${k} must be seconds or m:ss, e.g. \`${k}:0:15\`` };
|
|
140
|
+
}
|
|
141
|
+
case 'number': {
|
|
142
|
+
if (raw === true)
|
|
143
|
+
break;
|
|
144
|
+
const n = text.trim() === '' ? NaN : Number(text);
|
|
145
|
+
if (!Number.isFinite(n))
|
|
146
|
+
return { error: `\`${k}:${text}\` — ${k} must be a number` };
|
|
147
|
+
if (n < spec.min || n > spec.max) {
|
|
148
|
+
return { error: `\`${k}:${text}\` — out of range; use ${spec.min} to ${spec.max}` };
|
|
149
|
+
}
|
|
150
|
+
return { value: n };
|
|
151
|
+
}
|
|
152
|
+
case 'enum': {
|
|
153
|
+
if (raw === true)
|
|
154
|
+
break;
|
|
155
|
+
const options = enumValues(opts.reg, spec);
|
|
156
|
+
const v = text.toLowerCase();
|
|
157
|
+
if (options.includes(v))
|
|
158
|
+
return { value: v };
|
|
159
|
+
return {
|
|
160
|
+
error: `\`${k}:${text}\` — not one of: ${options.join(', ')}` + didYouMean(v, options),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
case 'track':
|
|
164
|
+
case 'tracks': {
|
|
165
|
+
if (raw === true)
|
|
166
|
+
break;
|
|
167
|
+
const names = spec.kind === 'tracks'
|
|
168
|
+
? text
|
|
169
|
+
.split(/[,+]/)
|
|
170
|
+
.map((n) => n.trim())
|
|
171
|
+
.filter(Boolean)
|
|
172
|
+
: [text];
|
|
173
|
+
for (const n of names) {
|
|
174
|
+
if (spec.kind === 'tracks' && n === 'speech')
|
|
175
|
+
continue;
|
|
176
|
+
if (!opts.tracks.has(n)) {
|
|
177
|
+
return {
|
|
178
|
+
error: `\`${k}:${text}\` — no track named \`${n}\`. Tracks: ${[...opts.tracks].sort().join(', ')}` +
|
|
179
|
+
didYouMean(n, opts.tracks) +
|
|
180
|
+
`\nDeclare new tracks under @tracks, e.g. \`${n}: { level: -24 }\``,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { value: spec.kind === 'tracks' ? names : text };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return { error: `\`${k}\` needs a value, e.g. \`${k}:…\`` };
|
|
188
|
+
}
|
|
189
|
+
function readAt(p) {
|
|
190
|
+
const text = p.value === true ? '' : p.value;
|
|
191
|
+
const n = text === '' ? NaN : Number(text);
|
|
192
|
+
if (!Number.isFinite(n)) {
|
|
193
|
+
return {
|
|
194
|
+
error: `\`at:${text}\` — at must be seconds: \`at:18.5\` from the start, ` +
|
|
195
|
+
`or \`at:+2\` / \`at:-2\` from this point`,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const at = { mode: /^[+-]/.test(text) ? 'offset' : 'absolute', seconds: n };
|
|
199
|
+
return { value: at };
|
|
200
|
+
}
|
|
201
|
+
/** Validate and coerce pairs against a schema. Problems go to `errs`; bad keys are left out. */
|
|
202
|
+
export function check(pairs, schema, opts, errs) {
|
|
203
|
+
const out = {};
|
|
204
|
+
for (const p0 of pairs) {
|
|
205
|
+
let p = p0;
|
|
206
|
+
// on a play or a fader move, `gain` is a linear multiplier: the same as vol
|
|
207
|
+
if (p.key === 'gain' && !('gain' in schema) && 'volume' in schema)
|
|
208
|
+
p = { ...p, key: 'volume' };
|
|
209
|
+
const spec = schema[p.key];
|
|
210
|
+
if (!spec) {
|
|
211
|
+
if ((opts.context === 'line' || opts.context === 'span') && ['persona', 'voice', 'speaker'].includes(p.key)) {
|
|
212
|
+
errs.push(`\`${p.typed}\` can't be changed per line — set it on the character under @characters, ` +
|
|
213
|
+
'e.g. `DAN: { persona: dan }`');
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
errs.push(`unknown key \`${shown(p.key)}\` on ${opts.what}` +
|
|
217
|
+
didYouMean(p.key, Object.keys(schema), shown) +
|
|
218
|
+
`\nValid keys: ${Object.keys(schema).sort().map(shown).join(', ')}`);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const r = p.key === 'at' ? readAt(p) : coerce(p, spec, opts);
|
|
222
|
+
if ('error' in r)
|
|
223
|
+
errs.push(r.error);
|
|
224
|
+
else
|
|
225
|
+
out[p.key] = r.value;
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
package/include.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@include` is resolved through an adapter, so the parser never touches a
|
|
3
|
+
* filesystem: a CLI reads files, a browser fetches ahead of time, a test keeps
|
|
4
|
+
* banks in memory.
|
|
5
|
+
*/
|
|
6
|
+
export type IncludeResult =
|
|
7
|
+
/** `id` identifies the bank (for cycle detection and as the `from` of its own includes) */
|
|
8
|
+
{
|
|
9
|
+
id: string;
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
/** not found: where it looked, for the error message */
|
|
13
|
+
| {
|
|
14
|
+
tried: string[];
|
|
15
|
+
};
|
|
16
|
+
export interface IncludeResolver {
|
|
17
|
+
/** `from` is the id/source of the including file, when known */
|
|
18
|
+
resolve(ref: string, from: string | undefined): IncludeResult;
|
|
19
|
+
/** bank names, for suggestions when a ref isn't found */
|
|
20
|
+
list?(): string[];
|
|
21
|
+
}
|
|
22
|
+
/** Banks held in memory, by name: `@include podcast` reads `banks.podcast`. */
|
|
23
|
+
export declare function memoryResolver(banks: Record<string, string>): IncludeResolver;
|
package/include.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `@include` is resolved through an adapter, so the parser never touches a
|
|
4
|
+
* filesystem: a CLI reads files, a browser fetches ahead of time, a test keeps
|
|
5
|
+
* banks in memory.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.memoryResolver = memoryResolver;
|
|
9
|
+
/** Banks held in memory, by name: `@include podcast` reads `banks.podcast`. */
|
|
10
|
+
function memoryResolver(banks) {
|
|
11
|
+
return {
|
|
12
|
+
resolve(ref) {
|
|
13
|
+
const name = ref.replace(/\.sounds$/, '');
|
|
14
|
+
return name in banks ? { id: name, text: banks[name] } : { tried: [name] };
|
|
15
|
+
},
|
|
16
|
+
list: () => Object.keys(banks).sort(),
|
|
17
|
+
};
|
|
18
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ConvError, formatProblems } from './errors';
|
|
2
|
+
export { IncludeResolver, IncludeResult, memoryResolver } from './include';
|
|
3
|
+
export { parse, ParseOptions } from './parse';
|
|
4
|
+
export { ConvPlugin, Directive, DirectiveContext, FinalizeContext, TrackRule, TrackRuleContext, } from './plugin';
|
|
5
|
+
export { defaultPlugins, effects, EffectsOptions, slides, SPEAKER_WIDGETS, speakers, video } from './plugins';
|
|
6
|
+
export { bool, CORE_RESERVED, CORE_SCHEMAS, CORE_TRACKS, int, KeyContext, KeySpec, named, num, oneOf, Schema, str, time, track, tracks, } from './schema';
|
|
7
|
+
export { didYouMean, similarity } from './suggest';
|
|
8
|
+
export * from './types';
|
package/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.similarity = exports.didYouMean = exports.tracks = exports.track = exports.time = exports.str = exports.oneOf = exports.num = exports.named = exports.int = exports.CORE_TRACKS = exports.CORE_SCHEMAS = exports.CORE_RESERVED = exports.bool = exports.video = exports.speakers = exports.SPEAKER_WIDGETS = exports.slides = exports.effects = exports.defaultPlugins = exports.parse = exports.memoryResolver = exports.formatProblems = exports.ConvError = void 0;
|
|
18
|
+
var errors_1 = require("./errors");
|
|
19
|
+
Object.defineProperty(exports, "ConvError", { enumerable: true, get: function () { return errors_1.ConvError; } });
|
|
20
|
+
Object.defineProperty(exports, "formatProblems", { enumerable: true, get: function () { return errors_1.formatProblems; } });
|
|
21
|
+
var include_1 = require("./include");
|
|
22
|
+
Object.defineProperty(exports, "memoryResolver", { enumerable: true, get: function () { return include_1.memoryResolver; } });
|
|
23
|
+
var parse_1 = require("./parse");
|
|
24
|
+
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_1.parse; } });
|
|
25
|
+
var plugins_1 = require("./plugins");
|
|
26
|
+
Object.defineProperty(exports, "defaultPlugins", { enumerable: true, get: function () { return plugins_1.defaultPlugins; } });
|
|
27
|
+
Object.defineProperty(exports, "effects", { enumerable: true, get: function () { return plugins_1.effects; } });
|
|
28
|
+
Object.defineProperty(exports, "slides", { enumerable: true, get: function () { return plugins_1.slides; } });
|
|
29
|
+
Object.defineProperty(exports, "SPEAKER_WIDGETS", { enumerable: true, get: function () { return plugins_1.SPEAKER_WIDGETS; } });
|
|
30
|
+
Object.defineProperty(exports, "speakers", { enumerable: true, get: function () { return plugins_1.speakers; } });
|
|
31
|
+
Object.defineProperty(exports, "video", { enumerable: true, get: function () { return plugins_1.video; } });
|
|
32
|
+
var schema_1 = require("./schema");
|
|
33
|
+
Object.defineProperty(exports, "bool", { enumerable: true, get: function () { return schema_1.bool; } });
|
|
34
|
+
Object.defineProperty(exports, "CORE_RESERVED", { enumerable: true, get: function () { return schema_1.CORE_RESERVED; } });
|
|
35
|
+
Object.defineProperty(exports, "CORE_SCHEMAS", { enumerable: true, get: function () { return schema_1.CORE_SCHEMAS; } });
|
|
36
|
+
Object.defineProperty(exports, "CORE_TRACKS", { enumerable: true, get: function () { return schema_1.CORE_TRACKS; } });
|
|
37
|
+
Object.defineProperty(exports, "int", { enumerable: true, get: function () { return schema_1.int; } });
|
|
38
|
+
Object.defineProperty(exports, "named", { enumerable: true, get: function () { return schema_1.named; } });
|
|
39
|
+
Object.defineProperty(exports, "num", { enumerable: true, get: function () { return schema_1.num; } });
|
|
40
|
+
Object.defineProperty(exports, "oneOf", { enumerable: true, get: function () { return schema_1.oneOf; } });
|
|
41
|
+
Object.defineProperty(exports, "str", { enumerable: true, get: function () { return schema_1.str; } });
|
|
42
|
+
Object.defineProperty(exports, "time", { enumerable: true, get: function () { return schema_1.time; } });
|
|
43
|
+
Object.defineProperty(exports, "track", { enumerable: true, get: function () { return schema_1.track; } });
|
|
44
|
+
Object.defineProperty(exports, "tracks", { enumerable: true, get: function () { return schema_1.tracks; } });
|
|
45
|
+
var suggest_1 = require("./suggest");
|
|
46
|
+
Object.defineProperty(exports, "didYouMean", { enumerable: true, get: function () { return suggest_1.didYouMean; } });
|
|
47
|
+
Object.defineProperty(exports, "similarity", { enumerable: true, get: function () { return suggest_1.similarity; } });
|
|
48
|
+
__exportStar(require("./types"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@conversation/spec",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Parser for .conv, a plain-text screenplay format for multi-voice audio: cast, dialogue, sound cues and tracks",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/conversation-spec",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/conversation-spec"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/conversation-spec/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean dist",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy",
|
|
27
|
+
"build:dev": "npm run clean && tsc --declarationMap && tsc -p tsconfig.esm.json && npm run copy",
|
|
28
|
+
"lint": "eslint . --fix",
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"test:watch": "jest --watch"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"makage": "^0.5.1"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"conv",
|
|
37
|
+
"screenplay",
|
|
38
|
+
"parser",
|
|
39
|
+
"audio",
|
|
40
|
+
"dialogue",
|
|
41
|
+
"tts"
|
|
42
|
+
],
|
|
43
|
+
"gitHead": "e9ff4eb39290c79759e672ef752756af45cb67d4"
|
|
44
|
+
}
|
package/parse.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IncludeResolver } from './include';
|
|
2
|
+
import { ConvPlugin } from './plugin';
|
|
3
|
+
import { Scene } from './types';
|
|
4
|
+
export interface ParseOptions {
|
|
5
|
+
/** a name for the script in error messages, and the `from` of its includes */
|
|
6
|
+
source?: string;
|
|
7
|
+
/** throw a ConvError listing every problem (default), or return them in `scene.problems` */
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
/** defaults to `defaultPlugins`; pass `[...defaultPlugins, yours]` to extend */
|
|
10
|
+
plugins?: ConvPlugin[];
|
|
11
|
+
/** resolves `@include`; without one, an include is a problem */
|
|
12
|
+
include?: IncludeResolver;
|
|
13
|
+
}
|
|
14
|
+
/** Parse a `.conv` script. */
|
|
15
|
+
export declare function parse(text: string, options?: ParseOptions): Scene;
|