tabherd 0.0.0-stage → 0.6.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.
- package/CHANGELOG.md +166 -0
- package/LICENSE +674 -0
- package/README.md +540 -2
- package/launcher/npm.js +9 -0
- package/lib/agents.js +324 -0
- package/lib/codex-client.js +306 -0
- package/lib/codex-rollout.js +251 -0
- package/lib/codex.js +424 -0
- package/lib/comfy.js +479 -0
- package/lib/completed.js +55 -0
- package/lib/copilot.js +359 -0
- package/lib/cursor.js +516 -0
- package/lib/dismissed.js +30 -0
- package/lib/filters.js +120 -0
- package/lib/groups.js +144 -0
- package/lib/history.js +253 -0
- package/lib/llamacpp.js +159 -0
- package/lib/lmstudio.js +134 -0
- package/lib/localai.js +291 -0
- package/lib/memory.js +55 -0
- package/lib/ollama.js +124 -0
- package/lib/open-dashboard.js +39 -0
- package/lib/opencode.js +371 -0
- package/lib/repos.js +81 -0
- package/lib/saved.js +33 -0
- package/lib/secrets.js +196 -0
- package/lib/session-id.js +9 -0
- package/lib/settings.js +231 -0
- package/lib/store.js +44 -0
- package/lib/terminals/index.js +62 -0
- package/lib/terminals/kitty.js +126 -0
- package/lib/terminals/linux.js +310 -0
- package/lib/terminals/macos.js +468 -0
- package/lib/terminals/tmux.js +89 -0
- package/lib/terminals/unix-driver.js +172 -0
- package/lib/terminals/unix.js +204 -0
- package/lib/terminals/unsupported.js +22 -0
- package/lib/terminals/wezterm.js +103 -0
- package/lib/terminals/windows-close.ps1 +125 -0
- package/lib/terminals/windows-focus.ps1 +235 -0
- package/lib/terminals/windows-pick-folder.ps1 +57 -0
- package/lib/terminals/windows-titles.js +124 -0
- package/lib/terminals/windows-titles.ps1 +95 -0
- package/lib/terminals/windows.js +166 -0
- package/lib/ticket-refs.js +184 -0
- package/lib/tickets.js +437 -0
- package/lib/todos.js +166 -0
- package/lib/trackers.js +541 -0
- package/lib/transcript.js +314 -0
- package/lib/updates.js +186 -0
- package/package.json +44 -4
- package/public/app.js +4077 -0
- package/public/favicon.svg +1 -0
- package/public/icons/tabherd-128.png +0 -0
- package/public/icons/tabherd-16.png +0 -0
- package/public/icons/tabherd-20.png +0 -0
- package/public/icons/tabherd-24.png +0 -0
- package/public/icons/tabherd-256.png +0 -0
- package/public/icons/tabherd-32.png +0 -0
- package/public/icons/tabherd-40.png +0 -0
- package/public/icons/tabherd-48.png +0 -0
- package/public/icons/tabherd-512.png +0 -0
- package/public/icons/tabherd-64.png +0 -0
- package/public/index.html +655 -0
- package/public/lang/es.js +742 -0
- package/public/style.css +1443 -0
- package/server.js +814 -0
package/lib/groups.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const store = require('./store');
|
|
7
|
+
const { isSessionId } = require('./session-id');
|
|
8
|
+
|
|
9
|
+
// Groups of sessions, each with a name and a colour. A session joins a group
|
|
10
|
+
// by its folder: the group with a folder that holds it, the nearest one if
|
|
11
|
+
// several do. A session put in a group (or in none) by hand stays there,
|
|
12
|
+
// kept by its session ID, so it's still there when it's resumed. A to-do put
|
|
13
|
+
// in one by hand passes that on to the session it starts.
|
|
14
|
+
|
|
15
|
+
// Named, not free colours: each is tuned for light and dark, and kept apart
|
|
16
|
+
// from the status colours, in the page's stylesheet.
|
|
17
|
+
const COLORS = ['rose', 'orange', 'olive', 'teal', 'indigo', 'plum', 'brown', 'slate'];
|
|
18
|
+
const MAX_GROUPS = 20;
|
|
19
|
+
const MAX_FOLDERS = 50;
|
|
20
|
+
const MAX_PLACED = 2000;
|
|
21
|
+
const NAME_MAX = 40;
|
|
22
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
23
|
+
const CASELESS = process.platform === 'win32' || process.platform === 'darwin';
|
|
24
|
+
|
|
25
|
+
const key = (p) => (CASELESS ? p.toLowerCase() : p);
|
|
26
|
+
|
|
27
|
+
// A full path, with ~ for the home folder. Nothing has to be there yet.
|
|
28
|
+
function cleanFolder(p) {
|
|
29
|
+
if (typeof p !== 'string' || p.length > 1000) return null;
|
|
30
|
+
const t = p.trim().replace(/^~(?=$|[\\/])/, os.homedir());
|
|
31
|
+
return t && path.isAbsolute(t) ? path.resolve(t) : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// A group as given, cleaned, or a reason it can't be saved.
|
|
35
|
+
function cleanGroup(g) {
|
|
36
|
+
if (!g || typeof g !== 'object') return { error: 'Not a group.' };
|
|
37
|
+
const name = typeof g.name === 'string' ? g.name.replace(/\s+/g, ' ').trim() : '';
|
|
38
|
+
if (!name) return { error: 'A group needs a name.' };
|
|
39
|
+
if (name.length > NAME_MAX) return { error: `Keep group names to ${NAME_MAX} characters.` };
|
|
40
|
+
if (!COLORS.includes(g.color)) return { error: `Not one of the group colours: ${g.color}` };
|
|
41
|
+
const given = Array.isArray(g.folders) ? g.folders : [];
|
|
42
|
+
if (given.length > MAX_FOLDERS) return { error: `A group can have up to ${MAX_FOLDERS} folders.` };
|
|
43
|
+
const folders = [];
|
|
44
|
+
for (const f of given) {
|
|
45
|
+
const clean = cleanFolder(f);
|
|
46
|
+
if (!clean) return { error: `"${f}" isn't a full path. Start it with a drive or /, or ~ for your home folder.` };
|
|
47
|
+
if (!folders.some((x) => key(x) === key(clean))) folders.push(clean);
|
|
48
|
+
}
|
|
49
|
+
const id = typeof g.id === 'string' && UUID.test(g.id) ? g.id : crypto.randomUUID();
|
|
50
|
+
return { group: { id, name, color: g.color, folders } };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function load() {
|
|
54
|
+
const saved = store.read('groups', null);
|
|
55
|
+
const groups = [];
|
|
56
|
+
for (const g of Array.isArray(saved?.groups) ? saved.groups : []) {
|
|
57
|
+
const { group } = cleanGroup(g);
|
|
58
|
+
if (group && groups.length < MAX_GROUPS && !groups.some((x) => x.id === group.id)) groups.push(group);
|
|
59
|
+
}
|
|
60
|
+
const placed = new Map();
|
|
61
|
+
for (const [id, p] of Object.entries(saved?.placed || {})) {
|
|
62
|
+
if (!isSessionId(id) || !p || typeof p !== 'object') continue;
|
|
63
|
+
if (p.group !== null && !groups.some((g) => g.id === p.group)) continue;
|
|
64
|
+
placed.set(id, { group: p.group, at: Number(p.at) || 0 });
|
|
65
|
+
}
|
|
66
|
+
return { groups, placed };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let state = load();
|
|
70
|
+
|
|
71
|
+
function save(next) {
|
|
72
|
+
const placed = [...next.placed].sort((a, b) => b[1].at - a[1].at).slice(0, MAX_PLACED);
|
|
73
|
+
try {
|
|
74
|
+
store.write('groups', { groups: next.groups, placed: Object.fromEntries(placed) });
|
|
75
|
+
} catch (err) {
|
|
76
|
+
return { ok: false, error: `Couldn't save the groups: ${err.message}` };
|
|
77
|
+
}
|
|
78
|
+
state = { groups: next.groups, placed: new Map(placed) };
|
|
79
|
+
return { ok: true, groups: state.groups };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const list = () => state.groups;
|
|
83
|
+
|
|
84
|
+
// Replaces every group, as the Settings editor saves them. A session put by
|
|
85
|
+
// hand into a group that's gone goes back to following its folder.
|
|
86
|
+
function setGroups(given) {
|
|
87
|
+
if (!Array.isArray(given)) return { ok: false, error: 'Expected a list of groups.' };
|
|
88
|
+
if (given.length > MAX_GROUPS) return { ok: false, error: `Up to ${MAX_GROUPS} groups.` };
|
|
89
|
+
const groups = [];
|
|
90
|
+
for (const g of given) {
|
|
91
|
+
const { group, error } = cleanGroup(g);
|
|
92
|
+
if (error) return { ok: false, error };
|
|
93
|
+
if (groups.some((x) => x.id === group.id)) return { ok: false, error: 'Two groups have the same ID.' };
|
|
94
|
+
if (groups.some((x) => x.name.toLowerCase() === group.name.toLowerCase())) return { ok: false, error: `There's already a group called ${group.name}.` };
|
|
95
|
+
groups.push(group);
|
|
96
|
+
}
|
|
97
|
+
const placed = new Map([...state.placed].filter(([, p]) => p.group === null || groups.some((g) => g.id === p.group)));
|
|
98
|
+
return save({ groups, placed });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Puts a session or to-do in a group by hand (`group` is its ID), in none
|
|
102
|
+
// (null), or back to following its folder ('auto').
|
|
103
|
+
function place(id, group) {
|
|
104
|
+
if (!isSessionId(id)) return { ok: false, error: 'Not a session.' };
|
|
105
|
+
if (group !== null && group !== 'auto' && !state.groups.some((g) => g.id === group)) return { ok: false, error: 'That group no longer exists.' };
|
|
106
|
+
const placed = new Map(state.placed);
|
|
107
|
+
if (group === 'auto') placed.delete(id);
|
|
108
|
+
else placed.set(id, { group, at: Date.now() });
|
|
109
|
+
return save({ groups: state.groups, placed });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// A to-do's session starts in the group the to-do was put in by hand.
|
|
113
|
+
function carry(fromId, toId) {
|
|
114
|
+
const p = state.placed.get(fromId);
|
|
115
|
+
return p ? place(toId, p.group) : { ok: true };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function inside(dir, folder) {
|
|
119
|
+
const d = key(path.resolve(dir));
|
|
120
|
+
const f = key(folder);
|
|
121
|
+
return d === f || d.startsWith(f.endsWith(path.sep) ? f : f + path.sep);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The group a session or to-do is in: { group: its ID or null, placed: true
|
|
125
|
+
// if it was put there by hand }.
|
|
126
|
+
function groupOf({ id, cwd }) {
|
|
127
|
+
const p = state.placed.get(id);
|
|
128
|
+
if (p) return { group: p.group, placed: true };
|
|
129
|
+
let best = null;
|
|
130
|
+
let bestLength = -1;
|
|
131
|
+
if (typeof cwd === 'string' && path.isAbsolute(cwd)) {
|
|
132
|
+
for (const g of state.groups) {
|
|
133
|
+
for (const f of g.folders) {
|
|
134
|
+
if (f.length > bestLength && inside(cwd, f)) {
|
|
135
|
+
best = g.id;
|
|
136
|
+
bestLength = f.length;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { group: best, placed: false };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = { list, setGroups, place, carry, groupOf, COLORS };
|
package/lib/history.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const store = require('./store');
|
|
6
|
+
const { Transcript } = require('./transcript');
|
|
7
|
+
const { PROJECTS_DIR } = require('./agents');
|
|
8
|
+
const codex = require('./codex');
|
|
9
|
+
const { Rollout } = require('./codex-rollout');
|
|
10
|
+
const copilot = require('./copilot');
|
|
11
|
+
const cursor = require('./cursor');
|
|
12
|
+
|
|
13
|
+
const MAX_SESSIONS = 300;
|
|
14
|
+
const LOOKBACK_MS = 30 * 24 * 60 * 60 * 1000;
|
|
15
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
+
const SESSION_FILE = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
|
|
17
|
+
|
|
18
|
+
// Closed sessions by ID, saved as history.json.
|
|
19
|
+
let sessions = load();
|
|
20
|
+
// The interactive sessions in the last snapshot, and when it was taken.
|
|
21
|
+
let live = new Map();
|
|
22
|
+
let liveAt = 0;
|
|
23
|
+
// Transcripts that aren't interactive sessions, so aren't read again this run.
|
|
24
|
+
const skipped = new Map();
|
|
25
|
+
let scanning = null;
|
|
26
|
+
|
|
27
|
+
const interactive = (a) => !a.kind || a.kind === 'interactive';
|
|
28
|
+
const ms = (t) => (typeof t === 'number' ? t : (t && Date.parse(t)) || null);
|
|
29
|
+
|
|
30
|
+
function load() {
|
|
31
|
+
const saved = store.read('history', null);
|
|
32
|
+
const list = Array.isArray(saved?.sessions) ? saved.sessions : [];
|
|
33
|
+
return new Map(list.filter((s) => s && UUID.test(s.id) && typeof s.cwd === 'string').map((s) => [s.id, s]));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function save() {
|
|
37
|
+
const list = [...sessions.values()].sort((a, b) => b.closedAt - a.closedAt).slice(0, MAX_SESSIONS);
|
|
38
|
+
sessions = new Map(list.map((s) => [s.id, s]));
|
|
39
|
+
try {
|
|
40
|
+
store.write('history', { sessions: list });
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// `mtime` is the transcript's modified time when it was last read, so an
|
|
45
|
+
// unchanged transcript isn't read again. A session seen closing has none yet.
|
|
46
|
+
function fromAgent(a, closedAt) {
|
|
47
|
+
return {
|
|
48
|
+
app: a.app || 'claude',
|
|
49
|
+
// Run headless, with `claude -p`, as background reviews and scripts do.
|
|
50
|
+
background: Boolean(a.background),
|
|
51
|
+
id: a.id,
|
|
52
|
+
name: a.name || null,
|
|
53
|
+
terminalTitle: a.terminalTitle || null,
|
|
54
|
+
title: a.title || null,
|
|
55
|
+
cwd: a.cwd,
|
|
56
|
+
gitBranch: a.gitBranch || null,
|
|
57
|
+
model: a.model || null,
|
|
58
|
+
modelName: a.modelName || null,
|
|
59
|
+
startedAt: ms(a.startedAt),
|
|
60
|
+
closedAt,
|
|
61
|
+
firstPrompt: a.firstPrompt || null,
|
|
62
|
+
lastPrompt: a.lastPrompt || null,
|
|
63
|
+
lastReply: a.lastReply || null,
|
|
64
|
+
recap: a.recap || null,
|
|
65
|
+
prompts: a.prompts || 0,
|
|
66
|
+
outputTokens: a.outputTokens || 0,
|
|
67
|
+
prs: a.prs || [],
|
|
68
|
+
// Its tickets as they were when it closed.
|
|
69
|
+
tickets: a.tickets || [],
|
|
70
|
+
// The to-do it was started from, so the notes aren't lost when the to-do goes.
|
|
71
|
+
todo: a.todo ? { title: a.todo.title, notes: a.todo.notes } : null,
|
|
72
|
+
mtime: null,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Keeps what only the live session showed: its name, its terminal tab's title
|
|
77
|
+
// and its to-do.
|
|
78
|
+
function fromTranscript(id, t, mtime, known, app = 'claude') {
|
|
79
|
+
const s = t.summary();
|
|
80
|
+
const o = t.origin();
|
|
81
|
+
return {
|
|
82
|
+
...fromAgent(
|
|
83
|
+
{
|
|
84
|
+
...s,
|
|
85
|
+
app,
|
|
86
|
+
id,
|
|
87
|
+
background: known?.background || o.entrypoint === 'sdk-cli',
|
|
88
|
+
cwd: known?.cwd || o.cwd,
|
|
89
|
+
name: known?.name,
|
|
90
|
+
terminalTitle: known?.terminalTitle,
|
|
91
|
+
todo: known?.todo,
|
|
92
|
+
tickets: known?.tickets,
|
|
93
|
+
},
|
|
94
|
+
Math.max(known?.closedAt || 0, mtime),
|
|
95
|
+
),
|
|
96
|
+
startedAt: known?.startedAt || ms(o.startedAt),
|
|
97
|
+
mtime,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Called with each snapshot. A session that was live last time and isn't now
|
|
102
|
+
// has closed. One that's live again has been resumed, so it leaves the history
|
|
103
|
+
// until it closes again. A session closed before it was asked anything isn't
|
|
104
|
+
// kept: Claude saves no conversation for it, so it couldn't be resumed.
|
|
105
|
+
function observe(agents, now) {
|
|
106
|
+
const next = new Map(agents.filter(interactive).map((a) => [a.id, a]));
|
|
107
|
+
let changed = false;
|
|
108
|
+
for (const id of next.keys()) if (sessions.delete(id)) changed = true;
|
|
109
|
+
for (const [id, a] of live) {
|
|
110
|
+
if (next.has(id) || !a.cwd || !a.prompts) continue;
|
|
111
|
+
sessions.set(id, fromAgent(a, liveAt));
|
|
112
|
+
changed = true;
|
|
113
|
+
}
|
|
114
|
+
live = next;
|
|
115
|
+
liveAt = now;
|
|
116
|
+
if (changed) save();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Once the history is full, transcripts older than all of it would only be
|
|
120
|
+
// trimmed off again.
|
|
121
|
+
function oldestKept() {
|
|
122
|
+
if (sessions.size < MAX_SESSIONS) return 0;
|
|
123
|
+
return Math.min(...[...sessions.values()].map((s) => s.closedAt));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Adds sessions from the last 30 days that closed while the server wasn't
|
|
127
|
+
// watching, from the transcripts Claude Code keeps, and updates any resumed
|
|
128
|
+
// and closed again since. Reading a transcript can take tens of milliseconds,
|
|
129
|
+
// so the scan yields between them.
|
|
130
|
+
async function scanTranscripts() {
|
|
131
|
+
const floor = Math.max(Date.now() - LOOKBACK_MS, oldestKept());
|
|
132
|
+
let changed = false;
|
|
133
|
+
let dirs;
|
|
134
|
+
try {
|
|
135
|
+
dirs = await fs.promises.readdir(PROJECTS_DIR);
|
|
136
|
+
} catch {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
for (const dir of dirs) {
|
|
140
|
+
let names;
|
|
141
|
+
try {
|
|
142
|
+
names = await fs.promises.readdir(path.join(PROJECTS_DIR, dir));
|
|
143
|
+
} catch {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
for (const name of names) {
|
|
147
|
+
const id = name.match(SESSION_FILE)?.[1];
|
|
148
|
+
if (!id || live.has(id)) continue;
|
|
149
|
+
const file = path.join(PROJECTS_DIR, dir, name);
|
|
150
|
+
let mtime;
|
|
151
|
+
try {
|
|
152
|
+
mtime = (await fs.promises.stat(file)).mtimeMs;
|
|
153
|
+
} catch {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const known = sessions.get(id);
|
|
157
|
+
if (mtime < floor || known?.mtime === mtime || skipped.get(id) === mtime) continue;
|
|
158
|
+
const t = new Transcript(file);
|
|
159
|
+
if (!t.update()) continue;
|
|
160
|
+
const { cwd, entrypoint } = t.origin();
|
|
161
|
+
// Sessions in a terminal, and `claude -p` runs, which can be resumed in
|
|
162
|
+
// one; not Agent SDK runs, which belong to the program that ran them. A
|
|
163
|
+
// session with no prompts has nothing to go back to.
|
|
164
|
+
if (!cwd || !['cli', 'sdk-cli'].includes(entrypoint) || !t.summary().prompts) {
|
|
165
|
+
skipped.set(id, mtime);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
sessions.set(id, fromTranscript(id, t, mtime, known));
|
|
169
|
+
changed = true;
|
|
170
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (await scanCodex(floor)) changed = true;
|
|
174
|
+
if (await scanCopilot(floor)) changed = true;
|
|
175
|
+
if (await scanCursor(floor)) changed = true;
|
|
176
|
+
if (changed) save();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The same for Codex, from its session files, named as Codex named them.
|
|
180
|
+
// Not until the dashboard has heard from Codex which sessions are open, so
|
|
181
|
+
// none of those is taken for closed.
|
|
182
|
+
async function scanCodex(floor) {
|
|
183
|
+
if (!codex.settled()) return false;
|
|
184
|
+
const names = codex.sessionNames();
|
|
185
|
+
let changed = false;
|
|
186
|
+
for (const { id, file, mtime } of await codex.sessionFiles(floor)) {
|
|
187
|
+
const known = sessions.get(id);
|
|
188
|
+
if (live.has(id) || known?.mtime === mtime || skipped.get(id) === mtime) continue;
|
|
189
|
+
const r = new Rollout(file);
|
|
190
|
+
if (!r.update()) continue;
|
|
191
|
+
const o = r.origin();
|
|
192
|
+
// Subagents' sessions and `codex exec` runs aren't ones to go back to.
|
|
193
|
+
if (!o.cwd || o.parentThreadId || typeof o.source !== 'string' || ['exec', 'mcp'].includes(o.source) || !r.summary().prompts) {
|
|
194
|
+
skipped.set(id, mtime);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
sessions.set(id, { ...fromTranscript(id, r, mtime, known, 'codex'), title: names.get(id) || known?.title || null });
|
|
198
|
+
changed = true;
|
|
199
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
200
|
+
}
|
|
201
|
+
return changed;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The same for the Copilot CLI's sessions. VS Code's are resumed in VS Code.
|
|
205
|
+
async function scanCopilot(floor) {
|
|
206
|
+
let changed = false;
|
|
207
|
+
for (const { id, file, mtime, workspace } of await copilot.sessionFiles(floor)) {
|
|
208
|
+
const known = sessions.get(id);
|
|
209
|
+
if (live.has(id) || known?.mtime === mtime || skipped.get(id) === mtime) continue;
|
|
210
|
+
const log = new copilot.CopilotLog(file);
|
|
211
|
+
if (!log.update() || /vscode/i.test(workspace.client_name || '') || !(workspace.cwd || log.origin().cwd) || !log.summary().prompts) {
|
|
212
|
+
skipped.set(id, mtime);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const entry = fromTranscript(id, log, mtime, { ...known, cwd: known?.cwd || workspace.cwd }, 'copilot');
|
|
216
|
+
sessions.set(id, { ...entry, title: workspace.name || workspace.summary || known?.title || null });
|
|
217
|
+
changed = true;
|
|
218
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
219
|
+
}
|
|
220
|
+
return changed;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The same for Cursor CLI's chats, titled as Cursor titled them.
|
|
224
|
+
async function scanCursor(floor) {
|
|
225
|
+
let changed = false;
|
|
226
|
+
for (const { id, file, mtime, chat } of await cursor.sessionFiles(floor)) {
|
|
227
|
+
const known = sessions.get(id);
|
|
228
|
+
if (live.has(id) || known?.mtime === mtime || skipped.get(id) === mtime) continue;
|
|
229
|
+
const log = new cursor.CursorLog(file);
|
|
230
|
+
if (!log.update() || !log.summary().prompts) {
|
|
231
|
+
skipped.set(id, mtime);
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const entry = fromTranscript(id, log, mtime, { ...known, cwd: known?.cwd || chat.cwd, startedAt: known?.startedAt || chat.createdAt }, 'cursor');
|
|
235
|
+
sessions.set(id, { ...entry, title: chat.title || known?.title || null });
|
|
236
|
+
changed = true;
|
|
237
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
238
|
+
}
|
|
239
|
+
return changed;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function backfill() {
|
|
243
|
+
if (!scanning) scanning = scanTranscripts().finally(() => (scanning = null));
|
|
244
|
+
return scanning;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Newest first, leaving out sessions that are live again, and any recorded
|
|
248
|
+
// with no prompts before sessions like that were left out.
|
|
249
|
+
const list = () => [...sessions.values()].filter((s) => !live.has(s.id) && s.prompts > 0).sort((a, b) => b.closedAt - a.closedAt);
|
|
250
|
+
const find = (id) => sessions.get(id) || null;
|
|
251
|
+
const isLive = (id) => live.has(id);
|
|
252
|
+
|
|
253
|
+
module.exports = { observe, backfill, list, find, isLive };
|
package/lib/llamacpp.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// llama.cpp's server, llama-server: the model it has loaded, as a row on the
|
|
4
|
+
// Memory and models card, with its size, context and how many requests it's
|
|
5
|
+
// answering. In router mode (started with --models-dir or --models-preset)
|
|
6
|
+
// it loads models as they're asked for, so each one loaded or loading gets a
|
|
7
|
+
// row, and Unload unloads it. A server started with one model keeps it until
|
|
8
|
+
// it's stopped, so that row has no Unload.
|
|
9
|
+
//
|
|
10
|
+
// It's found at Settings' llama.cpp address, else at LLAMA_ARG_HOST and
|
|
11
|
+
// LLAMA_ARG_PORT as llama-server reads them, else http://127.0.0.1:8080,
|
|
12
|
+
// llama-server's own default. LocalAI's default is the same port; each tells
|
|
13
|
+
// the other apart by what it answers.
|
|
14
|
+
|
|
15
|
+
const REFRESH_MS = 2000;
|
|
16
|
+
const REQUEST_MS = 2500;
|
|
17
|
+
const DEFAULT_URL = 'http://127.0.0.1:8080';
|
|
18
|
+
|
|
19
|
+
function fromEnv(env = process.env) {
|
|
20
|
+
if (!env.LLAMA_ARG_HOST && !env.LLAMA_ARG_PORT) return null;
|
|
21
|
+
let host = (env.LLAMA_ARG_HOST || '127.0.0.1').trim();
|
|
22
|
+
if (host === '0.0.0.0' || host === '::') host = '127.0.0.1';
|
|
23
|
+
const port = Number(env.LLAMA_ARG_PORT) || 8080;
|
|
24
|
+
try {
|
|
25
|
+
return new URL(`http://${host.includes(':') ? `[${host}]` : host}:${port}`).origin;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let url = fromEnv() || DEFAULT_URL;
|
|
32
|
+
let state = null;
|
|
33
|
+
let refreshing = null;
|
|
34
|
+
let refreshedAt = 0;
|
|
35
|
+
// Counts unloads, so a check begun before one doesn't put back what it removed.
|
|
36
|
+
let unloads = 0;
|
|
37
|
+
|
|
38
|
+
function configure({ url: next } = {}) {
|
|
39
|
+
const u = (next || fromEnv() || DEFAULT_URL).replace(/\/+$/, '');
|
|
40
|
+
if (u === url) return;
|
|
41
|
+
url = u;
|
|
42
|
+
state = null;
|
|
43
|
+
refreshedAt = 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function request(route, body) {
|
|
47
|
+
const res = await fetch(url + route, {
|
|
48
|
+
method: body ? 'POST' : 'GET',
|
|
49
|
+
headers: body ? { 'Content-Type': 'application/json' } : {},
|
|
50
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
51
|
+
signal: AbortSignal.timeout(REQUEST_MS),
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) throw new Error(`llama.cpp answered ${res.status}`);
|
|
54
|
+
return res.json();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const busy = (slots) => (Array.isArray(slots) ? slots.filter((s) => s.is_processing).length : null);
|
|
58
|
+
|
|
59
|
+
// What the server has loaded. /props says which mode it's in; a server still
|
|
60
|
+
// loading its one model answers 503 until it's ready, and shows nothing yet.
|
|
61
|
+
async function refresh() {
|
|
62
|
+
const begun = unloads;
|
|
63
|
+
const props = await request('/props');
|
|
64
|
+
if (props?.role === 'router') {
|
|
65
|
+
const list = await request('/models');
|
|
66
|
+
const shown = (list?.data || []).filter((m) => ['loaded', 'loading'].includes(m.status?.value));
|
|
67
|
+
const slots = await Promise.all(
|
|
68
|
+
shown.map((m) => (m.status.value === 'loaded' ? request(`/slots?model=${encodeURIComponent(m.id)}`).catch(() => null) : null)),
|
|
69
|
+
);
|
|
70
|
+
if (begun === unloads) state = { build: props.build_info || null, router: true, models: shown.map((m, i) => ({ ...m, slots: slots[i] })) };
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const [models, slots] = await Promise.all([request('/v1/models'), request('/slots').catch(() => null)]);
|
|
74
|
+
const m = models?.data?.[0] || {};
|
|
75
|
+
// Started without --alias, the model's name is its whole path.
|
|
76
|
+
const alias = props.model_alias && props.model_alias !== props.model_path ? props.model_alias : null;
|
|
77
|
+
state = {
|
|
78
|
+
build: props.build_info || null,
|
|
79
|
+
router: false,
|
|
80
|
+
models: [
|
|
81
|
+
{
|
|
82
|
+
id: alias || fileName(props.model_path) || m.id,
|
|
83
|
+
file: props.model_path,
|
|
84
|
+
ftype: props.model_ftype,
|
|
85
|
+
sleeping: props.is_sleeping === true,
|
|
86
|
+
meta: { ...m.meta, n_ctx: props.default_generation_settings?.n_ctx || m.meta?.n_ctx },
|
|
87
|
+
slots,
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function poll() {
|
|
94
|
+
if (refreshing || Date.now() - refreshedAt < REFRESH_MS) return;
|
|
95
|
+
refreshing = refresh()
|
|
96
|
+
// Not running is the usual case, and shows nothing.
|
|
97
|
+
.catch(() => (state = null))
|
|
98
|
+
.finally(() => {
|
|
99
|
+
refreshing = null;
|
|
100
|
+
refreshedAt = Date.now();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const bytes = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)} GB` : n >= 1e6 ? `${Math.round(n / 1e6)} MB` : null);
|
|
105
|
+
const params = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)}B` : n >= 1e6 ? `${Math.round(n / 1e6)}M` : null);
|
|
106
|
+
const fileName = (p) => (p ? String(p).split(/[\\/]/).pop() : null);
|
|
107
|
+
|
|
108
|
+
function itemsFrom(s) {
|
|
109
|
+
if (!s) return [];
|
|
110
|
+
// "b12669-30ec35ae2" is build 12669.
|
|
111
|
+
const build = s.build?.match(/^b(\d+)/)?.[1];
|
|
112
|
+
return s.models.map((m) => {
|
|
113
|
+
const loading = m.status?.value === 'loading';
|
|
114
|
+
const running = busy(m.slots);
|
|
115
|
+
return {
|
|
116
|
+
app: 'llamacpp',
|
|
117
|
+
kind: 'model',
|
|
118
|
+
id: `llamacpp:model:${m.id}`,
|
|
119
|
+
model: m.id,
|
|
120
|
+
name: m.id,
|
|
121
|
+
host: build ? `llama.cpp build ${build}` : 'llama.cpp',
|
|
122
|
+
where: [params(m.meta?.n_params), m.ftype || m.meta?.ftype].filter(Boolean).join(' · ') || null,
|
|
123
|
+
status: 'loaded',
|
|
124
|
+
facts: [
|
|
125
|
+
['State', loading ? 'Loading' : m.sleeping ? 'Asleep: loads again on the next request' : null],
|
|
126
|
+
['Size', bytes(m.meta?.size)],
|
|
127
|
+
['Context', Number.isFinite(m.meta?.n_ctx) && m.meta.n_ctx > 0 ? `${m.meta.n_ctx.toLocaleString('en')} tokens` : null],
|
|
128
|
+
['Answering', running ? `${running} of ${m.slots.length} at once` : null],
|
|
129
|
+
['File', s.router || fileName(m.file) === m.id ? null : fileName(m.file)],
|
|
130
|
+
].filter(([, v]) => v),
|
|
131
|
+
// Its size, about what it takes: llama.cpp doesn't say.
|
|
132
|
+
memory: m.meta?.size > 0 ? { used: m.meta.size, about: true } : null,
|
|
133
|
+
actions: s.router && !loading ? ['unload'] : [],
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// llama.cpp's rows from the last check, which checks again in the background.
|
|
139
|
+
function snapshot() {
|
|
140
|
+
poll();
|
|
141
|
+
return itemsFrom(state);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// In router mode, unloads a model. Its row goes at once, without waiting for
|
|
145
|
+
// the next check.
|
|
146
|
+
async function act(item, action) {
|
|
147
|
+
if (action !== 'unload' || !state?.router) return { ok: false, error: "That can't be done here." };
|
|
148
|
+
try {
|
|
149
|
+
await request('/models/unload', { model: item.model });
|
|
150
|
+
unloads++;
|
|
151
|
+
state = { ...state, models: state.models.filter((m) => m.id !== item.model) };
|
|
152
|
+
refreshedAt = 0;
|
|
153
|
+
return { ok: true };
|
|
154
|
+
} catch (err) {
|
|
155
|
+
return { ok: false, error: `Couldn't unload it: ${err.message}` };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = { snapshot, configure, act, itemsFrom, fromEnv, DEFAULT_URL };
|
package/lib/lmstudio.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// LM Studio's loaded models as cards, from its REST API (/api/v1/models):
|
|
4
|
+
// each loaded instance, with its size and context, and Unload. LM Studio's
|
|
5
|
+
// local server has to be on, in its Developer tab or with `lms server start`.
|
|
6
|
+
//
|
|
7
|
+
// It's found at Settings' LM Studio address, else the port LM Studio last
|
|
8
|
+
// served on, else http://127.0.0.1:1234, and asked with Settings' API token
|
|
9
|
+
// if LM Studio requires one.
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const os = require('os');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
|
|
15
|
+
const REFRESH_MS = 2000;
|
|
16
|
+
const REQUEST_MS = 2500;
|
|
17
|
+
const DEFAULT_URL = 'http://127.0.0.1:1234';
|
|
18
|
+
|
|
19
|
+
// The port LM Studio's server last ran on, from its own (undocumented) file.
|
|
20
|
+
function lastPort(home = os.homedir()) {
|
|
21
|
+
try {
|
|
22
|
+
const { port } = JSON.parse(fs.readFileSync(path.join(home, '.lmstudio', '.internal', 'http-server-config.json'), 'utf8'));
|
|
23
|
+
return Number.isInteger(port) && port > 0 && port < 65536 ? `http://127.0.0.1:${port}` : null;
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let config = { url: null, key: '' };
|
|
30
|
+
let state = null;
|
|
31
|
+
let refreshing = null;
|
|
32
|
+
let refreshedAt = 0;
|
|
33
|
+
|
|
34
|
+
const base = () => (config.url || lastPort() || DEFAULT_URL).replace(/\/+$/, '');
|
|
35
|
+
|
|
36
|
+
function configure({ url, key } = {}) {
|
|
37
|
+
if ((url || null) === config.url && (key || '') === config.key) return;
|
|
38
|
+
config = { url: url || null, key: key || '' };
|
|
39
|
+
state = null;
|
|
40
|
+
refreshedAt = 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function request(route, body) {
|
|
44
|
+
const res = await fetch(base() + route, {
|
|
45
|
+
method: body ? 'POST' : 'GET',
|
|
46
|
+
headers: { ...(config.key ? { Authorization: `Bearer ${config.key}` } : {}), ...(body ? { 'Content-Type': 'application/json' } : {}) },
|
|
47
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
48
|
+
signal: AbortSignal.timeout(REQUEST_MS),
|
|
49
|
+
});
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
const err = new Error(res.status === 401 || res.status === 403 ? 'LM Studio wants an API token: set it in Settings.' : `LM Studio answered ${res.status}`);
|
|
52
|
+
err.status = res.status;
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
return res.json();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function refresh() {
|
|
59
|
+
try {
|
|
60
|
+
const v1 = await request('/api/v1/models');
|
|
61
|
+
state = { models: v1?.models || [], v1: true };
|
|
62
|
+
} catch (err) {
|
|
63
|
+
if (err.status === 401 || err.status === 403) throw err;
|
|
64
|
+
// Before LM Studio 0.4 there was only v0, which says which models are
|
|
65
|
+
// loaded but can't unload them.
|
|
66
|
+
const v0 = await request('/api/v0/models');
|
|
67
|
+
state = { models: (v0?.data || []).filter((m) => m.state === 'loaded').map((m) => ({ ...m, key: m.id, loaded_instances: [{ id: m.id, config: { context_length: m.loaded_context_length } }] })), v1: false };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function poll() {
|
|
72
|
+
if (refreshing || Date.now() - refreshedAt < REFRESH_MS) return;
|
|
73
|
+
refreshing = refresh()
|
|
74
|
+
.catch((err) => {
|
|
75
|
+
// Not running is the usual case, and shows nothing; a refused token
|
|
76
|
+
// shows once, on a card, so it can be fixed.
|
|
77
|
+
state = err.status === 401 || err.status === 403 ? { error: err.message, models: [] } : null;
|
|
78
|
+
})
|
|
79
|
+
.finally(() => {
|
|
80
|
+
refreshing = null;
|
|
81
|
+
refreshedAt = Date.now();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const bytes = (n) => (n >= 1e9 ? `${(n / 1e9).toFixed(1)} GB` : n >= 1e6 ? `${Math.round(n / 1e6)} MB` : null);
|
|
86
|
+
|
|
87
|
+
function itemsFrom(s, { now = Date.now() } = {}) {
|
|
88
|
+
if (!s) return [];
|
|
89
|
+
if (s.error) return [{ app: 'lmstudio', kind: 'service', id: 'lmstudio:service', name: 'LM Studio', host: 'LM Studio', status: 'waiting', waitingFor: s.error, statusSince: now, actions: [] }];
|
|
90
|
+
const items = [];
|
|
91
|
+
for (const m of s.models) {
|
|
92
|
+
for (const inst of m.loaded_instances || []) {
|
|
93
|
+
if (!inst?.id) continue;
|
|
94
|
+
items.push({
|
|
95
|
+
app: 'lmstudio',
|
|
96
|
+
kind: 'model',
|
|
97
|
+
id: `lmstudio:model:${inst.id}`,
|
|
98
|
+
instanceId: inst.id,
|
|
99
|
+
name: m.display_name || m.key || inst.id,
|
|
100
|
+
host: 'LM Studio',
|
|
101
|
+
where: [m.quantization?.name || m.quantization, m.type && m.type !== 'llm' ? m.type : null].filter((v) => typeof v === 'string' && v).join(' · ') || null,
|
|
102
|
+
status: 'loaded',
|
|
103
|
+
facts: [
|
|
104
|
+
['Size', bytes(m.size_bytes)],
|
|
105
|
+
['Context', Number.isFinite(inst.config?.context_length) ? `${inst.config.context_length.toLocaleString('en')} tokens` : null],
|
|
106
|
+
['Parallel', Number.isFinite(inst.config?.parallel) && inst.config.parallel > 1 ? String(inst.config.parallel) : null],
|
|
107
|
+
].filter(([, v]) => v),
|
|
108
|
+
// LM Studio doesn't say what a loaded model takes. Its size is about that.
|
|
109
|
+
memory: m.size_bytes > 0 ? { used: m.size_bytes, about: true } : null,
|
|
110
|
+
actions: s.v1 ? ['unload'] : [],
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return items;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// LM Studio's cards from the last check, which checks again in the background.
|
|
118
|
+
function snapshot() {
|
|
119
|
+
poll();
|
|
120
|
+
return itemsFrom(state);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function act(item, action) {
|
|
124
|
+
if (action !== 'unload' || item.kind !== 'model') return { ok: false, error: "That can't be done here." };
|
|
125
|
+
try {
|
|
126
|
+
await request('/api/v1/models/unload', { instance_id: item.instanceId });
|
|
127
|
+
refreshedAt = 0;
|
|
128
|
+
return { ok: true };
|
|
129
|
+
} catch (err) {
|
|
130
|
+
return { ok: false, error: `Couldn't unload it: ${err.message}` };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = { snapshot, configure, act, itemsFrom, lastPort, DEFAULT_URL };
|