@faces-sh/cli 1.0.1 → 1.0.3

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.
Files changed (35) hide show
  1. package/README.md +32 -7
  2. package/dist/commands/causes.d.ts +6 -2
  3. package/dist/commands/causes.js +11 -0
  4. package/dist/commands/chat/chat.d.ts +12 -7
  5. package/dist/commands/chat/chat.js +19 -6
  6. package/dist/commands/chat/messages.d.ts +10 -6
  7. package/dist/commands/chat/messages.js +10 -1
  8. package/dist/commands/chat/responses.d.ts +10 -6
  9. package/dist/commands/chat/responses.js +10 -1
  10. package/dist/commands/chat/thread.d.ts +17 -10
  11. package/dist/commands/chat/thread.js +20 -6
  12. package/dist/commands/conversation/add.d.ts +18 -0
  13. package/dist/commands/conversation/add.js +66 -0
  14. package/dist/commands/conversation/close.d.ts +16 -0
  15. package/dist/commands/conversation/close.js +36 -0
  16. package/dist/commands/conversation/compact.d.ts +16 -0
  17. package/dist/commands/conversation/compact.js +69 -0
  18. package/dist/commands/conversation/create.d.ts +19 -0
  19. package/dist/commands/conversation/create.js +76 -0
  20. package/dist/commands/conversation/get.d.ts +15 -0
  21. package/dist/commands/conversation/get.js +35 -0
  22. package/dist/commands/conversation/items.d.ts +15 -0
  23. package/dist/commands/conversation/items.js +56 -0
  24. package/dist/commands/decide.d.ts +8 -4
  25. package/dist/commands/decide.js +10 -0
  26. package/dist/commands/session/close.js +1 -1
  27. package/dist/commands/session/compact.js +1 -1
  28. package/dist/commands/session/create.js +1 -1
  29. package/dist/commands/session/get.js +1 -1
  30. package/dist/conversation.d.ts +63 -0
  31. package/dist/conversation.js +246 -0
  32. package/dist/utils.d.ts +1 -1
  33. package/dist/utils.js +4 -3
  34. package/oclif.manifest.json +1501 -813
  35. package/package.json +4 -1
