@faces-sh/cli 0.0.0-stage → 1.0.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.
Files changed (290) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +158 -2
  3. package/bin/dev.js +5 -0
  4. package/bin/run.js +5 -0
  5. package/dist/base.d.ts +26 -0
  6. package/dist/base.js +181 -0
  7. package/dist/benchmark.d.ts +21 -0
  8. package/dist/benchmark.js +95 -0
  9. package/dist/billing.d.ts +17 -0
  10. package/dist/billing.js +101 -0
  11. package/dist/catalog.d.ts +35 -0
  12. package/dist/catalog.js +242 -0
  13. package/dist/client.d.ts +98 -0
  14. package/dist/client.js +590 -0
  15. package/dist/commands/account/preferences.d.ts +20 -0
  16. package/dist/commands/account/preferences.js +190 -0
  17. package/dist/commands/account/state.d.ts +11 -0
  18. package/dist/commands/account/state.js +24 -0
  19. package/dist/commands/auth/connect.d.ts +17 -0
  20. package/dist/commands/auth/connect.js +190 -0
  21. package/dist/commands/auth/connections.d.ts +12 -0
  22. package/dist/commands/auth/connections.js +40 -0
  23. package/dist/commands/auth/disconnect.d.ts +15 -0
  24. package/dist/commands/auth/disconnect.js +49 -0
  25. package/dist/commands/auth/login.d.ts +13 -0
  26. package/dist/commands/auth/login.js +42 -0
  27. package/dist/commands/auth/logout.d.ts +11 -0
  28. package/dist/commands/auth/logout.js +14 -0
  29. package/dist/commands/auth/refresh.d.ts +11 -0
  30. package/dist/commands/auth/refresh.js +30 -0
  31. package/dist/commands/auth/register.d.ts +15 -0
  32. package/dist/commands/auth/register.js +98 -0
  33. package/dist/commands/auth/whoami.d.ts +11 -0
  34. package/dist/commands/auth/whoami.js +108 -0
  35. package/dist/commands/benchmark/get.d.ts +16 -0
  36. package/dist/commands/benchmark/get.js +42 -0
  37. package/dist/commands/benchmark/list.d.ts +13 -0
  38. package/dist/commands/benchmark/list.js +47 -0
  39. package/dist/commands/benchmark/start.d.ts +22 -0
  40. package/dist/commands/benchmark/start.js +80 -0
  41. package/dist/commands/billing/balance.d.ts +11 -0
  42. package/dist/commands/billing/balance.js +27 -0
  43. package/dist/commands/billing/card/default.d.ts +15 -0
  44. package/dist/commands/billing/card/default.js +33 -0
  45. package/dist/commands/billing/card/remove.d.ts +16 -0
  46. package/dist/commands/billing/card/remove.js +41 -0
  47. package/dist/commands/billing/card-setup.d.ts +13 -0
  48. package/dist/commands/billing/card-setup.js +41 -0
  49. package/dist/commands/billing/cards.d.ts +11 -0
  50. package/dist/commands/billing/cards.js +34 -0
  51. package/dist/commands/billing/llm-costs.d.ts +14 -0
  52. package/dist/commands/billing/llm-costs.js +95 -0
  53. package/dist/commands/billing/subscription/activate.d.ts +14 -0
  54. package/dist/commands/billing/subscription/activate.js +91 -0
  55. package/dist/commands/billing/subscription/cancel.d.ts +12 -0
  56. package/dist/commands/billing/subscription/cancel.js +54 -0
  57. package/dist/commands/billing/subscription/index.d.ts +11 -0
  58. package/dist/commands/billing/subscription/index.js +46 -0
  59. package/dist/commands/billing/subscription/plan.d.ts +16 -0
  60. package/dist/commands/billing/subscription/plan.js +86 -0
  61. package/dist/commands/billing/topup.d.ts +16 -0
  62. package/dist/commands/billing/topup.js +60 -0
  63. package/dist/commands/billing/usage.d.ts +15 -0
  64. package/dist/commands/billing/usage.js +68 -0
  65. package/dist/commands/catalog/backup.d.ts +11 -0
  66. package/dist/commands/catalog/backup.js +169 -0
  67. package/dist/commands/catalog/doctor.d.ts +14 -0
  68. package/dist/commands/catalog/doctor.js +303 -0
  69. package/dist/commands/catalog/list.d.ts +11 -0
  70. package/dist/commands/catalog/list.js +39 -0
  71. package/dist/commands/catalog/restore.d.ts +17 -0
  72. package/dist/commands/catalog/restore.js +259 -0
  73. package/dist/commands/causes.d.ts +27 -0
  74. package/dist/commands/causes.js +135 -0
  75. package/dist/commands/chat/chat.d.ts +42 -0
  76. package/dist/commands/chat/chat.js +363 -0
  77. package/dist/commands/chat/messages.d.ts +26 -0
  78. package/dist/commands/chat/messages.js +139 -0
  79. package/dist/commands/chat/responses.d.ts +27 -0
  80. package/dist/commands/chat/responses.js +150 -0
  81. package/dist/commands/chat/thread.d.ts +43 -0
  82. package/dist/commands/chat/thread.js +458 -0
  83. package/dist/commands/compile/all.d.ts +14 -0
  84. package/dist/commands/compile/all.js +186 -0
  85. package/dist/commands/compile/doc/create.d.ts +19 -0
  86. package/dist/commands/compile/doc/create.js +58 -0
  87. package/dist/commands/compile/doc/delete.d.ts +15 -0
  88. package/dist/commands/compile/doc/delete.js +33 -0
  89. package/dist/commands/compile/doc/edit.d.ts +19 -0
  90. package/dist/commands/compile/doc/edit.js +59 -0
  91. package/dist/commands/compile/doc/get.d.ts +15 -0
  92. package/dist/commands/compile/doc/get.js +43 -0
  93. package/dist/commands/compile/doc/index.d.ts +24 -0
  94. package/dist/commands/compile/doc/index.js +211 -0
  95. package/dist/commands/compile/doc/list.d.ts +15 -0
  96. package/dist/commands/compile/doc/list.js +46 -0
  97. package/dist/commands/compile/doc/make.d.ts +17 -0
  98. package/dist/commands/compile/doc/make.js +95 -0
  99. package/dist/commands/compile/doc/pause.d.ts +17 -0
  100. package/dist/commands/compile/doc/pause.js +74 -0
  101. package/dist/commands/compile/doc/reset.d.ts +15 -0
  102. package/dist/commands/compile/doc/reset.js +33 -0
  103. package/dist/commands/compile/import.d.ts +20 -0
  104. package/dist/commands/compile/import.js +138 -0
  105. package/dist/commands/compile/stats.d.ts +11 -0
  106. package/dist/commands/compile/stats.js +75 -0
  107. package/dist/commands/compile/status.d.ts +17 -0
  108. package/dist/commands/compile/status.js +110 -0
  109. package/dist/commands/compile/thread/create.d.ts +16 -0
  110. package/dist/commands/compile/thread/create.js +42 -0
  111. package/dist/commands/compile/thread/delete.d.ts +15 -0
  112. package/dist/commands/compile/thread/delete.js +33 -0
  113. package/dist/commands/compile/thread/edit.d.ts +19 -0
  114. package/dist/commands/compile/thread/edit.js +120 -0
  115. package/dist/commands/compile/thread/get.d.ts +15 -0
  116. package/dist/commands/compile/thread/get.js +48 -0
  117. package/dist/commands/compile/thread/list.d.ts +14 -0
  118. package/dist/commands/compile/thread/list.js +38 -0
  119. package/dist/commands/compile/thread/make.d.ts +17 -0
  120. package/dist/commands/compile/thread/make.js +87 -0
  121. package/dist/commands/compile/thread/message.d.ts +16 -0
  122. package/dist/commands/compile/thread/message.js +43 -0
  123. package/dist/commands/compile/thread/pause.d.ts +17 -0
  124. package/dist/commands/compile/thread/pause.js +75 -0
  125. package/dist/commands/compile/thread/replace.d.ts +27 -0
  126. package/dist/commands/compile/thread/replace.js +114 -0
  127. package/dist/commands/compile/thread/reset.d.ts +15 -0
  128. package/dist/commands/compile/thread/reset.js +33 -0
  129. package/dist/commands/compile/upload.d.ts +21 -0
  130. package/dist/commands/compile/upload.js +162 -0
  131. package/dist/commands/config/clear.d.ts +12 -0
  132. package/dist/commands/config/clear.js +17 -0
  133. package/dist/commands/config/set.d.ts +15 -0
  134. package/dist/commands/config/set.js +43 -0
  135. package/dist/commands/config/show.d.ts +11 -0
  136. package/dist/commands/config/show.js +19 -0
  137. package/dist/commands/decide.d.ts +35 -0
  138. package/dist/commands/decide.js +242 -0
  139. package/dist/commands/face/attributes.d.ts +11 -0
  140. package/dist/commands/face/attributes.js +44 -0
  141. package/dist/commands/face/avatar/delete.d.ts +15 -0
  142. package/dist/commands/face/avatar/delete.js +38 -0
  143. package/dist/commands/face/avatar/get.d.ts +20 -0
  144. package/dist/commands/face/avatar/get.js +100 -0
  145. package/dist/commands/face/avatar/list.d.ts +14 -0
  146. package/dist/commands/face/avatar/list.js +56 -0
  147. package/dist/commands/face/avatar/make.d.ts +16 -0
  148. package/dist/commands/face/avatar/make.js +64 -0
  149. package/dist/commands/face/avatar/upload.d.ts +15 -0
  150. package/dist/commands/face/avatar/upload.js +67 -0
  151. package/dist/commands/face/create.d.ts +23 -0
  152. package/dist/commands/face/create.js +122 -0
  153. package/dist/commands/face/delete.d.ts +15 -0
  154. package/dist/commands/face/delete.js +39 -0
  155. package/dist/commands/face/diff.d.ts +12 -0
  156. package/dist/commands/face/diff.js +50 -0
  157. package/dist/commands/face/edit.d.ts +25 -0
  158. package/dist/commands/face/edit.js +155 -0
  159. package/dist/commands/face/get.d.ts +16 -0
  160. package/dist/commands/face/get.js +145 -0
  161. package/dist/commands/face/groups.d.ts +15 -0
  162. package/dist/commands/face/groups.js +34 -0
  163. package/dist/commands/face/list.d.ts +23 -0
  164. package/dist/commands/face/list.js +168 -0
  165. package/dist/commands/face/lock.d.ts +15 -0
  166. package/dist/commands/face/lock.js +35 -0
  167. package/dist/commands/face/neighbors.d.ts +16 -0
  168. package/dist/commands/face/neighbors.js +63 -0
  169. package/dist/commands/face/publish.d.ts +16 -0
  170. package/dist/commands/face/publish.js +42 -0
  171. package/dist/commands/face/share.d.ts +22 -0
  172. package/dist/commands/face/share.js +103 -0
  173. package/dist/commands/face/sources.d.ts +21 -0
  174. package/dist/commands/face/sources.js +169 -0
  175. package/dist/commands/face/stats.d.ts +12 -0
  176. package/dist/commands/face/stats.js +46 -0
  177. package/dist/commands/face/tag/add.d.ts +15 -0
  178. package/dist/commands/face/tag/add.js +40 -0
  179. package/dist/commands/face/tag/all.d.ts +11 -0
  180. package/dist/commands/face/tag/all.js +31 -0
  181. package/dist/commands/face/tag/list.d.ts +14 -0
  182. package/dist/commands/face/tag/list.js +36 -0
  183. package/dist/commands/face/tag/remove.d.ts +16 -0
  184. package/dist/commands/face/tag/remove.js +44 -0
  185. package/dist/commands/face/tag/set.d.ts +15 -0
  186. package/dist/commands/face/tag/set.js +40 -0
  187. package/dist/commands/face/unlock.d.ts +15 -0
  188. package/dist/commands/face/unlock.js +35 -0
  189. package/dist/commands/face/unpublish.d.ts +14 -0
  190. package/dist/commands/face/unpublish.js +31 -0
  191. package/dist/commands/face/unshare.d.ts +18 -0
  192. package/dist/commands/face/unshare.js +66 -0
  193. package/dist/commands/fidelity/get.d.ts +15 -0
  194. package/dist/commands/fidelity/get.js +42 -0
  195. package/dist/commands/fidelity/pick.d.ts +19 -0
  196. package/dist/commands/fidelity/pick.js +70 -0
  197. package/dist/commands/fidelity/run.d.ts +20 -0
  198. package/dist/commands/fidelity/run.js +99 -0
  199. package/dist/commands/fidelity/save-thread.d.ts +15 -0
  200. package/dist/commands/fidelity/save-thread.js +35 -0
  201. package/dist/commands/fidelity/start.d.ts +17 -0
  202. package/dist/commands/fidelity/start.js +56 -0
  203. package/dist/commands/fidelity/turn.d.ts +17 -0
  204. package/dist/commands/fidelity/turn.js +48 -0
  205. package/dist/commands/group/add.d.ts +16 -0
  206. package/dist/commands/group/add.js +83 -0
  207. package/dist/commands/group/create.d.ts +16 -0
  208. package/dist/commands/group/create.js +68 -0
  209. package/dist/commands/group/delete.d.ts +15 -0
  210. package/dist/commands/group/delete.js +39 -0
  211. package/dist/commands/group/edit.d.ts +18 -0
  212. package/dist/commands/group/edit.js +75 -0
  213. package/dist/commands/group/get.d.ts +14 -0
  214. package/dist/commands/group/get.js +29 -0
  215. package/dist/commands/group/list.d.ts +11 -0
  216. package/dist/commands/group/list.js +39 -0
  217. package/dist/commands/group/members.d.ts +15 -0
  218. package/dist/commands/group/members.js +68 -0
  219. package/dist/commands/group/remove.d.ts +15 -0
  220. package/dist/commands/group/remove.js +55 -0
  221. package/dist/commands/group/tag/add.d.ts +15 -0
  222. package/dist/commands/group/tag/add.js +30 -0
  223. package/dist/commands/group/tag/list.d.ts +14 -0
  224. package/dist/commands/group/tag/list.js +36 -0
  225. package/dist/commands/group/tag/remove.d.ts +15 -0
  226. package/dist/commands/group/tag/remove.js +30 -0
  227. package/dist/commands/group/tag/set.d.ts +15 -0
  228. package/dist/commands/group/tag/set.js +30 -0
  229. package/dist/commands/keys/create.d.ts +17 -0
  230. package/dist/commands/keys/create.js +72 -0
  231. package/dist/commands/keys/list.d.ts +11 -0
  232. package/dist/commands/keys/list.js +24 -0
  233. package/dist/commands/keys/revoke.d.ts +15 -0
  234. package/dist/commands/keys/revoke.js +49 -0
  235. package/dist/commands/keys/update.d.ts +17 -0
  236. package/dist/commands/keys/update.js +41 -0
  237. package/dist/commands/models/list.d.ts +15 -0
  238. package/dist/commands/models/list.js +98 -0
  239. package/dist/commands/session/close.d.ts +16 -0
  240. package/dist/commands/session/close.js +36 -0
  241. package/dist/commands/session/compact.d.ts +15 -0
  242. package/dist/commands/session/compact.js +58 -0
  243. package/dist/commands/session/create.d.ts +15 -0
  244. package/dist/commands/session/create.js +45 -0
  245. package/dist/commands/session/get.d.ts +15 -0
  246. package/dist/commands/session/get.js +34 -0
  247. package/dist/commands/style/delete.d.ts +19 -0
  248. package/dist/commands/style/delete.js +92 -0
  249. package/dist/commands/style/make.d.ts +28 -0
  250. package/dist/commands/style/make.js +298 -0
  251. package/dist/commands/style/revert.d.ts +17 -0
  252. package/dist/commands/style/revert.js +66 -0
  253. package/dist/commands/style/status.d.ts +20 -0
  254. package/dist/commands/style/status.js +109 -0
  255. package/dist/commands/style/upload.d.ts +19 -0
  256. package/dist/commands/style/upload.js +100 -0
  257. package/dist/commands/style/versions.d.ts +15 -0
  258. package/dist/commands/style/versions.js +104 -0
  259. package/dist/commands/user/follow.d.ts +14 -0
  260. package/dist/commands/user/follow.js +38 -0
  261. package/dist/commands/user/followers.d.ts +14 -0
  262. package/dist/commands/user/followers.js +50 -0
  263. package/dist/commands/user/following.d.ts +14 -0
  264. package/dist/commands/user/following.js +50 -0
  265. package/dist/commands/user/get.d.ts +14 -0
  266. package/dist/commands/user/get.js +49 -0
  267. package/dist/commands/user/unfollow.d.ts +14 -0
  268. package/dist/commands/user/unfollow.js +38 -0
  269. package/dist/config.d.ts +32 -0
  270. package/dist/config.js +139 -0
  271. package/dist/decisions.d.ts +42 -0
  272. package/dist/decisions.js +262 -0
  273. package/dist/fidelity.d.ts +50 -0
  274. package/dist/fidelity.js +92 -0
  275. package/dist/group-catalog.d.ts +22 -0
  276. package/dist/group-catalog.js +133 -0
  277. package/dist/paths.d.ts +1 -0
  278. package/dist/paths.js +8 -0
  279. package/dist/poll.d.ts +26 -0
  280. package/dist/poll.js +75 -0
  281. package/dist/routing.d.ts +15 -0
  282. package/dist/routing.js +135 -0
  283. package/dist/session.d.ts +14 -0
  284. package/dist/session.js +18 -0
  285. package/dist/style.d.ts +46 -0
  286. package/dist/style.js +285 -0
  287. package/dist/utils.d.ts +149 -0
  288. package/dist/utils.js +865 -0
  289. package/oclif.manifest.json +11070 -0
  290. package/package.json +127 -4
