@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/values.js ADDED
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitTop = splitTop;
4
+ exports.readConfig = readConfig;
5
+ exports.readOverrides = readOverrides;
6
+ exports.readPlay = readPlay;
7
+ exports.seconds = seconds;
8
+ exports.check = check;
9
+ /**
10
+ * Reading `key: value` lists and checking them against a schema.
11
+ *
12
+ * Three surface styles share one validator:
13
+ * config `{ persona: dan, prompt: "rain, distant thunder" }` commas, quotes allowed
14
+ * overrides `[excited, seed:303]` commas; a bareword is an emotion
15
+ * play `[GLASS vol:0.9, pan:0.2 hold]` commas or spaces; a bareword is a flag
16
+ */
17
+ const plugin_1 = require("./plugin");
18
+ const schema_1 = require("./schema");
19
+ const suggest_1 = require("./suggest");
20
+ const canonical = (k) => {
21
+ const key = k.trim().toLowerCase().replace(/-/g, '_');
22
+ return schema_1.ALIASES[key] ?? key;
23
+ };
24
+ function pair(part) {
25
+ const i = part.indexOf(':');
26
+ if (i < 0)
27
+ return { key: canonical(part), typed: part.trim(), value: true };
28
+ const typed = part.slice(0, i).trim();
29
+ return { key: canonical(typed), typed, value: part.slice(i + 1).trim() };
30
+ }
31
+ /** Split on commas outside quotes, dropping the quote characters themselves. */
32
+ function splitTop(raw) {
33
+ const out = [];
34
+ let buf = '';
35
+ let quote = null;
36
+ for (const ch of raw) {
37
+ if (quote) {
38
+ if (ch === quote)
39
+ quote = null;
40
+ else
41
+ buf += ch;
42
+ }
43
+ else if (ch === '"' || ch === "'") {
44
+ quote = ch;
45
+ }
46
+ else if (ch === ',') {
47
+ out.push(buf);
48
+ buf = '';
49
+ }
50
+ else {
51
+ buf += ch;
52
+ }
53
+ }
54
+ out.push(buf);
55
+ return out;
56
+ }
57
+ function readConfig(raw, errs) {
58
+ const pairs = [];
59
+ for (const part of splitTop(raw)) {
60
+ if (!part.trim())
61
+ continue;
62
+ const p = pair(part);
63
+ if (p.value === true) {
64
+ errs.push(`\`${p.typed}\` needs a value, e.g. \`${p.typed}: …\``);
65
+ continue;
66
+ }
67
+ pairs.push(p);
68
+ }
69
+ return pairs;
70
+ }
71
+ function readOverrides(raw, errs) {
72
+ const pairs = [];
73
+ const barewords = [];
74
+ for (const part of raw.split(',')) {
75
+ if (!part.trim())
76
+ continue;
77
+ const p = pair(part);
78
+ if (p.value === true)
79
+ barewords.push(p.typed.toLowerCase());
80
+ else
81
+ pairs.push(p);
82
+ }
83
+ if (barewords.length > 1) {
84
+ errs.push(`\`[${raw}]\` names ${barewords.length} emotions (${barewords.join(', ')}) — a line has one. ` +
85
+ `Pick one, e.g. \`[${barewords[0]}]\``);
86
+ }
87
+ if (barewords.length) {
88
+ const typed = barewords[barewords.length - 1];
89
+ pairs.push({ key: 'emotion', typed: 'emotion', value: typed });
90
+ }
91
+ return pairs;
92
+ }
93
+ function readPlay(raw) {
94
+ return (raw || '')
95
+ .split(/[,\s]+/)
96
+ .filter((p) => p.trim())
97
+ .map(pair);
98
+ }
99
+ function seconds(v) {
100
+ const s = String(v).trim();
101
+ if (!s)
102
+ return NaN;
103
+ if (s.includes(':')) {
104
+ return s.split(':').reduce((total, part) => {
105
+ return part.trim() === '' ? NaN : total * 60 + Number(part);
106
+ }, 0);
107
+ }
108
+ return Number(s);
109
+ }
110
+ const BOOLS = {
111
+ true: true,
112
+ yes: true,
113
+ on: true,
114
+ false: false,
115
+ no: false,
116
+ off: false,
117
+ };
118
+ function coerce(p, spec, opts) {
119
+ const k = p.typed;
120
+ const raw = p.value;
121
+ const text = raw === true ? 'true' : raw;
122
+ switch (spec.kind) {
123
+ case 'string':
124
+ return text.trim() ? { value: text } : { error: `\`${(0, schema_1.shown)(p.key)}\` is empty` };
125
+ case 'bool': {
126
+ const b = BOOLS[text.toLowerCase()];
127
+ return b === undefined
128
+ ? { error: `\`${k}:${text}\` — use true or false (yes/no also work)` }
129
+ : { value: b };
130
+ }
131
+ case 'int':
132
+ if (raw === true)
133
+ break;
134
+ if (/^-?\d+$/.test(text))
135
+ return { value: parseInt(text, 10) };
136
+ return {
137
+ error: p.key === 'seed'
138
+ ? `\`${k}:${text}\` — a seed must be a whole number, e.g. \`seed:303\``
139
+ : `\`${k}:${text}\` — ${k} must be a whole number`,
140
+ };
141
+ case 'time': {
142
+ if (raw === true)
143
+ break;
144
+ const s = seconds(text);
145
+ return Number.isFinite(s)
146
+ ? { value: s }
147
+ : { error: `\`${k}:${text}\` — ${k} must be seconds or m:ss, e.g. \`${k}:0:15\`` };
148
+ }
149
+ case 'number': {
150
+ if (raw === true)
151
+ break;
152
+ const n = text.trim() === '' ? NaN : Number(text);
153
+ if (!Number.isFinite(n))
154
+ return { error: `\`${k}:${text}\` — ${k} must be a number` };
155
+ if (n < spec.min || n > spec.max) {
156
+ return { error: `\`${k}:${text}\` — out of range; use ${spec.min} to ${spec.max}` };
157
+ }
158
+ return { value: n };
159
+ }
160
+ case 'enum': {
161
+ if (raw === true)
162
+ break;
163
+ const options = (0, plugin_1.enumValues)(opts.reg, spec);
164
+ const v = text.toLowerCase();
165
+ if (options.includes(v))
166
+ return { value: v };
167
+ return {
168
+ error: `\`${k}:${text}\` — not one of: ${options.join(', ')}` + (0, suggest_1.didYouMean)(v, options),
169
+ };
170
+ }
171
+ case 'track':
172
+ case 'tracks': {
173
+ if (raw === true)
174
+ break;
175
+ const names = spec.kind === 'tracks'
176
+ ? text
177
+ .split(/[,+]/)
178
+ .map((n) => n.trim())
179
+ .filter(Boolean)
180
+ : [text];
181
+ for (const n of names) {
182
+ if (spec.kind === 'tracks' && n === 'speech')
183
+ continue;
184
+ if (!opts.tracks.has(n)) {
185
+ return {
186
+ error: `\`${k}:${text}\` — no track named \`${n}\`. Tracks: ${[...opts.tracks].sort().join(', ')}` +
187
+ (0, suggest_1.didYouMean)(n, opts.tracks) +
188
+ `\nDeclare new tracks under @tracks, e.g. \`${n}: { level: -24 }\``,
189
+ };
190
+ }
191
+ }
192
+ return { value: spec.kind === 'tracks' ? names : text };
193
+ }
194
+ }
195
+ return { error: `\`${k}\` needs a value, e.g. \`${k}:…\`` };
196
+ }
197
+ function readAt(p) {
198
+ const text = p.value === true ? '' : p.value;
199
+ const n = text === '' ? NaN : Number(text);
200
+ if (!Number.isFinite(n)) {
201
+ return {
202
+ error: `\`at:${text}\` — at must be seconds: \`at:18.5\` from the start, ` +
203
+ `or \`at:+2\` / \`at:-2\` from this point`,
204
+ };
205
+ }
206
+ const at = { mode: /^[+-]/.test(text) ? 'offset' : 'absolute', seconds: n };
207
+ return { value: at };
208
+ }
209
+ /** Validate and coerce pairs against a schema. Problems go to `errs`; bad keys are left out. */
210
+ function check(pairs, schema, opts, errs) {
211
+ const out = {};
212
+ for (const p0 of pairs) {
213
+ let p = p0;
214
+ // on a play or a fader move, `gain` is a linear multiplier: the same as vol
215
+ if (p.key === 'gain' && !('gain' in schema) && 'volume' in schema)
216
+ p = { ...p, key: 'volume' };
217
+ const spec = schema[p.key];
218
+ if (!spec) {
219
+ if ((opts.context === 'line' || opts.context === 'span') && ['persona', 'voice', 'speaker'].includes(p.key)) {
220
+ errs.push(`\`${p.typed}\` can't be changed per line — set it on the character under @characters, ` +
221
+ 'e.g. `DAN: { persona: dan }`');
222
+ continue;
223
+ }
224
+ errs.push(`unknown key \`${(0, schema_1.shown)(p.key)}\` on ${opts.what}` +
225
+ (0, suggest_1.didYouMean)(p.key, Object.keys(schema), schema_1.shown) +
226
+ `\nValid keys: ${Object.keys(schema).sort().map(schema_1.shown).join(', ')}`);
227
+ continue;
228
+ }
229
+ const r = p.key === 'at' ? readAt(p) : coerce(p, spec, opts);
230
+ if ('error' in r)
231
+ errs.push(r.error);
232
+ else
233
+ out[p.key] = r.value;
234
+ }
235
+ return out;
236
+ }