@@ -0,0 +1,246 @@
1
+ import { Flags } from '@oclif/core';
2
+ export const CONVERSATIONS_PATH = '/v1/conversations';
3
+ export const MAX_CONVERSATION_ITEMS = 20;
4
+ const PREMISE_KEY = /^[A-Za-z0-9_-]{1,64}$/;
5
+ export const CONVERSATION_FLAG_DESCRIPTION = 'Add this turn to a conversation, by id (from faces conversation:create). Send only the new message: ' +
6
+ 'the conversation so far is read before it. A conversation opened with one face takes only that face.';
7
+ export const PREMISE_FLAG_DESCRIPTION = 'A premise, as key=value (repeatable): a named fact the face\'s answers depend on, kept on the ' +
8
+ 'conversation. The key is 1 to 64 letters, digits, _ or -. The value is read as JSON when it is JSON ' +
9
+ '(50000, true, null, ["a","b"], {"max":3}) and as text otherwise, so plan=annual is the text "annual". ' +
10
+ 'Quote it to keep a number as text: --premise \'code="007"\'. A newer value of a key replaces it from ' +
11
+ 'then on. A key used for the first time needs --premise-description.';
12
+ export const PREMISE_DESCRIPTION_FLAG_DESCRIPTION = 'What a --premise means, in plain words, as key="text" (repeatable, one per key), e.g. ' +
13
+ '--premise-description budget_usd="the most we can spend this year". Required the first time a key is ' +
14
+ 'used in a conversation, and kept after that.';
15
+ export const CAUSES_VISIBLE_TURN_FLAG_DESCRIPTION = 'Whether the face reads the other faces\' causes on this turn, in a conversation with several faces. ' +
16
+ 'Leave it out to follow the conversation (see faces conversation:create --no-causes-visible).';
17
+ export function conversationTurnFlags(opts = {}) {
18
+ const d = (text) => (opts.suffix ? `${text} ${opts.suffix}` : text);
19
+ const exclusive = opts.exclusive;
20
+ return {
21
+ conversation: Flags.string({ description: d(CONVERSATION_FLAG_DESCRIPTION), exclusive }),
22
+ premise: Flags.string({ description: d(`${PREMISE_FLAG_DESCRIPTION} Needs --conversation.`), multiple: true, exclusive }),
23
+ 'premise-description': Flags.string({ description: d(PREMISE_DESCRIPTION_FLAG_DESCRIPTION), multiple: true, dependsOn: ['premise'], exclusive }),
24
+ 'causes-visible': Flags.boolean({ description: d(`${CAUSES_VISIBLE_TURN_FLAG_DESCRIPTION} Needs --conversation.`), allowNo: true, exclusive }),
25
+ };
26
+ }
27
+ export function conversationLines(c) {
28
+ const pairs = [
29
+ ['conversation', c.id],
30
+ ['face', c.model ?? (c.object === 'conversation' ? 'any of your faces' : undefined)],
31
+ ['status', c.status],
32
+ ['turns', c.turns],
33
+ ['items', c.items],
34
+ ['causes', c.causes_visible === undefined ? undefined : c.causes_visible ? 'each face reads the other faces\' causes' : 'each face reads its own only'],
35
+ ['created', c.created_at],
36
+ ['last used', c.last_used_at],
37
+ ['expires', c.expires_at],
38
+ ];
39
+ return pairs.filter(([, v]) => v !== undefined && v !== null).map(([k, v]) => `${`${k}:`.padEnd(14)}${v}`);
40
+ }
41
+ export function conversationHint(err) {
42
+ if (err.statusCode === 404) {
43
+ return 'A conversation expires 30 days after it was last used, and a closed one cannot be used. ' +
44
+ 'Start another with: faces conversation:create';
45
+ }
46
+ const fresh = err.statusCode === 422 ? /premise '([A-Za-z0-9_-]{1,64})' is new/.exec(err.message) : null;
47
+ if (fresh)
48
+ return `Say what it means: --premise-description ${fresh[1]}="what it means, in plain words"`;
49
+ return undefined;
50
+ }
51
+ export function premiseValue(raw) {
52
+ if (raw.trim() === '')
53
+ return raw;
54
+ try {
55
+ const v = JSON.parse(raw);
56
+ if (typeof v === 'number' && !Number.isFinite(v))
57
+ return raw;
58
+ return v;
59
+ }
60
+ catch {
61
+ return raw;
62
+ }
63
+ }
64
+ function splitPair(raw, flag, shape) {
65
+ const i = raw.indexOf('=');
66
+ if (i <= 0)
67
+ throw new Error(`${flag} takes ${shape} (got '${raw}').`);
68
+ return [raw.slice(0, i).trim(), raw.slice(i + 1)];
69
+ }
70
+ function checkKey(key, flag) {
71
+ if (!PREMISE_KEY.test(key))
72
+ throw new Error(`${flag}: a premise key is 1 to 64 letters, digits, _ or - (got '${key}').`);
73
+ }
74
+ export function parsePremises(premises = [], descriptions = []) {
75
+ const out = [];
76
+ const seen = new Map();
77
+ for (const raw of premises) {
78
+ const [key, value] = splitPair(raw, '--premise', 'key=value');
79
+ checkKey(key, '--premise');
80
+ if (seen.has(key))
81
+ throw new Error(`--premise sets '${key}' twice. Give each key once per request.`);
82
+ const p = { key, value: premiseValue(value) };
83
+ seen.set(key, p);
84
+ out.push(p);
85
+ }
86
+ const described = new Set();
87
+ for (const raw of descriptions) {
88
+ const [key, text] = splitPair(raw, '--premise-description', 'key="what it means"');
89
+ checkKey(key, '--premise-description');
90
+ const p = seen.get(key);
91
+ if (!p)
92
+ throw new Error(`--premise-description names '${key}', which no --premise sets. Add --premise ${key}=<value>.`);
93
+ if (described.has(key))
94
+ throw new Error(`--premise-description describes '${key}' twice. Give one description per key.`);
95
+ if (text.trim() === '')
96
+ throw new Error(`--premise-description for '${key}' is empty. Say what it means, in plain words.`);
97
+ described.add(key);
98
+ p.description = text.trim();
99
+ }
100
+ return out;
101
+ }
102
+ export function conversationFields(flags) {
103
+ const out = {};
104
+ if (flags.conversation && flags.session && flags.conversation !== flags.session) {
105
+ throw new Error('--session is the old name for --conversation: pass one of them, not two different ids.');
106
+ }
107
+ const premises = parsePremises(flags.premise, flags['premise-description']);
108
+ const inConversation = Boolean(flags.conversation || flags.session);
109
+ if (!inConversation && (premises.length > 0 || flags['causes-visible'] !== undefined)) {
110
+ throw new Error('Premises and --causes-visible belong to a conversation: add --conversation <id> (from faces conversation:create).');
111
+ }
112
+ if (flags.conversation)
113
+ out.conversation = flags.conversation;
114
+ if (premises.length > 0)
115
+ out.premises = premises;
116
+ if (flags['causes-visible'] !== undefined)
117
+ out.causes_visible = flags['causes-visible'];
118
+ return out;
119
+ }
120
+ export function clientItems(messages = [], premises = []) {
121
+ return [...premises.map((p) => ({ ...p })), ...messages.map((content) => ({ type: 'message', role: 'user', content }))];
122
+ }
123
+ export function shownValue(v) {
124
+ return v === undefined ? '-' : JSON.stringify(v);
125
+ }
126
+ export function premiseLines(premises) {
127
+ const entries = Object.entries(premises ?? {});
128
+ if (entries.length === 0)
129
+ return ['premises: none'];
130
+ const lines = ['premises:'];
131
+ for (const [key, p] of entries) {
132
+ lines.push(` ${key} = ${shownValue(p?.value)}${p?.description ? ` (${p.description})` : ''}`);
133
+ }
134
+ return lines;
135
+ }
136
+ function textOf(content) {
137
+ if (typeof content === 'string')
138
+ return content;
139
+ if (Array.isArray(content)) {
140
+ return content
141
+ .map((part) => (part && typeof part === 'object' ? String(part.text ?? '') : String(part ?? '')))
142
+ .join('');
143
+ }
144
+ return content === undefined || content === null ? '' : JSON.stringify(content);
145
+ }
146
+ function parsed(raw) {
147
+ if (raw && typeof raw === 'object')
148
+ return raw;
149
+ if (typeof raw !== 'string')
150
+ return {};
151
+ try {
152
+ const v = JSON.parse(raw);
153
+ return v && typeof v === 'object' ? v : {};
154
+ }
155
+ catch {
156
+ return {};
157
+ }
158
+ }
159
+ function questionText(q) {
160
+ return String(q.instructions ?? q.question ?? q.name ?? q.id ?? '?');
161
+ }
162
+ function choiceOf(out) {
163
+ if (out.choice !== undefined && out.choice !== null)
164
+ return String(out.choice);
165
+ const answer = out.answer;
166
+ if (answer && typeof answer === 'object') {
167
+ const a = answer;
168
+ for (const k of ['choice', 'value', 'score', 'answer'])
169
+ if (a[k] !== undefined && a[k] !== null)
170
+ return String(a[k]);
171
+ }
172
+ return answer === undefined || answer === null ? '-' : String(answer);
173
+ }
174
+ export function itemLines(items) {
175
+ const calls = new Map();
176
+ const lines = [];
177
+ for (const it of items) {
178
+ const id = String(it.id ?? '');
179
+ const head = id ? `${id.padEnd(7)}` : '';
180
+ const pad = ' '.repeat(head.length);
181
+ if (it.type === 'function_call') {
182
+ calls.set(String(it.call_id ?? ''), it);
183
+ continue;
184
+ }
185
+ if (it.type === 'decision') {
186
+ const answer = (it.answer && typeof it.answer === 'object' ? it.answer : {});
187
+ const question = (it.question && typeof it.question === 'object' ? it.question : {});
188
+ const result = answer.result === undefined || answer.result === null ? '-' : String(answer.result);
189
+ lines.push(`${head}${String(it.face ?? 'face')}${it.model ? ` (${it.model})` : ''} decided: ${questionText(question)} -> ${result}`);
190
+ if (typeof it.cause === 'string' && it.cause)
191
+ lines.push(`${pad}cause: ${it.cause}`);
192
+ continue;
193
+ }
194
+ if (it.type === 'function_call_output') {
195
+ const call = calls.get(String(it.call_id ?? ''));
196
+ const out = parsed(it.output);
197
+ if (call?.name === 'premise') {
198
+ const key = String(parsed(call.arguments).key ?? '?');
199
+ const desc = typeof out.description === 'string' && out.description ? ` (${out.description})` : '';
200
+ lines.push(`${head}premise ${key} = ${shownValue(out.value)}${desc}`);
201
+ }
202
+ else if (call?.name === 'decision') {
203
+ const who = String(it.face ?? call.face ?? 'face');
204
+ const model = it.model ?? call.model;
205
+ lines.push(`${head}${who}${model ? ` (${model})` : ''} decided: ${questionText(parsed(call.arguments))} -> ${choiceOf(out)}`);
206
+ if (typeof out.cause === 'string' && out.cause)
207
+ lines.push(`${pad}cause: ${out.cause}`);
208
+ }
209
+ else {
210
+ lines.push(`${head}${String(call?.name ?? 'output')}: ${typeof it.output === 'string' ? it.output : JSON.stringify(it.output)}`);
211
+ }
212
+ continue;
213
+ }
214
+ if (it.type === 'message' || it.role !== undefined) {
215
+ const who = it.role === 'user' ? 'you' : `${String(it.name ?? it.role ?? '?')}${it.model ? ` (${it.model})` : ''}`;
216
+ const text = textOf(it.content);
217
+ const cause = typeof it.cause === 'string' && it.cause ? it.cause : '';
218
+ if (!text && cause) {
219
+ lines.push(`${head}${who}'s cause: ${cause}`);
220
+ continue;
221
+ }
222
+ lines.push(`${head}${who}: ${text}`);
223
+ if (cause)
224
+ lines.push(`${pad}cause: ${cause}`);
225
+ continue;
226
+ }
227
+ lines.push(`${head}${String(it.type ?? 'item')}`);
228
+ }
229
+ return lines;
230
+ }
231
+ export function staleDecisionLines(stale) {
232
+ if (!stale || stale.length === 0)
233
+ return [];
234
+ const lines = [
235
+ `${stale.length === 1 ? '1 earlier decision was' : `${stale.length} earlier decisions were`} made under a value you just changed. Nothing was decided again:`,
236
+ ];
237
+ for (const d of stale) {
238
+ const q = d.question ? questionText(d.question) : '?';
239
+ const choice = d.choice === undefined || d.choice === null ? '-' : String(d.choice);
240
+ lines.push(` ${d.face ?? 'face'}: ${q} -> ${choice}${d.call_id ? ` [${d.call_id}]` : ''}`);
241
+ for (const [key, c] of Object.entries(d.changed ?? {})) {
242
+ lines.push(` ${key}: was ${shownValue(c?.was)}, now ${shownValue(c?.now)}`);
243
+ }
244
+ }
245
+ return lines;
246
+ }
package/dist/utils.d.ts CHANGED
@@ -55,7 +55,7 @@ export declare function isMaxTokensRenameError(message: string): boolean;
55
55
  export declare const DOCUMENT_VS_THREAD: string;
