clavarium 0.2.175-beta.1 → 0.2.176-beta.1

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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.175-beta.1",
3
+ "version": "0.2.176-beta.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.175-beta.1",
9
+ "version": "0.2.176-beta.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.175-beta.1",
3
+ "version": "0.2.176-beta.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
package/server/main.mjs CHANGED
@@ -23,12 +23,13 @@ import pty from 'node-pty';
23
23
  import { attachBridge } from './bridge.mjs';
24
24
  import { attachCommand } from './tmux.mjs';
25
25
  import { handleApi } from './api.mjs';
26
- import { Conversations, DRAWN_MS } from './conversations.mjs';
26
+ import { Conversations, DRAWN_MS, Refusal } from './conversations.mjs';
27
27
  import { Store, JsonFile } from './store.mjs';
28
28
  import { Instance } from './instance.mjs';
29
29
  import { loadTools } from './tools.mjs';
30
30
  import { Vms, runner } from './vm.mjs';
31
- import { Resources } from './resources.mjs';
31
+ import { Resources, WORK_TAB } from './resources.mjs';
32
+ import { download } from './files.mjs';
32
33
  import { makeGate, makeOriginCheck, readOperators } from './gate.mjs';
33
34
  import { Shortcuts } from './shortcuts.mjs';
34
35
  import { hearBox, Dictation } from './voice.mjs';
@@ -684,7 +685,18 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
684
685
  rooms: (name, list) => talk.rooms(name, list), conversations: () => all.list(), boxes: () => boxes.all(),
685
686
  speak: (where, text, ids, how) => { void boxes.agentSaid(where, text, ids, how); },
686
687
  /* What an AI shows in a room: opened through its own conversation, with its rights (talk.mjs). */
687
- reach: (name, path) => talk.reachOf(name, path), shown: (name, ref) => talk.shown(name, ref) });
688
+ reach: (name, path) => talk.reachOf(name, path), shown: (name, ref) => talk.shown(name, ref),
689
+ /* What an AI gives another AI of the room: read from its own folders, as the room's /shown route does (api.mjs). */
690
+ read: async (name, ref) => {
691
+ const m = /^([a-z][a-z0-9-]{0,31}):(.+)$/.exec(String(ref ?? ''));
692
+ if (!m) throw new Refusal(404, 'No such file.');
693
+ if (m[1] === WORK_TAB) {
694
+ if (all.list().find((x) => x.name === name)?.work !== 'kept') throw new Refusal(404, 'No such file.');
695
+ return download(folders.workFolder(name), m[2]);
696
+ }
697
+ if (!folders.of(name).some((r) => r.name === m[1])) throw new Refusal(404, 'No such file.');
698
+ return download(folders.folder(m[1]), m[2]);
699
+ } });
688
700
  rooms.boxesBound(boxes.all());
689
701
  rooms.refresh();
690
702
  /* FEATURES (features.mjs): read from what the modules say, never a secret's value. The remote screen and the
package/server/rooms.mjs CHANGED
@@ -18,7 +18,7 @@ import { createReadStream, lstatSync, mkdirSync, rmSync } from 'node:fs';
18
18
  import { randomUUID } from 'node:crypto';
19
19
  import { join } from 'node:path';
20
20
  import { Parley, FILES_MAX } from 'clavarium-parley/host';
21
- import { importFile } from './files.mjs';
21
+ import { importFile, IMPORT_MAX } from './files.mjs';
22
22
  import { Refusal } from './conversations.mjs';
23
23
  import { scan } from './secret-scan.mjs';
24
24
 
@@ -40,6 +40,13 @@ const ROOM_FILE = /^\/room\/attached\/([0-9a-f-]{36})\/([^/]+)$/;
40
40
  rights -- never another's. In the thread, its reference reads `ai:<member id>:<how its chat opens it>` (work:report.html);
41
41
  the room's /shown route opens it only when that AI cited it in this room AND its own chat has it (talk.shown). */
42
42
  const CITE = 'cite:';
