@faces-sh/cli 1.2.0 → 1.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 +51 -33
- package/dist/chat-command.d.ts +2 -0
- package/dist/chat.d.ts +2 -0
- package/dist/choices.d.ts +9 -0
- package/dist/choices.js +17 -1
- package/dist/client.d.ts +8 -0
- package/dist/client.js +37 -0
- package/dist/commands/chat/chat.d.ts +2 -0
- package/dist/commands/chat/messages.d.ts +2 -0
- package/dist/commands/chat/responses.d.ts +2 -0
- package/dist/commands/chat/thread.d.ts +2 -0
- package/dist/commands/choices.d.ts +3 -0
- package/dist/commands/choices.js +36 -0
- package/dist/commands/conversation/add.d.ts +2 -0
- package/dist/commands/conversation/add.js +5 -2
- package/dist/commands/conversation/create.d.ts +2 -0
- package/dist/commands/conversation/create.js +4 -2
- package/dist/commands/decide.d.ts +31 -0
- package/dist/commands/decide.js +84 -4
- package/dist/commands/face/list.d.ts +2 -0
- package/dist/commands/face/list.js +23 -1
- package/dist/commands/premise/add.d.ts +30 -0
- package/dist/commands/premise/add.js +86 -0
- package/dist/commands/premise/choices.d.ts +21 -0
- package/dist/commands/premise/choices.js +68 -0
- package/dist/commands/premise/claim.d.ts +20 -0
- package/dist/commands/premise/claim.js +38 -0
- package/dist/commands/premise/decide.d.ts +18 -0
- package/dist/commands/premise/decide.js +36 -0
- package/dist/commands/premise/delete.d.ts +17 -0
- package/dist/commands/premise/delete.js +17 -0
- package/dist/commands/premise/edit.d.ts +28 -0
- package/dist/commands/premise/edit.js +51 -0
- package/dist/commands/premise/get.d.ts +22 -0
- package/dist/commands/premise/get.js +37 -0
- package/dist/commands/premise/refine.d.ts +20 -0
- package/dist/commands/premise/refine.js +80 -0
- package/dist/commands/state/create.d.ts +13 -2
- package/dist/commands/state/create.js +55 -2
- package/dist/commands/state/get.d.ts +19 -2
- package/dist/commands/state/get.js +30 -2
- package/dist/commands/state/use.d.ts +16 -0
- package/dist/commands/state/use.js +25 -0
- package/dist/conversation.d.ts +30 -3
- package/dist/conversation.js +202 -18
- package/dist/foreman.d.ts +47 -0
- package/dist/foreman.js +115 -0
- package/dist/refine.d.ts +106 -0
- package/dist/refine.js +601 -0
- package/dist/synopsis.d.ts +49 -0
- package/dist/synopsis.js +43 -0
- package/oclif.manifest.json +1415 -109
- package/package.json +5 -2
package/dist/conversation.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { Flags } from '@oclif/core';
|
|
2
2
|
export const CONVERSATIONS_PATH = '/v1/states';
|
|
3
3
|
export const MAX_CONVERSATION_ITEMS = 20;
|
|
4
|
-
const
|
|
4
|
+
export const MAX_KEY_CHARS = 120;
|
|
5
|
+
const KEY_REFUSED = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/;
|
|
5
6
|
export const PREMISE_STATUSES = ['stated', 'verified', 'assumed', 'unresolved', 'invalidated'];
|
|
7
|
+
export const PREMISE_ROLES = ['goal', 'test'];
|
|
8
|
+
export const EDGE_KINDS = ['and', 'or'];
|
|
9
|
+
export const PREMISE_ORIGINS = ['human', 'agent', 'tool', 'web'];
|
|
6
10
|
export const MAX_DEPENDS_ON = 200;
|
|
7
11
|
export const CONVERSATION_FLAG_DESCRIPTION = 'Add this turn to a state, by id (from faces state:create). Send only what is new: ' +
|
|
8
12
|
'the state so far is read before it. A state opened with one face takes only that face.';
|
|
9
13
|
export const PREMISE_FLAG_DESCRIPTION = 'A premise, as key=value (repeatable): a named fact the face\'s answers depend on, kept on the ' +
|
|
10
|
-
'state. The key is 1 to
|
|
14
|
+
'state. The key is plain text, 1 to 120 characters, split from the value at the first =: ' +
|
|
15
|
+
'--premise \'everyone can eat=yes\'. A key that holds = goes in double quotes: --premise \'"a=b"=yes\'. ' +
|
|
16
|
+
'The value is read as JSON when it is JSON ' +
|
|
11
17
|
'(50000, true, null, ["a","b"], {"max":3}) and as text otherwise, so plan=annual is the text "annual". ' +
|
|
12
18
|
'Quote it to keep a number as text: --premise \'code="007"\'. A newer value of a key replaces it from ' +
|
|
13
19
|
'then on. A key used for the first time needs --premise-description.';
|
|
@@ -17,9 +23,20 @@ export const PREMISE_DESCRIPTION_FLAG_DESCRIPTION = 'What a --premise means, in
|
|
|
17
23
|
export const PREMISE_STATUS_FLAG_DESCRIPTION = `A --premise's status, as key=status (repeatable, one per key): ${PREMISE_STATUSES.join(', ')}. ` +
|
|
18
24
|
'Left out, a new premise is stated. An invalidated premise invalidates everything that depends on it; ' +
|
|
19
25
|
'set it to another status to restore them.';
|
|
20
|
-
export const PREMISE_DEPENDS_ON_FLAG_DESCRIPTION = 'What a --premise rests on, as key=a,b (repeatable, one per key): premise keys
|
|
21
|
-
|
|
26
|
+
export const PREMISE_DEPENDS_ON_FLAG_DESCRIPTION = 'What a --premise rests on, as key=a,b (repeatable, one per key): premise keys, decision ids and option ids ' +
|
|
27
|
+
'(opt_..., or cho_... for all of a choices list, from faces choices --state-id) already in ' +
|
|
28
|
+
`the state or set in the same request, at most ${MAX_DEPENDS_ON}, comma separated (a key that holds a comma ` +
|
|
29
|
+
'goes in double quotes: key=\'"x, y",b\'). key rests on each: each must hold for it to hold. Weigh an entry ' +
|
|
30
|
+
'with :and or :or, then a weight from 0 to 1, then @ and the decision that set it: ' +
|
|
31
|
+
'task=a,b:or:0.7,c:and:0.5@dec_x. An and entry is a child that must hold (its weight is importance); the or ' +
|
|
32
|
+
'entries are alternatives, one of which holds (their weights are probabilities and sum to 1). A plain entry is ' +
|
|
33
|
+
'an and with no weight. key= with nothing after it clears it; ' +
|
|
22
34
|
'left out, a newer value keeps what the key rested on before.';
|
|
35
|
+
export const PREMISE_ROLE_FLAG_DESCRIPTION = `A --premise's role, as key=role (repeatable, one per key): ${PREMISE_ROLES.join(' or ')}. wanted marks a goal, ` +
|
|
36
|
+
'the outcome the state is working toward (the work is done when every goal is achieved); test marks a check ' +
|
|
37
|
+
'that must pass. key= with nothing after it clears it; left out, a newer value keeps the role the key had.';
|
|
38
|
+
export const PREMISE_ORIGIN_FLAG_DESCRIPTION = `Who set a --premise, as key=origin (repeatable, one per key): ${PREMISE_ORIGINS.join(', ')}. key= with ` +
|
|
39
|
+
'nothing after it clears it; left out, a newer value keeps the origin the key had.';
|
|
23
40
|
export const CAUSES_VISIBLE_TURN_FLAG_DESCRIPTION = 'Whether the face reads the other faces\' causes on this turn, in a conversation with several faces. ' +
|
|
24
41
|
'Leave it out to follow the state (see faces state:create --no-causes-visible).';
|
|
25
42
|
export function conversationTurnFlags(opts = {}) {
|
|
@@ -31,6 +48,8 @@ export function conversationTurnFlags(opts = {}) {
|
|
|
31
48
|
'premise-description': Flags.string({ description: d(PREMISE_DESCRIPTION_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
|
|
32
49
|
'premise-status': Flags.string({ description: d(PREMISE_STATUS_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
|
|
33
50
|
'premise-depends-on': Flags.string({ description: d(PREMISE_DEPENDS_ON_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
|
|
51
|
+
'premise-role': Flags.string({ description: d(PREMISE_ROLE_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
|
|
52
|
+
'premise-origin': Flags.string({ description: d(PREMISE_ORIGIN_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
|
|
34
53
|
'causes-visible': Flags.boolean({ description: d(`${CAUSES_VISIBLE_TURN_FLAG_DESCRIPTION} Needs --conversation.`), allowNo: true, exclusive }),
|
|
35
54
|
};
|
|
36
55
|
}
|
|
@@ -53,11 +72,15 @@ export function conversationHint(err) {
|
|
|
53
72
|
return 'A state expires 30 days after it was last used, and a closed one cannot be used. ' +
|
|
54
73
|
'Start another with: faces state:create';
|
|
55
74
|
}
|
|
56
|
-
const fresh = err.statusCode === 422 ? /premise '([
|
|
57
|
-
if (fresh)
|
|
58
|
-
|
|
75
|
+
const fresh = err.statusCode === 422 ? /premise ('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*") is new/.exec(err.message) : null;
|
|
76
|
+
if (fresh) {
|
|
77
|
+
const key = unrepr(fresh[1]);
|
|
78
|
+
if (/^[A-Za-z0-9_-]+$/.test(key))
|
|
79
|
+
return `Say what it means: --premise-description ${key}="what it means, in plain words"`;
|
|
80
|
+
return `Say what it means: --premise-description ${shellWord(`${keyArg(key)}=what it means, in plain words`)}`;
|
|
81
|
+
}
|
|
59
82
|
if (err.statusCode === 422 && /depends_on` names no premise or decision/.test(err.message)) {
|
|
60
|
-
return 'Name premise keys and
|
|
83
|
+
return 'Name premise keys, decision ids and option ids already in the state (faces state:items <id>), or premises set in the same request.';
|
|
61
84
|
}
|
|
62
85
|
return undefined;
|
|
63
86
|
}
|
|
@@ -74,17 +97,55 @@ export function premiseValue(raw) {
|
|
|
74
97
|
return raw;
|
|
75
98
|
}
|
|
76
99
|
}
|
|
100
|
+
function quotedHead(raw) {
|
|
101
|
+
const s = raw.trimStart();
|
|
102
|
+
if (!s.startsWith('"'))
|
|
103
|
+
return undefined;
|
|
104
|
+
for (let i = 1; i < s.length; i++) {
|
|
105
|
+
if (s[i] === '\\') {
|
|
106
|
+
i++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (s[i] === '"') {
|
|
110
|
+
try {
|
|
111
|
+
return [JSON.parse(s.slice(0, i + 1)), s.slice(i + 1)];
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
77
120
|
function splitPair(raw, flag, shape) {
|
|
121
|
+
const q = quotedHead(raw);
|
|
122
|
+
if (q) {
|
|
123
|
+
const rest = q[1].trimStart();
|
|
124
|
+
if (!rest.startsWith('='))
|
|
125
|
+
throw new Error(`${flag} takes ${shape}; after a quoted key comes = (got '${raw}').`);
|
|
126
|
+
return [q[0].trim(), rest.slice(1)];
|
|
127
|
+
}
|
|
78
128
|
const i = raw.indexOf('=');
|
|
79
129
|
if (i <= 0)
|
|
80
130
|
throw new Error(`${flag} takes ${shape} (got '${raw}').`);
|
|
81
131
|
return [raw.slice(0, i).trim(), raw.slice(i + 1)];
|
|
82
132
|
}
|
|
83
133
|
function checkKey(key, flag) {
|
|
84
|
-
|
|
85
|
-
|
|
134
|
+
const n = [...key].length;
|
|
135
|
+
if (n < 1 || n > MAX_KEY_CHARS || KEY_REFUSED.test(key)) {
|
|
136
|
+
throw new Error(`${flag}: a premise key is plain text of 1 to ${MAX_KEY_CHARS} characters on one line, with no control characters (got ${JSON.stringify(key)}).`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export function keyArg(key) {
|
|
140
|
+
return /[=,]/.test(key) || key.startsWith('"') ? JSON.stringify(key) : key;
|
|
141
|
+
}
|
|
142
|
+
function shellWord(text) {
|
|
143
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(text) ? text : `'${text.replace(/'/g, "'\\''")}'`;
|
|
86
144
|
}
|
|
87
|
-
|
|
145
|
+
function unrepr(q) {
|
|
146
|
+
return q.slice(1, -1).replace(/\\(.)/g, '$1');
|
|
147
|
+
}
|
|
148
|
+
export function parsePremises(premises = [], descriptions = [], statuses = [], dependsOn = [], roles = [], origins = []) {
|
|
88
149
|
const out = [];
|
|
89
150
|
const seen = new Map();
|
|
90
151
|
for (const raw of premises) {
|
|
@@ -135,16 +196,131 @@ export function parsePremises(premises = [], descriptions = [], statuses = [], d
|
|
|
135
196
|
throw new Error(`--premise-depends-on names '${key}', which no --premise sets. Add --premise ${key}=<value>.`);
|
|
136
197
|
if (linked.has(key))
|
|
137
198
|
throw new Error(`--premise-depends-on sets '${key}' twice. List everything it rests on once, comma separated.`);
|
|
138
|
-
const deps =
|
|
139
|
-
if (deps.
|
|
199
|
+
const deps = parseWeightedDependsOn(text, '--premise-depends-on');
|
|
200
|
+
if (deps.some((d) => entryId(d) === key))
|
|
140
201
|
throw new Error(`--premise-depends-on: premise '${key}' cannot depend on itself.`);
|
|
141
202
|
linked.add(key);
|
|
142
203
|
p.depends_on = deps;
|
|
143
204
|
}
|
|
205
|
+
setOneOf(seen, roles, '--premise-role', 'role', PREMISE_ROLES);
|
|
206
|
+
setOneOf(seen, origins, '--premise-origin', 'origin', PREMISE_ORIGINS);
|
|
144
207
|
return out;
|
|
145
208
|
}
|
|
209
|
+
function setOneOf(seen, raws, flag, field, allowed) {
|
|
210
|
+
const done = new Set();
|
|
211
|
+
for (const raw of raws) {
|
|
212
|
+
const [key, text] = splitPair(raw, flag, `key=${field}`);
|
|
213
|
+
checkKey(key, flag);
|
|
214
|
+
const p = seen.get(key);
|
|
215
|
+
if (!p)
|
|
216
|
+
throw new Error(`${flag} names '${key}', which no --premise sets. Add --premise ${key}=<value>.`);
|
|
217
|
+
if (done.has(key))
|
|
218
|
+
throw new Error(`${flag} sets '${key}' twice. Give one ${field} per key.`);
|
|
219
|
+
const v = text.trim().toLowerCase();
|
|
220
|
+
if (v !== '' && !allowed.includes(v)) {
|
|
221
|
+
throw new Error(`${flag} for '${key}' is ${allowed.length === 1 ? allowed[0] : `one of ${allowed.join(', ')}`}, or ${key}= to clear it (got '${text}').`);
|
|
222
|
+
}
|
|
223
|
+
done.add(key);
|
|
224
|
+
p[field] = v === '' ? null : v;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
146
227
|
export function parseDependsOn(raw, flag = '--depends-on') {
|
|
147
|
-
const
|
|
228
|
+
const names = [];
|
|
229
|
+
let s = raw;
|
|
230
|
+
for (;;) {
|
|
231
|
+
s = s.trimStart();
|
|
232
|
+
if (s === '')
|
|
233
|
+
break;
|
|
234
|
+
const q = quotedHead(s);
|
|
235
|
+
if (q) {
|
|
236
|
+
names.push(q[0]);
|
|
237
|
+
s = q[1].trimStart();
|
|
238
|
+
if (s !== '' && !s.startsWith(','))
|
|
239
|
+
throw new Error(`${flag}: after a quoted key comes a comma or the end (got '${raw}').`);
|
|
240
|
+
s = s.slice(1);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const i = s.indexOf(',');
|
|
244
|
+
names.push((i < 0 ? s : s.slice(0, i)).trim());
|
|
245
|
+
if (i < 0)
|
|
246
|
+
break;
|
|
247
|
+
s = s.slice(i + 1);
|
|
248
|
+
}
|
|
249
|
+
const out = [...new Set(names.map((x) => x.trim()).filter(Boolean))];
|
|
250
|
+
if (out.length > MAX_DEPENDS_ON)
|
|
251
|
+
throw new Error(`${flag} names at most ${MAX_DEPENDS_ON} premises and decisions (got ${out.length}).`);
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
export function entryId(d) {
|
|
255
|
+
return typeof d === 'string' ? d : d.id;
|
|
256
|
+
}
|
|
257
|
+
export function entryText(d) {
|
|
258
|
+
if (typeof d === 'string')
|
|
259
|
+
return keyArg(d);
|
|
260
|
+
const p = typeof d.p === 'number' ? ` ${Math.round(d.p * 100) / 100}` : '';
|
|
261
|
+
return `${keyArg(d.id)} (${d.kind}${p}${d.decision ? ` by ${d.decision}` : ''})`;
|
|
262
|
+
}
|
|
263
|
+
const EDGE_SUFFIX = /^(?::(and|or)(?::([^:@,]*))?)?(?:@(dec_[A-Za-z0-9_-]+))?$/;
|
|
264
|
+
const UNQUOTED_ENTRY = /^(.*?)((?::(?:and|or)(?::[^:@,]*)?)?(?:@dec_[A-Za-z0-9_-]+)?)$/;
|
|
265
|
+
function edgeOf(id, suffix, flag, raw) {
|
|
266
|
+
const m = EDGE_SUFFIX.exec(suffix);
|
|
267
|
+
if (!m)
|
|
268
|
+
throw new Error(`${flag}: '${suffix}' after '${id}' is not :and or :or, then :p, then @dec_... (got '${raw}').`);
|
|
269
|
+
const [, kind, p, decision] = m;
|
|
270
|
+
if (!kind && !decision)
|
|
271
|
+
return id;
|
|
272
|
+
if (!kind)
|
|
273
|
+
throw new Error(`${flag}: give '${id}' a kind before its decision, :and or :or (as ${id}:or:0.7@${decision}).`);
|
|
274
|
+
const out = { id, kind };
|
|
275
|
+
if (p !== undefined) {
|
|
276
|
+
const n = Number(p);
|
|
277
|
+
if (p.trim() === '' || !Number.isFinite(n) || n < 0 || n > 1)
|
|
278
|
+
throw new Error(`${flag}: the weight of '${id}' is a number from 0 to 1 (got '${p}').`);
|
|
279
|
+
out.p = n;
|
|
280
|
+
}
|
|
281
|
+
if (decision)
|
|
282
|
+
out.decision = decision;
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
export function parseWeightedDependsOn(raw, flag = '--premise-depends-on') {
|
|
286
|
+
const out = [];
|
|
287
|
+
let s = raw;
|
|
288
|
+
for (;;) {
|
|
289
|
+
s = s.trimStart();
|
|
290
|
+
if (s === '')
|
|
291
|
+
break;
|
|
292
|
+
const q = quotedHead(s);
|
|
293
|
+
let entry;
|
|
294
|
+
if (q) {
|
|
295
|
+
const rest = q[1];
|
|
296
|
+
const i = rest.indexOf(',');
|
|
297
|
+
entry = edgeOf(q[0], (i < 0 ? rest : rest.slice(0, i)).trim(), flag, raw);
|
|
298
|
+
if (i < 0)
|
|
299
|
+
s = '';
|
|
300
|
+
else
|
|
301
|
+
s = rest.slice(i + 1);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
const i = s.indexOf(',');
|
|
305
|
+
const tok = (i < 0 ? s : s.slice(0, i)).trim();
|
|
306
|
+
const m = UNQUOTED_ENTRY.exec(tok);
|
|
307
|
+
if (m[1].trim() === '')
|
|
308
|
+
throw new Error(`${flag}: an entry needs an id before its weight (got '${tok}').`);
|
|
309
|
+
entry = edgeOf(m[1].trim(), m[2], flag, raw);
|
|
310
|
+
if (i < 0)
|
|
311
|
+
s = '';
|
|
312
|
+
else
|
|
313
|
+
s = s.slice(i + 1);
|
|
314
|
+
}
|
|
315
|
+
const id = entryId(entry);
|
|
316
|
+
const was = out.findIndex((d) => entryId(d) === id);
|
|
317
|
+
if (was >= 0) {
|
|
318
|
+
if (typeof entry !== 'string' || typeof out[was] !== 'string')
|
|
319
|
+
throw new Error(`${flag} names '${id}' twice. Give each entry once.`);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
out.push(entry);
|
|
323
|
+
}
|
|
148
324
|
if (out.length > MAX_DEPENDS_ON)
|
|
149
325
|
throw new Error(`${flag} names at most ${MAX_DEPENDS_ON} premises and decisions (got ${out.length}).`);
|
|
150
326
|
return out;
|
|
@@ -154,7 +330,7 @@ export function conversationFields(flags) {
|
|
|
154
330
|
if (flags.conversation && flags.session && flags.conversation !== flags.session) {
|
|
155
331
|
throw new Error('--session is the old name for --conversation: pass one of them, not two different ids.');
|
|
156
332
|
}
|
|
157
|
-
const premises = parsePremises(flags.premise, flags['premise-description'], flags['premise-status'], flags['premise-depends-on']);
|
|
333
|
+
const premises = parsePremises(flags.premise, flags['premise-description'], flags['premise-status'], flags['premise-depends-on'], flags['premise-role'], flags['premise-origin']);
|
|
158
334
|
const inConversation = Boolean(flags.conversation || flags.session);
|
|
159
335
|
if (!inConversation && (premises.length > 0 || flags['causes-visible'] !== undefined)) {
|
|
160
336
|
throw new Error('Premises and --causes-visible belong to a state: add --conversation <id> (from faces state:create).');
|
|
@@ -184,7 +360,15 @@ function statusNote(p) {
|
|
|
184
360
|
if (p.because)
|
|
185
361
|
parts.push(`because ${p.because}`);
|
|
186
362
|
if (p.depends_on && p.depends_on.length > 0)
|
|
187
|
-
parts.push(`rests on ${p.depends_on.join(', ')}`);
|
|
363
|
+
parts.push(`rests on ${p.depends_on.map(entryText).join(', ')}`);
|
|
364
|
+
if (p.role)
|
|
365
|
+
parts.push(p.role);
|
|
366
|
+
if (p.origin)
|
|
367
|
+
parts.push(`from ${p.origin}`);
|
|
368
|
+
if (p.face)
|
|
369
|
+
parts.push(`proposed by @${p.face}`);
|
|
370
|
+
if (p.reason)
|
|
371
|
+
parts.push(p.reason);
|
|
188
372
|
return parts.length > 0 ? ` [${parts.join('; ')}]` : '';
|
|
189
373
|
}
|
|
190
374
|
export function statusChangeLines(changes) {
|
|
@@ -203,7 +387,7 @@ export function premiseLines(premises) {
|
|
|
203
387
|
return ['premises: none'];
|
|
204
388
|
const lines = ['premises:'];
|
|
205
389
|
for (const [key, p] of entries) {
|
|
206
|
-
lines.push(` ${key} = ${shownValue(p?.value)}${statusNote(p)}${p?.description ? ` (${p.description})` : ''}`);
|
|
390
|
+
lines.push(` ${keyArg(key)} = ${shownValue(p?.value)}${statusNote(p)}${p?.description ? ` (${p.description})` : ''}`);
|
|
207
391
|
}
|
|
208
392
|
return lines;
|
|
209
393
|
}
|
|
@@ -272,7 +456,7 @@ export function itemLines(items) {
|
|
|
272
456
|
if (call?.name === 'premise') {
|
|
273
457
|
const key = String(parsed(call.arguments).key ?? '?');
|
|
274
458
|
const desc = typeof out.description === 'string' && out.description ? ` (${out.description})` : '';
|
|
275
|
-
lines.push(`${head}premise ${key} = ${shownValue(out.value)}${desc}`);
|
|
459
|
+
lines.push(`${head}premise ${keyArg(key)} = ${shownValue(out.value)}${desc}`);
|
|
276
460
|
}
|
|
277
461
|
else if (call?.name === 'decision') {
|
|
278
462
|
const who = String(it.face ?? call.face ?? 'face');
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BaseCommand } from './base.js';
|
|
2
|
+
import { FacesClient } from './client.js';
|
|
3
|
+
import { premiseValue } from './conversation.js';
|
|
4
|
+
export declare const STATE_DIR = ".faces";
|
|
5
|
+
export declare const STATE_FILE = "state";
|
|
6
|
+
export declare const STATE_LOCAL_FILE = "state.local";
|
|
7
|
+
export declare function findStateFile(from?: string): {
|
|
8
|
+
file: string;
|
|
9
|
+
id: string;
|
|
10
|
+
} | undefined;
|
|
11
|
+
export declare function writeStateFile(id: string, local?: boolean, dir?: string): string;
|
|
12
|
+
export declare function keyList(raw: string[] | undefined, flag: string): string[] | undefined;
|
|
13
|
+
export { premiseValue };
|
|
14
|
+
export declare const stateIdFlag: {
|
|
15
|
+
'state-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
|
+
};
|
|
17
|
+
export declare const asFlag: {
|
|
18
|
+
as: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
19
|
+
};
|
|
20
|
+
export declare abstract class ReplyCommand extends BaseCommand {
|
|
21
|
+
protected stateId(flags: {
|
|
22
|
+
'state-id'?: string;
|
|
23
|
+
}, positional?: string): string;
|
|
24
|
+
protected statePath(id: string, route: string): string;
|
|
25
|
+
protected reply(client: FacesClient, method: 'GET' | 'POST' | 'PATCH' | 'DELETE', p: string, opts?: {
|
|
26
|
+
body?: Record<string, unknown>;
|
|
27
|
+
params?: Record<string, string>;
|
|
28
|
+
}): Promise<Record<string, unknown>>;
|
|
29
|
+
protected show(got: {
|
|
30
|
+
status: number;
|
|
31
|
+
ok: boolean;
|
|
32
|
+
data: Record<string, unknown>;
|
|
33
|
+
}): Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
export declare function compact(body: Record<string, unknown>): Record<string, unknown>;
|
|
36
|
+
export declare const formatFlags: {
|
|
37
|
+
problog: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
38
|
+
dot: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
39
|
+
};
|
|
40
|
+
export declare function formatOf(flags: {
|
|
41
|
+
problog?: boolean;
|
|
42
|
+
dot?: boolean;
|
|
43
|
+
}, json: boolean): string;
|
|
44
|
+
export declare const faceFlag: {
|
|
45
|
+
face: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
46
|
+
};
|
|
47
|
+
export declare function faceRef(face: string): string;
|
package/dist/foreman.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { Flags } from '@oclif/core';
|
|
4
|
+
import { BaseCommand } from './base.js';
|
|
5
|
+
import { FacesAPIError } from './client.js';
|
|
6
|
+
import { CONVERSATIONS_PATH, parseDependsOn, premiseValue } from './conversation.js';
|
|
7
|
+
import { seg } from './utils.js';
|
|
8
|
+
export const STATE_DIR = '.faces';
|
|
9
|
+
export const STATE_FILE = 'state';
|
|
10
|
+
export const STATE_LOCAL_FILE = 'state.local';
|
|
11
|
+
export function findStateFile(from = process.cwd()) {
|
|
12
|
+
let dir = path.resolve(from);
|
|
13
|
+
for (;;) {
|
|
14
|
+
for (const name of [STATE_LOCAL_FILE, STATE_FILE]) {
|
|
15
|
+
const file = path.join(dir, STATE_DIR, name);
|
|
16
|
+
try {
|
|
17
|
+
const id = fs.readFileSync(file, 'utf8').trim().split(/\s+/)[0];
|
|
18
|
+
if (id)
|
|
19
|
+
return { file, id };
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const up = path.dirname(dir);
|
|
25
|
+
if (up === dir)
|
|
26
|
+
return undefined;
|
|
27
|
+
dir = up;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function writeStateFile(id, local = false, dir = process.cwd()) {
|
|
31
|
+
const d = path.join(dir, STATE_DIR);
|
|
32
|
+
fs.mkdirSync(d, { recursive: true });
|
|
33
|
+
const file = path.join(d, local ? STATE_LOCAL_FILE : STATE_FILE);
|
|
34
|
+
fs.writeFileSync(file, id + '\n');
|
|
35
|
+
return file;
|
|
36
|
+
}
|
|
37
|
+
export function keyList(raw, flag) {
|
|
38
|
+
if (!raw || raw.length === 0)
|
|
39
|
+
return undefined;
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const r of raw)
|
|
42
|
+
out.push(...parseDependsOn(r, flag));
|
|
43
|
+
return [...new Set(out)];
|
|
44
|
+
}
|
|
45
|
+
export { premiseValue };
|
|
46
|
+
export const stateIdFlag = {
|
|
47
|
+
'state-id': Flags.string({
|
|
48
|
+
description: 'The state (fs_...). Default: FACES_STATE_ID, else .faces/state.local or .faces/state here or above.',
|
|
49
|
+
env: 'FACES_STATE_ID',
|
|
50
|
+
}),
|
|
51
|
+
};
|
|
52
|
+
export const asFlag = {
|
|
53
|
+
as: Flags.string({ description: 'Who writes: your name, as a claim names it. A write into a branch another has claimed is refused (409) unless it is theirs.' }),
|
|
54
|
+
};
|
|
55
|
+
export class ReplyCommand extends BaseCommand {
|
|
56
|
+
stateId(flags, positional) {
|
|
57
|
+
const id = positional || flags['state-id'] || findStateFile()?.id;
|
|
58
|
+
if (!id) {
|
|
59
|
+
this.error('No state here: no --state-id, no FACES_STATE_ID and no .faces/state in this directory or above. ' +
|
|
60
|
+
'faces state:create --use makes one; faces state:use <id> uses one of yours.');
|
|
61
|
+
}
|
|
62
|
+
return id;
|
|
63
|
+
}
|
|
64
|
+
statePath(id, route) {
|
|
65
|
+
return `${CONVERSATIONS_PATH}/${seg(id)}${route}`;
|
|
66
|
+
}
|
|
67
|
+
async reply(client, method, p, opts = {}) {
|
|
68
|
+
let got;
|
|
69
|
+
try {
|
|
70
|
+
got = await client.stateReply(method, p, opts);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
if (err instanceof FacesAPIError)
|
|
74
|
+
this.error(`HTTP ${err.statusCode}: ${err.message}`);
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
return this.show(got);
|
|
78
|
+
}
|
|
79
|
+
show(got) {
|
|
80
|
+
const { data } = got;
|
|
81
|
+
if (!got.ok)
|
|
82
|
+
process.exitCode = 1;
|
|
83
|
+
if (this.jsonEnabled())
|
|
84
|
+
return got.ok ? data : { ...data, error: { message: data.error, status: got.status } };
|
|
85
|
+
const text = typeof data.text === 'string' ? data.text : JSON.stringify(data);
|
|
86
|
+
if (got.ok)
|
|
87
|
+
this.log(text);
|
|
88
|
+
else
|
|
89
|
+
process.stderr.write(text + '\n');
|
|
90
|
+
return data;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function compact(body) {
|
|
94
|
+
return Object.fromEntries(Object.entries(body).filter(([, v]) => v !== undefined));
|
|
95
|
+
}
|
|
96
|
+
export const formatFlags = {
|
|
97
|
+
problog: Flags.boolean({ description: 'The proof as runnable ProbLog.', default: false, exclusive: ['dot'] }),
|
|
98
|
+
dot: Flags.boolean({ description: 'The proof as Graphviz dot.', default: false, exclusive: ['problog'] }),
|
|
99
|
+
};
|
|
100
|
+
export function formatOf(flags, json) {
|
|
101
|
+
if (json)
|
|
102
|
+
return 'json';
|
|
103
|
+
if (flags.problog)
|
|
104
|
+
return 'problog';
|
|
105
|
+
if (flags.dot)
|
|
106
|
+
return 'dot';
|
|
107
|
+
return 'text';
|
|
108
|
+
}
|
|
109
|
+
export const faceFlag = {
|
|
110
|
+
face: Flags.string({ description: 'The face that does it: @alias (faces face:list --for "<the task>" ranks them).', required: true }),
|
|
111
|
+
};
|
|
112
|
+
export function faceRef(face) {
|
|
113
|
+
const f = face.trim();
|
|
114
|
+
return f.startsWith('@') || f.startsWith('(') ? f : '@' + f;
|
|
115
|
+
}
|
package/dist/refine.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { FacesClient } from './client.js';
|
|
2
|
+
export declare const AND: string;
|
|
3
|
+
export declare const AND_LEAD = "It holds only if:";
|
|
4
|
+
export declare const OR: string;
|
|
5
|
+
export declare const OR_LEAD = "Take it to mean:";
|
|
6
|
+
export declare const NEEDED = "Is \"{k}: {v}\" needed for \"{P}\"? It is needed if \"{P}\" would be false without it.";
|
|
7
|
+
export declare const GATE: string;
|
|
8
|
+
export declare const GAP_ROUNDS = 3;
|
|
9
|
+
export declare const GAP_PROPOSE: string;
|
|
10
|
+
export declare const GAP_MAX = 4;
|
|
11
|
+
export declare const KIND = "Before anything is done, \"{P}\" has to be settled, and the choice here is how it will be settled.";
|
|
12
|
+
export declare const KIND_OPTIONS: Array<[Kind, string, string]>;
|
|
13
|
+
export declare const TEST: string;
|
|
14
|
+
export declare const TEST_LEAD = "Passes only if:";
|
|
15
|
+
export declare const AWAIT_QUESTION: string;
|
|
16
|
+
export declare const AWAIT_MAX = 2;
|
|
17
|
+
export declare const WHO = "the human";
|
|
18
|
+
export declare const RECHECK: string;
|
|
19
|
+
export declare const NAME_LINE = "Name each option by what it is, in a few words (e.g. the dishes themselves), and put the full statement in its description.";
|
|
20
|
+
export declare function named(question: string): string;
|
|
21
|
+
export declare const SAME = "Is \"{a}\" the same condition as \"{b}\"?";
|
|
22
|
+
export declare const AT = 0.5;
|
|
23
|
+
export declare const CLEAR_P = 0.6;
|
|
24
|
+
export declare const CLEAR_MARGIN = 0.2;
|
|
25
|
+
export declare const SIMILAR_AT = 0.85;
|
|
26
|
+
export declare const DECISION_MODEL: string;
|
|
27
|
+
export declare const CONDITION_MODEL: string;
|
|
28
|
+
export type Kind = 'and' | 'or' | 'test' | 'human';
|
|
29
|
+
export type Option = {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function fill(t: string, vars: Record<string, string>): string;
|
|
34
|
+
export declare function stripLead(text: string, lead: string): string;
|
|
35
|
+
export declare function plainKey(text: string, limit?: number): string;
|
|
36
|
+
export declare function stripOptions(opts: Option[], lead?: string): Option[];
|
|
37
|
+
export declare function readsOf(definition: string): {
|
|
38
|
+
statement: string;
|
|
39
|
+
reads: string[];
|
|
40
|
+
};
|
|
41
|
+
export declare function asked(o: Option): string;
|
|
42
|
+
export declare function condition(template: string, p: string, o: Option): string;
|
|
43
|
+
export declare function clearWinner(probs: Record<string, number>): string | undefined;
|
|
44
|
+
export type Premise = {
|
|
45
|
+
key: string;
|
|
46
|
+
number: string | null;
|
|
47
|
+
value: unknown;
|
|
48
|
+
role: string | null;
|
|
49
|
+
origin: string | null;
|
|
50
|
+
status: string;
|
|
51
|
+
truth: string;
|
|
52
|
+
op: string | null;
|
|
53
|
+
children: string[];
|
|
54
|
+
on: string[];
|
|
55
|
+
refinement: string | null;
|
|
56
|
+
await: string | null;
|
|
57
|
+
messages?: Array<{
|
|
58
|
+
kind: string | null;
|
|
59
|
+
author: string;
|
|
60
|
+
}>;
|
|
61
|
+
};
|
|
62
|
+
export declare class Loop {
|
|
63
|
+
readonly client: FacesClient;
|
|
64
|
+
readonly sid: string;
|
|
65
|
+
readonly face: string;
|
|
66
|
+
readonly as?: string | undefined;
|
|
67
|
+
readonly log: (line: string) => void;
|
|
68
|
+
calls: number;
|
|
69
|
+
usage: {
|
|
70
|
+
input_tokens: number;
|
|
71
|
+
output_tokens: number;
|
|
72
|
+
};
|
|
73
|
+
constructor(client: FacesClient, sid: string, face: string, as?: string | undefined, log?: (line: string) => void);
|
|
74
|
+
private state;
|
|
75
|
+
send(method: 'GET' | 'POST' | 'PATCH' | 'DELETE', p: string, opts?: {
|
|
76
|
+
body?: Record<string, unknown>;
|
|
77
|
+
params?: Record<string, string>;
|
|
78
|
+
}): Promise<Record<string, unknown>>;
|
|
79
|
+
premises(): Promise<Record<string, Premise>>;
|
|
80
|
+
propose(key: string, question: string | undefined, max?: number, see?: boolean): Promise<Option[]>;
|
|
81
|
+
ownQuestion(key: string): Promise<string>;
|
|
82
|
+
write(into: string, kind: 'and' | 'or', options: Option[]): Promise<{
|
|
83
|
+
new: string[];
|
|
84
|
+
matched: string[];
|
|
85
|
+
}>;
|
|
86
|
+
sameP: Map<string, number>;
|
|
87
|
+
sameElsewhere(into: string, options: Option[], P: Record<string, Premise>): Promise<Option[]>;
|
|
88
|
+
decide(questions: Array<Record<string, unknown>>, dependsOn: string[], model?: string): Promise<Array<Record<string, unknown>>>;
|
|
89
|
+
predicates(texts: string[], dependsOn: string[], model?: string): Promise<number[]>;
|
|
90
|
+
choice(name: string, instructions: string, values: Array<[string, string]>, dependsOn: string[]): Promise<Record<string, number>>;
|
|
91
|
+
kind(key: string): Promise<[Kind, number]>;
|
|
92
|
+
judged(key: string, opts: Option[]): Promise<[Option[], string[]]>;
|
|
93
|
+
question(key: string, also?: string[]): Promise<string>;
|
|
94
|
+
edit(key: string, body: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
95
|
+
add(body: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
96
|
+
search(text: string, within?: string): Promise<Array<[string, number]>>;
|
|
97
|
+
awaitHuman(key: string, also?: string[]): Promise<string>;
|
|
98
|
+
weigh(key: string): Promise<Record<string, number>>;
|
|
99
|
+
}
|
|
100
|
+
export declare function refine(L: Loop, key: string): Promise<string[]>;
|
|
101
|
+
export declare function rank(L: Loop, key: string, awaitOnTie?: boolean): Promise<string[]>;
|
|
102
|
+
export declare function refineTree(L: Loop, root: string, depth: number, log?: (line: string) => void): Promise<{
|
|
103
|
+
refined: string[];
|
|
104
|
+
awaits: string[];
|
|
105
|
+
beyond: string[];
|
|
106
|
+
}>;
|