56
56
  export declare const STYLE_FLAG_DESCRIPTION: string;
57
57
  export declare const MEDIUM_FLAG_DESCRIPTION: string;
58
- export declare const SESSION_FLAG_DESCRIPTION = "Add this turn to a session, by id (from faces session:create). The session must be with this face.";
58
+ export declare const SESSION_FLAG_DESCRIPTION: string;
59
59
  export declare const COMPACTION_FLAG_DESCRIPTION: string;
60
60
  export declare function chatExtras(flags: {
61
61
  style?: string;
package/dist/utils.js CHANGED
@@ -426,9 +426,10 @@ export const STYLE_FLAG_DESCRIPTION = 'Apply the style the face has built for th
426
426
  export const MEDIUM_FLAG_DESCRIPTION = 'What the reply is, so it is shaped for it: email, text message, social post, essay, academic paper, ' +
427
427
  'blog post, legal document, thread reply, conversation, lecture, other. Needs no built style. Leave it ' +
428
428
  'out and the medium is inferred from your message. With --style, leave it out or give the same medium.';
429
- export const SESSION_FLAG_DESCRIPTION = 'Add this turn to a session, by id (from faces session:create). The session must be with this face.';
430
- export const COMPACTION_FLAG_DESCRIPTION = 'Read a compaction, by id (from faces session:compact), alongside the messages sent. It must be of a ' +
431
- 'session with this face.';
429
+ export const SESSION_FLAG_DESCRIPTION = 'The old name for --conversation, still accepted: add this turn to a session (a conversation), by id ' +
430
+ '(from faces session:create or conversation:create). The session must be with this face.';
431
+ export const COMPACTION_FLAG_DESCRIPTION = 'Read a compaction, by id (from faces conversation:compact or session:compact), alongside the messages ' +
432
+ 'sent. It must be of this face.';
432
433
  export function chatExtras(flags) {
433
434
  const out = {};
434
435
  if (flags.style)