agentpen-cli 0.1.2 → 0.1.4

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/server/notify.js DELETED
@@ -1,91 +0,0 @@
1
- // Per-user notifications. The server decides who should hear about what and
2
- // writes the words; clients (web, the Mac app, anything later) only display.
3
- import { q, id as newId, now } from './db.js';
4
-
5
- const BASE_URL = () => process.env.AGENTPEN_URL;
6
-
7
- // kind -> on by default? Users can flip each one.
8
- export const KINDS = {
9
- needs_you: true, // your own agent stopped to ask something
10
- team_blocked: true, // a teammate's agent has been stuck for a while
11
- done: true, // your agent finished a long piece of work
12
- prompt: true, // someone sent your agent a prompt
13
- joined: true, // someone joined your session
14
- chat: true, // someone messaged you in a session
15
- started: false, // a teammate went live
16
- pickup: true, // someone picked up your session to carry it on
17
- };
18
-
19
- const streams = new Map(); // user id -> Set<ws>
20
- let liveFor = () => [];
21
- export const setLiveProvider = (fn) => { liveFor = fn; };
22
-
23
- const push = (userId, m) => {
24
- const s = JSON.stringify(m);
25
- for (const ws of streams.get(userId) || []) if (ws.readyState === 1) ws.send(s);
26
- };
27
-
28
- export function prefsOf(userId) {
29
- let saved = {};
30
- try { saved = JSON.parse(q.get(`SELECT prefs FROM users WHERE id=?`, userId)?.prefs || '{}'); } catch {}
31
- return Object.fromEntries(Object.entries(KINDS).map(([k, dflt]) => [k, typeof saved[k] === 'boolean' ? saved[k] : dflt]));
32
- }
33
-
34
- export function savePrefs(userId, patch) {
35
- const next = prefsOf(userId);
36
- for (const k of Object.keys(KINDS)) if (typeof patch?.[k] === 'boolean') next[k] = patch[k];
37
- q.run(`UPDATE users SET prefs=? WHERE id=?`, JSON.stringify(next), userId);
38
- return next;
39
- }
40
-
41
- // n: { kind, title, body, session, reply: 'prompt' | 'chat' | null, urgent }
42
- export function notify(userId, n) {
43
- if (!userId || !prefsOf(userId)[n.kind]) return;
44
- const note = {
45
- id: newId(14), kind: n.kind, title: String(n.title).slice(0, 120), body: String(n.body || '').slice(0, 300),
46
- session: n.session || null, url: n.session ? `${BASE_URL()}/s/${n.session}` : BASE_URL(),
47
- reply: n.reply || null, urgent: !!n.urgent, at: now(),
48
- };
49
- q.run(`INSERT INTO notifications (id, user_id, kind, title, body, session_id, reply, urgent, at) VALUES (?,?,?,?,?,?,?,?,?)`,
50
- note.id, userId, note.kind, note.title, note.body, note.session, note.reply, note.urgent ? 1 : 0, note.at);
51
- push(userId, { t: 'notify', note });
52
- }
53
-
54
- // The reason for a notification went away (someone answered the agent, the
55
- // session ended): clients take the banner down so nobody chases a solved problem.
56
- export function retract(userIds, session, kinds) {
57
- for (const u of new Set(userIds)) {
58
- if (!u) continue;
59
- q.run(`DELETE FROM notifications WHERE user_id=? AND session_id=? AND kind IN (${kinds.map(() => '?').join(',')})`, u, session, ...kinds);
60
- push(u, { t: 'clear', session, kinds });
61
- }
62
- }
63
-
64
- const row = (r) => ({ id: r.id, kind: r.kind, title: r.title, body: r.body, session: r.session_id, url: r.session_id ? `${BASE_URL()}/s/${r.session_id}` : BASE_URL(), reply: r.reply, urgent: !!r.urgent, at: r.at });
65
-
66
- export function attachUser(ws, user, since) {
67
- if (!streams.has(user.id)) streams.set(user.id, new Set());
68
- streams.get(user.id).add(ws);
69
- // anything that arrived while this client was away, oldest first
70
- const missed = Number.isFinite(since) && since > 0
71
- ? q.all(`SELECT * FROM notifications WHERE user_id=? AND at>? ORDER BY at DESC LIMIT 20`, user.id, since).reverse().map(row) : [];
72
- ws.send(JSON.stringify({ t: 'hello', user: { id: user.id, name: user.name }, prefs: prefsOf(user.id), missed, sessions: liveFor(user.id), now: now() }));
73
- ws.on('close', () => { streams.get(user.id)?.delete(ws); if (!streams.get(user.id)?.size) streams.delete(user.id); });
74
- ws.on('error', () => {});
75
- }
76
-
77
- // Live session list for every connected member of a workspace (menu bar, badges).
78
- export function pushSessions(workspaceId) {
79
- if (!streams.size) return;
80
- for (const { user_id } of q.all(`SELECT user_id FROM memberships WHERE workspace_id=?`, workspaceId)) {
81
- if (streams.has(user_id)) push(user_id, { t: 'sessions', sessions: liveFor(user_id) });
82
- }
83
- }
84
-
85
- export function dropUserStreams(userId) {
86
- if (streams.has(userId)) push(userId, { t: 'sessions', sessions: liveFor(userId) });
87
- }
88
-
89
- export const membersOf = (workspaceId) => q.all(`SELECT user_id FROM memberships WHERE workspace_id=?`, workspaceId).map((r) => r.user_id);
90
-
91
- setInterval(() => q.run(`DELETE FROM notifications WHERE at<?`, now() - 7 * 86_400_000), 3_600_000).unref();
package/server/search.js DELETED
@@ -1,34 +0,0 @@
1
- // Search across everything a workspace's agents have done: terminal output,
2
- // conversations, prompts, chat and summaries. Indexed when a session ends.
3
- import { db, q } from './db.js';
4
- import { recordedText } from './agents.js';
5
-
6
- db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS search_idx USING fts5(sid UNINDEXED, body, tokenize='porter unicode61')`);
7
-
8
- const MAX_BODY = 1_500_000;
9
-
10
- export function indexSession(sessionId, chat = []) {
11
- const row = q.get(`SELECT * FROM sessions WHERE id=?`, sessionId);
12
- if (!row) return;
13
- const prompts = q.all(`SELECT actor, detail FROM events WHERE session_id=? AND type='session.prompt'`, sessionId).map((e) => `${e.actor}: ${e.detail}`);
14
- let summary = '';
15
- try { const s = JSON.parse(row.summary || 'null'); if (s) summary = [s.goal, ...(s.done || []), s.state, s.next].join('\n'); } catch {}
16
- const body = [row.title, row.project, row.branch, summary, prompts.join('\n'), chat.map((m) => `${m.name}: ${m.text}`).join('\n'), recordedText(row)]
17
- .filter(Boolean).join('\n\n').slice(0, MAX_BODY);
18
- q.run(`DELETE FROM search_idx WHERE sid=?`, sessionId);
19
- q.run(`INSERT INTO search_idx (sid, body) VALUES (?,?)`, sessionId, body);
20
- }
21
-
22
- /** Words become prefix terms that must all match; nothing the user types is treated as query syntax. */
23
- const toQuery = (text) => (String(text).match(/[\p{L}\p{N}_]+/gu) || []).slice(0, 12).map((w) => `"${w}"*`).join(' ');
24
-
25
- export function search(workspaceId, userId, text) {
26
- const query = toQuery(text);
27
- if (!query) return [];
28
- return q.all(
29
- `SELECT s.id, s.title, s.agent, s.kind, s.project, s.branch, s.started_at, s.ended_at, s.has_recording, s.access, u.name host_name,
30
- snippet(search_idx, 1, char(2), char(3), ' … ', 22) snippet
31
- FROM search_idx JOIN sessions s ON s.id = search_idx.sid JOIN users u ON u.id = s.host_id
32
- WHERE search_idx MATCH ? AND s.workspace_id = ? AND (s.access != 'private' OR s.host_id = ?)
33
- ORDER BY rank LIMIT 25`, query, workspaceId, userId);
34
- }