43
+ /* A file name as a chat's files take it (files.mjs isFileName): one visible segment, at most 120 characters. */
44
+ const cleanName = (name) => {
45
+ const plain = String(name ?? '').replace(/[\\/\u0000-\u001f\u007f-\u009f]+/g, '-').replace(/^[.\s]+/, '').trim();
46
+ if (plain.length <= 120) return plain;
47
+ const dot = plain.lastIndexOf('.'); const ext = dot > 0 && plain.length - dot <= 16 ? plain.slice(dot) : '';
48
+ return plain.slice(0, 120 - ext.length) + ext;
49
+ };
43
50
  const AI_OPEN = /^ai:([0-9a-f-]{36}):([a-z][a-z0-9-]{0,31}):(.+)$/;
44
51
  const nameOf = (v) => String(v ?? '').replace(/\s+/g, ' ').trim().slice(0, ROOM_NAME_MAX);
45
52
 
@@ -53,15 +60,17 @@ export const masked = (text) => { const str = String(text ?? ''); const hits = s
53
60
  export const lookOf = (name) => { const text = String(name ?? ''); let h = 0; for (const ch of text) h = (h * 31 + ch.codePointAt(0)) >>> 0; return { initial: ([...text.trim()][0] ?? '?').toUpperCase(), hue: h % 360 }; };
54
61
 
55
62
  export class Rooms {
56
- #file; #dir; #say; #attach; #rooms; #conversations; #boxes; #now; #speak; #reachOf; #shownIn; #hops = new Map(); #thinking = new Map();
63
+ #file; #dir; #say; #attach; #read; #rooms; #conversations; #boxes; #now; #speak; #reachOf; #shownIn; #hops = new Map(); #thinking = new Map();
57
64
  /* file: a JsonFile of the rooms; dir: where their threads are; say(name, text, extra): a message written in a
58
65
  conversation's own chat (talk.say; its 4th argument the paths of its files there); attach(name, fileName,
59
66
  request): a file put in a conversation's chat as the person attaches one (talk.attach); rooms(name, list): what a conversation is told of its rooms (talk.rooms);
60
67
  conversations(): the conversations there are; boxes(): the voice boxes there are.
61
68
  reach(name, path): how a conversation's chat opens a path of its own ("work:a.png"), or null (talk.reachOf);
62
- shown(name, ref): whether that chat has that file (talk.shown) -- what an AI shows in a room is read through them. */
63
- constructor({ file, dir, say, attach = async () => { throw new Refusal(409, 'Files cannot be given here.'); }, rooms = () => {}, conversations = () => [], boxes = () => [], speak = () => {}, now = () => new Date(), reach = () => null, shown = () => false }) {
64
- this.#speak = speak; this.#attach = attach; this.#reachOf = reach; this.#shownIn = shown;
69
+ shown(name, ref): whether that chat has that file (talk.shown) -- what an AI shows in a room is read through them.
70
+ read(name, ref): that file of that conversation ({ name, size, stream }), read with its rights -- what an AI
71
+ shows or cites to another AI of the room is copied from it (#carried). */
72
+ constructor({ file, dir, say, attach = async () => { throw new Refusal(409, 'Files cannot be given here.'); }, rooms = () => {}, conversations = () => [], boxes = () => [], speak = () => {}, now = () => new Date(), reach = () => null, shown = () => false, read = async () => { throw new Refusal(404, 'No such file.'); } }) {
73
+ this.#speak = speak; this.#attach = attach; this.#read = read; this.#reachOf = reach; this.#shownIn = shown;
65
74
  this.#file = file; this.#dir = dir; this.#say = say; this.#rooms = rooms; this.#conversations = conversations; this.#boxes = boxes; this.#now = now;
66
75
  }
67
76
  #all() { const v = this.#file.get(); return Array.isArray(v) ? v : []; }
@@ -290,12 +299,16 @@ export class Rooms {
290
299
  /* The other AIs it is sent to (#280): they get it in their own chat, as said in the room -- never beyond a few
291
300
  hand-overs after the person last spoke (no two AIs talking for ever). */
292
301
  const ais = to.map((x) => members.find((m) => m.id === x && m.kind === 'ai')).filter(Boolean);
293
- const handOver = (ai, text, from, message) => {
302
+ /* `paths`: the files it shows or cites in it -- each AI it goes to gets its own copy, as when the person attaches one. */
303
+ const handOver = (ai, text, from, message, paths = []) => {
294
304
  const n = (this.#hops.get(room.id) ?? 0) + 1;
295
305
  if (n > HOPS_MAX) return false;
296
306
  this.#hops.set(room.id, n);
297
307
  this.#think(room.id, ai.id, true);
298
- void this.#say(ai.ref, text, { by: from, room: { id: room.id, name: room.name, message } }).catch(() => {});
308
+ void (async () => {
309
+ const copies = paths.length ? await this.#carried(me, ai, paths) : [];
310
+ await this.#say(ai.ref, text, { by: from, room: { id: room.id, name: room.name, message } }, ...(copies.length ? [copies] : []));
311
+ })().catch(() => {});
299
312
  return true;
300
313
  };
301
314
  /* Handed on as it is: the message itself, by who said it; the room's thread keeps no copy. */
@@ -319,10 +332,41 @@ export class Rooms {
319
332
  /* The boxes it is sent to say it aloud (#281): its words, a member named by its name, never an id. */
320
333
  const boxes = to.map((x) => members.find((m) => m.id === x && m.kind === 'box')).filter(Boolean).map((m) => m.ref);
321
334
  if (boxes.length) { const aloud = text.replace(/@\{([0-9a-f-]{36})\}/g, (w, mid) => members.find((x) => x.id === mid)?.name ?? ''); try { this.#speak(`room:${room.id}`, aloud, boxes, { doing: kind === 'doing', asked: kind === 'ask' }); } catch { /* the boxes' */ } }
322
- if (!forwarded && kind !== 'doing') for (const ai of ais) handOver(ai, text, { id: me.id, name: me.name, kind: 'ai', look: me.look }, id);
335
+ if (!forwarded && kind !== 'doing' && ais.length) {
336
+ /* What it shows, and the paths it cites (its message's refs, as the room's thread read them). */
337
+ const said = all().find((m) => m.id === id);
338
+ const paths = [...new Set([...(show?.path ? [show.path] : []), ...(show?.files ?? []).map((f) => f.path),
339
+ ...(Array.isArray(said?.refs) ? said.refs.filter((r) => String(r.open ?? '').startsWith(CITE)).map((r) => r.path) : [])])].slice(0, FILES_MAX);
340
+ for (const ai of ais) handOver(ai, text, { id: me.id, name: me.name, kind: 'ai', look: me.look }, id, paths);
341
+ }
323
342
  return id;
324
343
  }
325
344
 
345
+ /* A FILE AN AI GIVES ANOTHER AI OF THE ROOM (06-11): read from the giver's conversation, with its rights -- a path
346
+ its own chat opens (its /work, a folder attached to it) and has (shown or cited there), as the room's preview --
347
+ then put in the receiver's chat files, as if the person attached it there. One it may not give, too large, or
348
+ of a name that cannot be cleaned: not given (the message still goes). Resolves with the receiver's paths. */
349
+ async #carried(from, to, paths) {
350
+ const copies = [];
351
+ for (const path of paths) {
352
+ if (typeof path !== 'string' || path.split('/').includes('..')) continue;
353
+ let own = null; try { own = this.#reachOf(from.ref, path); } catch { own = null; }
354
+ if (typeof own !== 'string' || !own) continue;
355
+ let has = false; try { has = this.#shownIn(from.ref, own) === true; } catch { has = false; }
356
+ if (!has) continue;
357
+ let file = null;
358
+ try {
359
+ file = await this.#read(from.ref, own);
360
+ if (!file?.stream || !(file.size <= IMPORT_MAX)) continue;
361
+ const name = cleanName(file.name);
362
+ if (!name) continue;
363
+ file.stream.headers = { 'content-length': String(file.size) };
364
+ copies.push((await this.#attach(to.ref, name, file.stream)).path);
365
+ } catch { /* not given */ } finally { file?.stream?.destroy?.(); }
366
+ }
367
+ return copies;
368
+ }
369
+
326
370
  /* Each conversation told the rooms it is in, and who is there. */
327
371
  #tellConversations(list) {
328
372
  const shown = list.map((r) => ({ ...r, members: this.#shown(r) }));