@@ -0,0 +1,259 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { Args, Flags } from '@oclif/core';
5
+ import { BaseCommand } from '../../base.js';
6
+ import { FacesAPIError } from '../../client.js';
7
+ import { CatalogService } from '../../catalog.js';
8
+ import { seg } from '../../utils.js';
9
+ export default class CatalogRestore extends BaseCommand {
10
+ static description = 'Restore faces, groups, and source material from a backup snapshot';
11
+ static flags = {
12
+ ...BaseCommand.baseFlags,
13
+ compile: Flags.boolean({ description: 'Run compile:all after restoring', default: false }),
14
+ };
15
+ static args = {
16
+ file: Args.string({ description: 'Backup file path (default: most recent in ~/.faces/backups/)' }),
17
+ };
18
+ async run() {
19
+ const { args, flags } = await this.parse(CatalogRestore);
20
+ const client = this.makeClient(flags);
21
+ const json = this.jsonEnabled();
22
+ const filePath = args.file ?? this.findLatestBackup();
23
+ if (!fs.existsSync(filePath))
24
+ this.error(`Backup file not found: ${filePath}`);
25
+ let backup;
26
+ try {
27
+ backup = JSON.parse(fs.readFileSync(filePath, 'utf8'));
28
+ }
29
+ catch {
30
+ this.error(`${filePath} is not a backup file: it is not valid JSON. Make one with: faces catalog:backup`);
31
+ }
32
+ if (!backup || typeof backup !== 'object' || !Array.isArray(backup.faces)) {
33
+ this.error(`${filePath} is not a faces backup: it has no list of faces. Make one with: faces catalog:backup`);
34
+ }
35
+ if (!backup.groups && Array.isArray(backup.teams)) {
36
+ backup.groups = backup.teams;
37
+ }
38
+ if (backup.version !== 1 && backup.version !== 2)
39
+ this.error(`Unsupported backup version: ${backup.version}`);
40
+ if (!json) {
41
+ this.log(`Restoring from: ${filePath}`);
42
+ this.log(` Created: ${backup.created_at}`);
43
+ this.log(` Faces: ${backup.faces.length}, Groups: ${backup.groups?.length ?? 0}\n`);
44
+ }
45
+ const catalog = new CatalogService();
46
+ let facesCreated = 0;
47
+ let facesSkipped = 0;
48
+ let docsUploaded = 0;
49
+ let threadsUploaded = 0;
50
+ let groupsCreated = 0;
51
+ const failures = [];
52
+ const skippedCorpus = [];
53
+ for (const face of backup.faces) {
54
+ if (!json)
55
+ process.stderr.write(`${face.alias}: `);
56
+ const created = await this.createFace(client, face);
57
+ if (!created) {
58
+ facesSkipped++;
59
+ if (!json)
60
+ process.stderr.write('exists, skipping\n');
61
+ continue;
62
+ }
63
+ facesCreated++;
64
+ if (face.tags && face.tags.length > 0) {
65
+ try {
66
+ await client.post(`/v1/iam/${seg(face.alias)}/tags`, { body: { tags: face.tags } });
67
+ }
68
+ catch { }
69
+ }
70
+ try {
71
+ catalog.writeFace({ alias: face.alias, name: face.name, basic_facts: face.basic_facts }, face.description ?? undefined);
72
+ }
73
+ catch { }
74
+ let docsHere = 0;
75
+ for (const doc of face.documents) {
76
+ try {
77
+ const payload = { alias: face.alias, content: doc.content };
78
+ if (doc.label)
79
+ payload.label = doc.label;
80
+ if (doc.perspective)
81
+ payload.perspective = doc.perspective;
82
+ if (doc.medium)
83
+ payload.medium = doc.medium;
84
+ await client.post('/v1/compile/documents', { body: payload });
85
+ docsUploaded++;
86
+ docsHere++;
87
+ }
88
+ catch (err) {
89
+ const msg = err instanceof FacesAPIError ? `${err.statusCode}: ${err.message}` : String(err);
90
+ const why = doc.medium ? '' : ` (the backup records no medium for "${doc.label ?? 'untitled'}"; ` +
91
+ `create it with faces compile:doc:create ${face.alias} --medium <medium>)`;
92
+ failures.push(`${face.alias}: document "${doc.label ?? 'untitled'}" (${msg})${why}`);
93
+ if (!json)
94
+ process.stderr.write(`\n warn: doc upload failed: ${msg}${why}\n `);
95
+ }
96
+ }
97
+ let threadsHere = 0;
98
+ for (const thread of face.threads) {
99
+ const label = thread.label ?? 'untitled';
100
+ if (thread.corpus) {
101
+ skippedCorpus.push(`${face.alias}: ${label}`);
102
+ continue;
103
+ }
104
+ if (thread.messages.length === 0) {
105
+ failures.push(`${face.alias}: thread "${label}" has no messages in the backup`);
106
+ continue;
107
+ }
108
+ try {
109
+ const transcript = thread.messages
110
+ .map((m) => `${(m.name || m.role).replace(/:/g, ' ')}: ${m.content.replace(/\s+/g, ' ')}`)
111
+ .join('\n');
112
+ const speaker = thread.messages.find((m) => m.role === 'user');
113
+ const params = new URLSearchParams({ type: 'thread', perspective: 'first-person' });
114
+ if (speaker)
115
+ params.set('face_speaker', (speaker.name || speaker.role).replace(/:/g, ' '));
116
+ const form = new FormData();
117
+ form.append('file', new Blob([transcript], { type: 'text/plain' }), `${label}.txt`);
118
+ const created = await client.postForm(`/v1/faces/${seg(face.alias)}/upload?${params}`, form);
119
+ const threadId = created.thread_id;
120
+ await client.patch(`/v1/compile/threads/${seg(threadId)}/messages`, { body: { messages: thread.messages } });
121
+ const meta = { label };
122
+ if (thread.medium)
123
+ meta.medium = thread.medium;
124
+ await client.patch(`/v1/compile/threads/${seg(threadId)}`, { body: meta });
125
+ threadsUploaded++;
126
+ threadsHere++;
127
+ }
128
+ catch (err) {
129
+ const msg = err instanceof FacesAPIError ? `${err.statusCode}: ${err.message}` : String(err);
130
+ failures.push(`${face.alias}: thread "${label}" (${msg})`);
131
+ if (!json)
132
+ process.stderr.write(`\n warn: thread upload failed: ${msg}\n `);
133
+ }
134
+ }
135
+ if (!json)
136
+ process.stderr.write(`${docsHere} of ${face.documents.length} doc(s), ${threadsHere} of ${face.threads.length} thread(s)\n`);
137
+ }
138
+ if (backup.groups && backup.groups.length > 0) {
139
+ if (!json)
140
+ this.log(`\nRestoring ${backup.groups.length} group(s)...`);
141
+ const existing = new Set();
142
+ try {
143
+ const resp = await client.get('/v1/me/groups');
144
+ for (const t of resp.data ?? [])
145
+ existing.add(String(t.name));
146
+ }
147
+ catch { }
148
+ for (const group of backup.groups) {
149
+ if (!json)
150
+ process.stderr.write(` ${group.name}: `);
151
+ if (existing.has(group.name)) {
152
+ if (!json)
153
+ process.stderr.write('exists, skipping\n');
154
+ continue;
155
+ }
156
+ try {
157
+ const payload = { name: group.name };
158
+ if (group.description)
159
+ payload.description = group.description;
160
+ if (group.protocol)
161
+ payload.protocol = group.protocol;
162
+ const created = await client.post('/v1/groups', { body: payload });
163
+ const groupId = created.id;
164
+ if (group.tags.length > 0) {
165
+ try {
166
+ await client.post(`/v1/groups/${seg(groupId)}/tags`, { body: { tags: group.tags } });
167
+ }
168
+ catch { }
169
+ }
170
+ for (const alias of group.members) {
171
+ try {
172
+ const face = await client.get(`/v1/faces/${seg(alias)}`);
173
+ const iamId = (face.uid ?? face.id ?? face.iam_id);
174
+ if (iamId) {
175
+ await client.post(`/v1/groups/${seg(groupId)}/members`, { body: { iam_id: iamId } });
176
+ }
177
+ }
178
+ catch { }
179
+ }
180
+ groupsCreated++;
181
+ if (!json)
182
+ process.stderr.write(`${group.members.length} member(s)\n`);
183
+ }
184
+ catch (err) {
185
+ const msg = err instanceof FacesAPIError ? `${err.statusCode}: ${err.message}` : String(err);
186
+ failures.push(`group "${group.name}" (${msg})`);
187
+ if (!json)
188
+ process.stderr.write(`failed: ${msg}\n`);
189
+ }
190
+ }
191
+ }
192
+ if (!json) {
193
+ this.log(`\nRestore complete:`);
194
+ this.log(` Faces created: ${facesCreated}, skipped: ${facesSkipped}`);
195
+ this.log(` Documents uploaded: ${docsUploaded}`);
196
+ this.log(` Threads uploaded: ${threadsUploaded}`);
197
+ if (groupsCreated > 0)
198
+ this.log(` Groups created: ${groupsCreated}`);
199
+ if (skippedCorpus.length > 0) {
200
+ this.log(` Style corpus threads not restored: ${skippedCorpus.length}. Re-upload them with faces style:upload:`);
201
+ for (const s of skippedCorpus)
202
+ this.log(` ${s}`);
203
+ }
204
+ }
205
+ if (flags.compile) {
206
+ if (!json)
207
+ this.log('\nRunning compile:all...\n');
208
+ await this.config.runCommand('compile:all', json ? ['--json'] : []);
209
+ }
210
+ const result = { faces_created: facesCreated, faces_skipped: facesSkipped, documents: docsUploaded, threads: threadsUploaded, groups_created: groupsCreated, corpus_threads_skipped: skippedCorpus, failures };
211
+ if (failures.length > 0) {
212
+ this.error(`${failures.length} item(s) were not restored:\n${failures.map((f) => ` ${f}`).join('\n')}`);
213
+ }
214
+ return result;
215
+ }
216
+ async createFace(client, face) {
217
+ const payload = { name: face.name, alias: face.alias };
218
+ if (face.description)
219
+ payload.description = face.description;
220
+ if (face.specialty)
221
+ payload.specialty = face.specialty;
222
+ if (face.profile_addendum)
223
+ payload.profile_addendum = face.profile_addendum;
224
+ if (face.formula) {
225
+ payload.formula = face.formula;
226
+ if (face.default_model)
227
+ payload.default_model = face.default_model;
228
+ }
229
+ else {
230
+ if (face.basic_facts && Object.keys(face.basic_facts).length > 0)
231
+ payload.basic_facts = face.basic_facts;
232
+ if (face.default_tools && face.default_tools.length > 0)
233
+ payload.default_tools = face.default_tools;
234
+ if (face.default_model)
235
+ payload.default_model = face.default_model;
236
+ }
237
+ try {
238
+ await client.post('/v1/faces', { body: payload });
239
+ return true;
240
+ }
241
+ catch (err) {
242
+ if (err instanceof FacesAPIError && err.statusCode === 400) {
243
+ return false;
244
+ }
245
+ throw err;
246
+ }
247
+ }
248
+ findLatestBackup() {
249
+ const backupDir = path.join(os.homedir(), '.faces', 'backups');
250
+ if (!fs.existsSync(backupDir))
251
+ this.error('No backups found in ~/.faces/backups/');
252
+ const files = fs.readdirSync(backupDir)
253
+ .filter(f => f.endsWith('.json'))
254
+ .sort();
255
+ if (files.length === 0)
256
+ this.error('No backups found in ~/.faces/backups/');
257
+ return path.join(backupDir, files[files.length - 1]);
258
+ }
259
+ }
@@ -0,0 +1,27 @@
1
+ import { BaseCommand } from '../base.js';
2
+ export default class Causes extends BaseCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static flags: {
6
+ message: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
7
+ file: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
8
+ state: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
9
+ 'state-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
10
+ question: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
+ type: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
+ option: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
+ level: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
+ id: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
15
+ 'questions-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
+ session: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
+ compaction: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ 'base-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
19
+ token: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
20
+ 'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
21
+ toon: import("@oclif/core/interfaces").BooleanFlag<boolean>;
22
+ };
23
+ static args: {
24
+ face: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
25
+ };
26
+ run(): Promise<unknown>;
27
+ }
@@ -0,0 +1,135 @@
1
+ import fs from 'node:fs';
2
+ import { Args, Flags } from '@oclif/core';
3
+ import { BaseCommand } from '../base.js';
4
+ import { FacesAPIError } from '../client.js';
5
+ import { QUESTION_TYPES, causeLines, heldDecisionError, readQuestionFlags, readStateFlags } from '../decisions.js';
6
+ import { compileHint, isMeAlias, ME_MISSING, retryOnceOnWarmUp, sayWarmUp, SESSION_FLAG_DESCRIPTION, COMPACTION_FLAG_DESCRIPTION } from '../utils.js';
7
+ const MAX_CAUSE_QUESTIONS = 10;
8
+ export default class Causes extends BaseCommand {
9
+ static description = 'Get a face\'s cause alone, with no answering model: the principal why behind its response in this ' +
10
+ 'context (POST /v1/causes). Send a conversation with -m, or a situation with --state, optionally with ' +
11
+ 'questions for one cause per question. Billed at the cause rate only (see faces models:list --kind cause). ' +
12
+ 'A cause takes about 10 seconds. To have a model answer, use chat:chat or decide.';
13
+ static examples = [
14
+ '<%= config.bin %> <%= command.id %> alice -m "Should I take the offer?"',
15
+ '<%= config.bin %> <%= command.id %> alice --state "A fund offers $80k to change a finding before it is published."',
16
+ '<%= config.bin %> <%= command.id %> alice --state-file situation.txt --question "Does the speaker take the money?"',
17
+ '<%= config.bin %> <%= command.id %> "(alice | bob)" -m "What matters most here?" --json',
18
+ '<%= config.bin %> <%= command.id %> alice -m "So what do I tell them tomorrow?" --compaction fcmp_COMPACTION_ID',
19
+ ];
20
+ static flags = {
21
+ ...BaseCommand.baseFlags,
22
+ message: Flags.string({
23
+ char: 'm',
24
+ description: 'A user message (repeatable, sent in order). The cause is for the last one.',
25
+ multiple: true,
26
+ exclusive: ['state', 'state-file', 'file'],
27
+ }),
28
+ file: Flags.string({ description: 'Read the user message from a file.', exclusive: ['message', 'state', 'state-file'] }),
29
+ state: Flags.string({
30
+ description: 'A situation, as plain text, in place of a conversation.',
31
+ exclusive: ['state-file', 'message', 'file'],
32
+ }),
33
+ 'state-file': Flags.string({
34
+ description: 'Read the situation from this file. A .json file is sent as the JSON it holds.',
35
+ exclusive: ['state', 'message', 'file'],
36
+ }),
37
+ question: Flags.string({
38
+ description: 'A question about the situation, for a cause of its own. Its type follows from --option (a choice), ' +
39
+ '--level (a score), or neither (a yes-or-no). Needs --state or --state-file.',
40
+ exclusive: ['questions-file'],
41
+ }),
42
+ type: Flags.string({ description: 'The type of --question.', options: [...QUESTION_TYPES], dependsOn: ['question'] }),
43
+ option: Flags.string({ description: 'An option for a choice, as name="description" (repeatable).', multiple: true, dependsOn: ['question'], exclusive: ['level'] }),
44
+ level: Flags.string({ description: 'A level of a score, lowest first (repeatable).', multiple: true, dependsOn: ['question'], exclusive: ['option'] }),
45
+ id: Flags.string({ description: 'Key for the --question in the result (default: answer)', dependsOn: ['question'] }),
46
+ 'questions-file': Flags.string({
47
+ description: `A JSON file of 1 to ${MAX_CAUSE_QUESTIONS} questions in System One's format, as for faces decide. Needs --state or --state-file.`,
48
+ exclusive: ['question'],
49
+ }),
50
+ session: Flags.string({ description: SESSION_FLAG_DESCRIPTION }),
51
+ compaction: Flags.string({ description: COMPACTION_FLAG_DESCRIPTION }),
52
+ };
53
+ static args = {
54
+ face: Args.string({
55
+ description: 'The face: an alias, owner:alias for a face shared with you, or a composite such as "(a | b)". No @model.',
56
+ required: true,
57
+ }),
58
+ };
59
+ async run() {
60
+ const { args, flags } = await this.parse(Causes);
61
+ const client = this.makeClient(flags);
62
+ const json = this.jsonEnabled();
63
+ const face = args.face.trim();
64
+ if (face.includes('@')) {
65
+ this.error(`A cause is the face's alone, so leave the model off: ${face.slice(0, face.lastIndexOf('@'))}\nTo have a model answer, use faces chat:chat.`);
66
+ }
67
+ const fail = (m) => this.error(m);
68
+ const body = { model: face };
69
+ const situation = flags.state !== undefined || flags['state-file'] !== undefined;
70
+ const questions = readQuestionFlags(flags, fail);
71
+ if (situation) {
72
+ body.state = readStateFlags(flags, fail);
73
+ if (questions) {
74
+ const n = Object.keys(questions).length;
75
+ if (n > MAX_CAUSE_QUESTIONS)
76
+ this.error(`At most ${MAX_CAUSE_QUESTIONS} questions (got ${n}). Split them across requests.`);
77
+ body.questions = questions;
78
+ }
79
+ }
80
+ else {
81
+ if (questions)
82
+ this.error('Questions go with a situation: add --state or --state-file.');
83
+ const messages = [];
84
+ if (flags.file) {
85
+ if (!fs.existsSync(flags.file))
86
+ this.error(`File not found: ${flags.file}`);
87
+ messages.push(fs.readFileSync(flags.file, 'utf8'));
88
+ }
89
+ else if (flags.message?.length) {
90
+ messages.push(...flags.message);
91
+ }
92
+ else {
93
+ this.error('Send a conversation with -m (or --file), or a situation with --state (or --state-file).');
94
+ }
95
+ body.messages = messages.map((content) => ({ role: 'user', content }));
96
+ }
97
+ if (flags.session)
98
+ body.session = flags.session;
99
+ if (flags.compaction)
100
+ body.compaction = flags.compaction;
101
+ let data;
102
+ try {
103
+ data = (await retryOnceOnWarmUp(async () => {
104
+ const res = await client.post('/v1/causes', { body });
105
+ const held = heldDecisionError(res);
106
+ if (held)
107
+ throw held;
108
+ return res;
109
+ }, json ? undefined : sayWarmUp));
110
+ }
111
+ catch (err) {
112
+ if (err instanceof FacesAPIError) {
113
+ const head = `HTTP ${err.statusCode}: ${err.message}`;
114
+ if (err.statusCode === 409)
115
+ this.error(`${head}\n${compileHint(face, !/nothing compiled/i.test(err.message), 'give a cause')}`);
116
+ if (err.statusCode === 404 && isMeAlias(face))
117
+ this.error(`${head}\n${ME_MISSING}`);
118
+ if (err.statusCode === 503)
119
+ this.error(`${head}\n${err.retryAfter === undefined ? 'Try again.' : `Try again in ${err.retryAfter}s.`}`);
120
+ if (err.statusCode === 504)
121
+ this.error(`${head}\nThe face did not finish within 300 seconds. Ask again.`);
122
+ this.error(head);
123
+ }
124
+ throw err;
125
+ }
126
+ if (json)
127
+ return data;
128
+ const lines = causeLines(data, questions ? Object.keys(questions) : []);
129
+ if (lines.length === 0)
130
+ this.error('The response carried no cause. Run it again with --json to see what the server said.');
131
+ for (const line of lines)
132
+ this.log(line);
133
+ return data;
134
+ }
135
+ }
@@ -0,0 +1,42 @@
1
+ import { BaseCommand } from '../../base.js';
2
+ export default class ChatChat extends BaseCommand {
3
+ static description: string;
4
+ static examples: string[];
5
+ static flags: {
6
+ message: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
7
+ llm: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
8
+ formula: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
9
+ system: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
10
+ stream: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
+ 'max-tokens': import("@oclif/core/interfaces").OptionFlag<number | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
+ temperature: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
+ file: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
+ responses: import("@oclif/core/interfaces").BooleanFlag<boolean>;
15
+ style: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
+ medium: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
+ session: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ compaction: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
19
+ 'oauth-only': import("@oclif/core/interfaces").BooleanFlag<boolean>;
20
+ 'show-cause': import("@oclif/core/interfaces").BooleanFlag<boolean>;
21
+ cause: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
22
+ 'cause-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
23
+ 'cause-only': import("@oclif/core/interfaces").BooleanFlag<boolean>;
24
+ 'base-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
25
+ token: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
26
+ 'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
27
+ toon: import("@oclif/core/interfaces").BooleanFlag<boolean>;
28
+ };
29
+ static args: {
30
+ face_username: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
31
+ };
32
+ run(): Promise<unknown>;
33
+ private oauthHint;
34
+ private metaFor;
35
+ private streamDeltas;
36
+ private printReply;
37
+ private noteReasoningOnly;
38
+ private reasoningOnly;
39
+ private runChatCompletions;
40
+ private runMessages;
41
+ private runResponses;
42
+ }