create-pmos 0.352.0 → 0.354.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.
@@ -0,0 +1,92 @@
1
+ /* pm/session-rail.js — the session list beside the chat (initiative premium-session-list, PRD L2). Pure: no DOM, no fetch, so Node tests it.
2
+ *
3
+ * railModel(rows, goals, { showAll }) → { groups: [{ id, title, rows, hidden }], ready, blocked, working, live, warn }
4
+ * railHtml(model, { sel, shut, folded }) → the list's markup
5
+ * teaser(model) → { dot, text } | null the chip in a normal-size chat dialog
6
+ * menuCount(rows) → how many live sessions need the PM (Ready or Maybe blocked): the ⋯ menu badge
7
+ * loudestRow(model) → the row the menu's Sessions opens on
8
+ *
9
+ * rows are GET /api/sessions rows (origin, automatic, shown, label, name, created, headless); goals is GET /api/briefing-premium/goals.
10
+ * Every word on a row is a record field or the fixed word for its state. Exited sessions are not listed. */
11
+
12
+ const esc = (s) => String(s == null ? '' : s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
13
+ // lib/session-state.js ATTENTION, minus exited: the order rows sort in and the state a folded group stands for
14
+ export const RANK = Object.freeze({ needs: 4, blocked: 3, working: 2, quiet: 1 });
15
+ export const DOT = Object.freeze({ needs: 'ready', blocked: 'blk', working: 'run', quiet: 'idle' });
16
+ export const WORD = Object.freeze({ needs: 'Ready', blocked: 'Maybe blocked', working: 'Running', quiet: 'Idle' });
17
+ const GOAL_LIVE = ['started', 'waiting'];
18
+ export const MANY = 6; // more agents than this working at once earns the soft warning (premium-sessions-warning)
19
+ const GROUPS = [['goals', 'Goals'], ['yours', 'Yours'], ['pmos', 'Started by PMOS']];
20
+
21
+ const keyOf = (origin) => (/^chat:/.test(String(origin || '')) ? String(origin).slice(5) : null);
22
+ const needsYou = (r) => r.state === 'needs' || r.state === 'blocked';
23
+ const kindOf = (r) => (r.headless ? 'run' : /^schedule:/.test(r.origin || '') ? 'clock' : r.goal ? 'goal' : r.key ? 'chat' : r.group === 'pmos' ? 'auto' : 'step');
24
+
25
+ function rowOf(s, goals) {
26
+ const key = keyOf(s.origin), g = key && goals ? goals[key] : null;
27
+ const goal = g && GOAL_LIVE.includes(g.state) ? g : null;
28
+ const state = RANK[s.shown] ? s.shown : 'working'; // an unknown live word is never promoted into a claim (session-state)
29
+ const group = goal ? 'goals' : s.automatic ? 'pmos' : 'yours';
30
+ let hint = WORD[state];
31
+ if (goal && goal.state === 'waiting') hint = 'Asked you';
32
+ else if (goal && state === 'working') hint = 'Round ' + (goal.rounds || 0) + ' of ' + goal.limit;
33
+ else if (state === 'working' && s.label && /\d/.test(s.label)) hint = s.label; // "Bash 2m": the server's word carries the open call
34
+ const r = { id: s.id, key, name: String(s.name || s.id).replace(/^chat:\s*/, ''), state, group, hint, goal, origin: s.origin || null, created: s.created || null, headless: s.headless || null, note: s.note || '', cwd: s.cwd || '', agent: s.agent || '' };
35
+ r.kind = kindOf(r);
36
+ return r;
37
+ }
38
+
39
+ export function railModel(rows, goals = {}, { showAll = false } = {}) {
40
+ const live = (Array.isArray(rows) ? rows : []).filter((s) => s && s.id && s.shown !== 'exited').map((s) => rowOf(s, goals));
41
+ live.sort((a, b) => (RANK[b.state] - RANK[a.state]) || (Date.parse(b.created || '') || 0) - (Date.parse(a.created || '') || 0));
42
+ const groups = GROUPS.map(([id, title]) => {
43
+ const all = live.filter((r) => r.group === id);
44
+ const shown = id === 'pmos' && !showAll ? all.filter(needsYou) : all;
45
+ return { id, title, rows: shown, all: all.length, hidden: all.length - shown.length };
46
+ }).filter((g) => g.all);
47
+ const n = (st) => live.filter((r) => r.state === st).length;
48
+ const working = n('working');
49
+ return { groups, live: live.length, ready: n('needs'), blocked: n('blocked'), working, warn: working > MANY ? working : 0, rows: live };
50
+ }
51
+
52
+ export const menuCount = (rows) => (Array.isArray(rows) ? rows : []).filter((s) => s && (s.shown === 'needs' || s.shown === 'blocked')).length;
53
+
54
+ export function loudestRow(m) { return (m && m.rows && m.rows[0]) || null; }
55
+
56
+ /** The dialog's title when the ⋯ menu's Sessions opens it: what needs the PM first. */
57
+ export function titleOf(m) { return !m || !m.live ? 'No sessions running' : m.ready ? m.ready + ' ready for you' : m.blocked ? m.blocked + ' ' + WORD.blocked.toLowerCase() : m.working ? m.working + ' running' : m.live + (m.live === 1 ? ' session' : ' sessions'); }
58
+
59
+ export function teaser(m) {
60
+ if (!m || !m.live) return null;
61
+ const top = m.rows[0];
62
+ const text = m.ready ? m.ready + ' ready' : m.blocked ? m.blocked + ' ' + WORD.blocked.toLowerCase() : m.working ? m.working + ' running' : m.live + (m.live === 1 ? ' session' : ' sessions');
63
+ return { dot: DOT[top.state], text };
64
+ }
65
+
66
+ const ICON = {
67
+ chat: '<path d="M4 5h16v11H9l-5 4z"/>', goal: '<path d="M5 21V4"/><path d="M5 4h12l-2 4 2 4H5"/>', step: '<path d="M5 12h14"/><path d="M13 6l6 6-6 6"/>',
68
+ auto: '<circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M4.2 4.2l2.1 2.1M17.7 17.7l2.1 2.1M2 12h3M19 12h3M4.2 19.8l2.1-2.1M17.7 6.3l2.1-2.1"/>',
69
+ clock: '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>', run: '<path d="M4 17l6-6-6-6"/><path d="M12 19h8"/>',
70
+ };
71
+ export const icon = (k) => `<svg class="i" viewBox="0 0 24 24" aria-hidden="true">${ICON[k] || ICON.step}</svg>`;
72
+ const CHEV = '<svg class="i chev" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 9l6 6 6-6"/></svg>';
73
+
74
+ export function railHtml(m, { sel = null, shut = [], showAll = false } = {}) {
75
+ if (!m || !m.live) return '<p class="srl-none">No sessions are running. Write in a chat to start one.</p>';
76
+ let h = m.warn ? `<p class="srl-warn" role="status"><b>${m.warn} agents are working at once.</b> Each one is a program running on this computer, so many at once can slow it. Stop the ones you are not watching.</p>` : '';
77
+ for (const g of m.groups) {
78
+ const open = !shut.includes(g.id);
79
+ h += `<section class="srl-g"><button type="button" class="srl-gh" data-sg="${g.id}" aria-expanded="${open}">${CHEV}<span>${esc(g.title)}</span><em>${g.all}</em></button>`;
80
+ if (!open) { h += '</section>'; continue; }
81
+ h += '<ul>' + g.rows.map((r) => `<li><button type="button" class="srl-r${r.id === sel ? ' on' : ''}${r.state === 'needs' ? ' rd' : ''}" data-sid="${esc(r.id)}" aria-current="${r.id === sel}" aria-label="${esc(r.name + ', ' + r.hint)}" title="${esc(r.name + ' · ' + r.hint)}"><i class="sdot2 ${DOT[r.state]}" aria-hidden="true"></i><span class="srl-ic">${icon(r.kind)}</span><span class="srl-n">${esc(r.name)}</span><span class="srl-h">${esc(r.hint)}</span></button></li>`).join('');
82
+ if (g.id === 'pmos' && (g.hidden || showAll)) h += `<li><button type="button" class="srl-more" data-smore="1">${showAll ? 'Hide the ones that need nothing' : g.hidden + ' more working on their own · Show'}</button></li>`;
83
+ h += '</ul></section>';
84
+ }
85
+ return h;
86
+ }
87
+
88
+ /** The facts the viewer shows for a session that is not a chat row: every line a record field. */
89
+ export function factsOf(r) {
90
+ const who = r.group === 'pmos' ? 'Started by PMOS' : r.goal ? 'A goal in your chat' : r.key ? 'Your chat' : 'You started it';
91
+ return [['State', WORD[r.state]], ['Started by', who], ['Started', r.created ? new Date(r.created).toLocaleString() : ''], ['Folder', r.cwd && r.cwd !== '.' ? r.cwd : ''], ['Note', r.note], ['Kind', r.headless ? 'Background run, no terminal' : '']].filter((x) => x[1]);
92
+ }
@@ -6455,6 +6455,27 @@ async function handleRequest(req, res) {
6455
6455
  }
6456
6456
  // End a session and remove it, with an answer. The WebSocket kill/remove messages return nothing,
6457
6457
  // so a refusal or a slow exit left the PM clicking a control that seemed to do nothing.
6458
+ // premium-session-list L5: the session list's panel writes to a session the PM did not start from a chat panel (a Next step, a PMOS run).
6459
+ // A page press only, into a live session that is not a plain shell; `say` types a line with Enter, `keys` a permission option or Esc.
6460
+ if ((p === '/api/session/say' || p === '/api/session/keys') && req.method === 'POST') {
6461
+ let body = {};
6462
+ try { body = JSON.parse((await readBody(req)) || '{}'); } catch { return json(res, { ok: false, error: 'invalid JSON body' }, 400); }
6463
+ const g = pmGuard.requirePagePress(req, body);
6464
+ if (!g.ok) return json(res, { ok: false, error: g.error }, g.status);
6465
+ const sess = sessions.get(String(body.id || ''));
6466
+ if (!sess || !sess.pty || sess.state === 'exited') return json(res, { ok: false, error: 'That session is not running.' }, 409);
6467
+ if (sess.agent === 'shell') return json(res, { ok: false, error: 'A terminal shell takes input in the console.' }, 409);
6468
+ if (p === '/api/session/say') {
6469
+ const t = String(body.text == null ? '' : body.text).trim();
6470
+ if (!t) return json(res, { ok: false, error: 'Write a message for Claude.' }, 400);
6471
+ if (t.length > 8000) return json(res, { ok: false, error: 'The message is too long (8000 characters at most).' }, 400);
6472
+ return chatSessions.type(sess.id, t) ? json(res, { ok: true, session: sess.id }) : json(res, { ok: false, error: 'The message did not reach the session.' }, 409);
6473
+ }
6474
+ const KEYS = { esc: '\x1b', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9' };
6475
+ const data = Object.prototype.hasOwnProperty.call(KEYS, String(body.press)) ? KEYS[String(body.press)] : null;
6476
+ if (!data) return json(res, { ok: false, error: 'press must be esc or 1 to 9' }, 400);
6477
+ return chatSessions.keys(sess.id, data) ? json(res, { ok: true, session: sess.id }) : json(res, { ok: false, error: 'The key did not reach the session.' }, 409);
6478
+ }
6458
6479
  if (p === '/api/session/remove' && req.method === 'POST') {
6459
6480
  let body = {};
6460
6481
  try { body = JSON.parse((await readBody(req)) || '{}'); } catch { return json(res, { ok: false, error: 'invalid JSON body' }, 400); }
@@ -56,6 +56,7 @@
56
56
  "briefing-premium-sources.test.js",
57
57
  "briefing-premium-drafts.test.js",
58
58
  "agent-inbox.test.js",
59
+ "session-rail.test.js",
59
60
  "briefing-premium-metrics.test.js",
60
61
  "premium-welcome.test.js",
61
62
  "calendar-events.test.js",
@@ -232,6 +233,7 @@
232
233
  "briefing-premium-sources.test.js": 1,
233
234
  "briefing-premium-drafts.test.js": 1,
234
235
  "agent-inbox.test.js": 1,
236
+ "session-rail.test.js": 1,
235
237
  "briefing-premium-metrics.test.js": 1,
236
238
  "premium-welcome.test.js": 1,
237
239
  "calendar-events.test.js": 1,