@bakapiano/ccsm 0.10.3 → 0.11.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 (48) hide show
  1. package/CLAUDE.md +475 -475
  2. package/README.md +190 -190
  3. package/bin/ccsm.js +194 -194
  4. package/lib/cliSessionWatcher.js +249 -249
  5. package/lib/config.js +185 -185
  6. package/lib/folders.js +96 -96
  7. package/lib/localCliSessions.js +489 -177
  8. package/lib/persistedSessions.js +134 -134
  9. package/lib/webTerminal.js +208 -208
  10. package/lib/workspace.js +230 -255
  11. package/package.json +57 -57
  12. package/public/css/base.css +99 -99
  13. package/public/css/cards.css +183 -183
  14. package/public/css/feedback.css +303 -303
  15. package/public/css/forms.css +405 -405
  16. package/public/css/layout.css +160 -160
  17. package/public/css/modal.css +190 -183
  18. package/public/css/responsive.css +10 -10
  19. package/public/css/sidebar.css +616 -601
  20. package/public/css/terminals.css +294 -294
  21. package/public/css/tokens.css +81 -79
  22. package/public/css/wco.css +98 -98
  23. package/public/css/widgets.css +1596 -1375
  24. package/public/index.html +105 -103
  25. package/public/js/api.js +272 -260
  26. package/public/js/components/AdoptModal.js +343 -171
  27. package/public/js/components/App.js +35 -35
  28. package/public/js/components/DirectoryPicker.js +203 -203
  29. package/public/js/components/EntityFormModal.js +105 -105
  30. package/public/js/components/Modal.js +51 -51
  31. package/public/js/components/OfflineBanner.js +93 -93
  32. package/public/js/components/PageTitleBar.js +13 -13
  33. package/public/js/components/Picker.js +179 -179
  34. package/public/js/components/Popover.js +55 -55
  35. package/public/js/components/Sidebar.js +270 -270
  36. package/public/js/components/TerminalView.js +298 -298
  37. package/public/js/components/useDragSort.js +67 -67
  38. package/public/js/dialog.js +67 -67
  39. package/public/js/icons.js +177 -177
  40. package/public/js/main.js +140 -140
  41. package/public/js/pages/AboutPage.js +165 -165
  42. package/public/js/pages/ConfigurePage.js +475 -487
  43. package/public/js/pages/LaunchPage.js +369 -369
  44. package/public/js/pages/SessionsPage.js +97 -97
  45. package/public/js/state.js +231 -231
  46. package/public/manifest.webmanifest +15 -15
  47. package/scripts/install.js +137 -137
  48. package/server.js +1126 -1117
package/public/js/api.js CHANGED
@@ -1,260 +1,272 @@
1
- // Fetch wrapper + every loader. Loaders push into signals from ./state.js.
2
- // Cross-origin (hosted frontend → local backend) flows through httpBase().
3
-
4
- import * as S from './state.js';
5
- import { httpBase } from './backend.js';
6
-
7
- export async function api(method, url, body) {
8
- const opts = { method, headers: { 'Content-Type': 'application/json' } };
9
- if (body !== undefined) opts.body = JSON.stringify(body);
10
- const r = await fetch(httpBase() + url, opts);
11
- const text = await r.text();
12
- let json;
13
- try { json = text ? JSON.parse(text) : {}; } catch { json = { raw: text }; }
14
- if (!r.ok) throw new Error(json.error || `HTTP ${r.status}`);
15
- return json;
16
- }
17
-
18
- export async function loadConfig() {
19
- const [cfg, caps] = await Promise.all([
20
- api('GET', '/api/config'),
21
- api('GET', '/api/capabilities').catch(() => ({ webTerminal: false })),
22
- ]);
23
- S.config.value = cfg;
24
- S.capabilities.value = caps;
25
- }
26
-
27
- // Update an existing CLI by id. patch is shallow-merged into the record.
28
- export async function updateCli(id, patch) {
29
- const cfg = S.config.value || (await api('GET', '/api/config'));
30
- const target = (cfg.clis || []).find((c) => c.id === id);
31
- // Built-in CLIs lock down identity-defining fields. UI already greys these
32
- // out; we belt-and-braces here so a tampered request from elsewhere
33
- // can't change them either.
34
- if (target?.builtin) {
35
- delete patch.command;
36
- delete patch.id;
37
- delete patch.builtin;
38
- }
39
- const toArr = (v, fallback) => Array.isArray(v) ? v :
40
- typeof v === 'string' ? v.split(/\s+/).filter(Boolean) : fallback;
41
- const next = {
42
- ...cfg,
43
- clis: (cfg.clis || []).map((c) => c.id === id ? {
44
- ...c, ...patch,
45
- args: toArr(patch.args, c.args),
46
- resumeArgs: toArr(patch.resumeArgs, c.resumeArgs || []),
47
- shell: ['direct', 'pwsh', 'cmd'].includes(patch.shell ?? c.shell) ? (patch.shell ?? c.shell) : 'direct',
48
- } : c),
49
- };
50
- const saved = await api('PUT', '/api/config', next);
51
- S.config.value = saved;
52
- return id;
53
- }
54
-
55
- export async function deleteCli(id) {
56
- const cfg = S.config.value || (await api('GET', '/api/config'));
57
- const target = (cfg.clis || []).find((c) => c.id === id);
58
- if (target?.builtin) throw new Error(`"${target.name}" is built-in and can't be deleted`);
59
- const clis = (cfg.clis || []).filter((c) => c.id !== id);
60
- if (clis.length === 0) throw new Error('cannot delete the last CLI');
61
- const next = { ...cfg, clis };
62
- if (next.defaultCliId === id) next.defaultCliId = clis[0].id;
63
- const saved = await api('PUT', '/api/config', next);
64
- S.config.value = saved;
65
- }
66
-
67
- export async function updateRepo(name, patch) {
68
- const cfg = S.config.value || (await api('GET', '/api/config'));
69
- const next = {
70
- ...cfg,
71
- repos: (cfg.repos || []).map((r) => r.name === name ? {
72
- ...r,
73
- name: (patch.name ?? r.name).trim(),
74
- url: (patch.url ?? r.url).trim(),
75
- defaultSelected: patch.defaultSelected ?? r.defaultSelected,
76
- } : r),
77
- };
78
- const saved = await api('PUT', '/api/config', next);
79
- S.config.value = saved;
80
- }
81
-
82
- export async function deleteRepo(name) {
83
- const cfg = S.config.value || (await api('GET', '/api/config'));
84
- const next = { ...cfg, repos: (cfg.repos || []).filter((r) => r.name !== name) };
85
- const saved = await api('PUT', '/api/config', next);
86
- S.config.value = saved;
87
- }
88
-
89
- export async function setDefaultCli(id) {
90
- const cfg = S.config.value || (await api('GET', '/api/config'));
91
- const saved = await api('PUT', '/api/config', { ...cfg, defaultCliId: id });
92
- S.config.value = saved;
93
- }
94
-
95
- // Add a new CLI to config.clis and return its id. Generates a fresh id
96
- // from the command name + an integer suffix when collisions exist.
97
- export async function createCli({ name, command, args, resumeArgs, shell, type }) {
98
- const cfg = S.config.value || (await api('GET', '/api/config'));
99
- const base = (name || command || 'cli').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'cli';
100
- let id = base, n = 1;
101
- while ((cfg.clis || []).some((c) => c.id === id)) { id = `${base}-${++n}`; }
102
- const toArr = (v) => Array.isArray(v) ? v : (typeof v === 'string' ? v.split(/\s+/).filter(Boolean) : []);
103
- const next = {
104
- ...cfg,
105
- clis: [...(cfg.clis || []), {
106
- id,
107
- name: (name || command || id).trim(),
108
- command: (command || '').trim(),
109
- args: toArr(args),
110
- resumeArgs: toArr(resumeArgs),
111
- shell: ['direct', 'pwsh', 'cmd'].includes(shell) ? shell : 'direct',
112
- type: ['claude', 'codex', 'other'].includes(type) ? type : 'other',
113
- }],
114
- };
115
- const saved = await api('PUT', '/api/config', next);
116
- S.config.value = saved;
117
- return id;
118
- }
119
-
120
- // Add a new repo to config.repos. Repos are addressed by name (which must
121
- // be unique). Returns the name on success, throws on duplicate.
122
- export async function createRepo({ name, url, defaultSelected }) {
123
- const cfg = S.config.value || (await api('GET', '/api/config'));
124
- const cleanName = (name || '').trim();
125
- const cleanUrl = (url || '').trim();
126
- if (!cleanName) throw new Error('repo name required');
127
- if (!cleanUrl) throw new Error('repo url required');
128
- if ((cfg.repos || []).some((r) => r.name === cleanName)) {
129
- throw new Error(`repo "${cleanName}" already exists`);
130
- }
131
- const next = {
132
- ...cfg,
133
- repos: [...(cfg.repos || []), {
134
- name: cleanName,
135
- url: cleanUrl,
136
- defaultSelected: !!defaultSelected,
137
- }],
138
- };
139
- const saved = await api('PUT', '/api/config', next);
140
- S.config.value = saved;
141
- return cleanName;
142
- }
143
-
144
- export async function loadSessions() {
145
- const r = await api('GET', '/api/sessions');
146
- S.sessions.value = r.sessions || [];
147
- try { localStorage.setItem('ccsm.sessions-cache', JSON.stringify(S.sessions.value)); } catch {}
148
- }
149
-
150
- export async function loadFolders() {
151
- const r = await api('GET', '/api/folders');
152
- S.folders.value = (r.folders || []).sort((a, b) => (a.order || 0) - (b.order || 0));
153
- try { localStorage.setItem('ccsm.folders-cache', JSON.stringify(S.folders.value)); } catch {}
154
- }
155
-
156
- export async function createFolder(name) {
157
- const r = await api('POST', '/api/folders', { name });
158
- await loadFolders();
159
- return r.folder;
160
- }
161
-
162
- export async function renameFolder(id, name) {
163
- const r = await api('PUT', `/api/folders/${id}`, { name });
164
- await loadFolders();
165
- return r.folder;
166
- }
167
-
168
- export async function deleteFolder(id) {
169
- await api('DELETE', `/api/folders/${id}`);
170
- await Promise.all([loadFolders(), loadSessions()]);
171
- }
172
-
173
- export async function reorderFolders(ids) {
174
- const r = await api('POST', '/api/folders/reorder', { ids });
175
- await loadFolders();
176
- return r.folders;
177
- }
178
-
179
- export async function setSessionFolder(sessionId, folderId) {
180
- await api('PUT', `/api/sessions/${sessionId}`, { folderId: folderId || null });
181
- await loadSessions();
182
- }
183
-
184
- export async function setSessionTitle(sessionId, title) {
185
- await api('PUT', `/api/sessions/${sessionId}`, { title });
186
- await loadSessions();
187
- }
188
-
189
- export async function deleteSession(sessionId) {
190
- await api('DELETE', `/api/sessions/${sessionId}`);
191
- await loadSessions();
192
- }
193
-
194
- // Per-session in-flight resume promise. Sidebar.onClick and the
195
- // SessionsPage auto-resume effect can both fire for the same exited
196
- // session in the same tick (clicking an exited row mounts SessionsPage
197
- // which runs its effect AND awaits Sidebar's own POST). Without this
198
- // dedup the backend gets two concurrent /resume requests and may spawn
199
- // two PTYs against the same record. Cleared on resolve/reject.
200
- const resumeInFlight = new Map(); // sessionId → Promise
201
-
202
- export function resumeSession(sessionId) {
203
- const cached = resumeInFlight.get(sessionId);
204
- if (cached) return cached;
205
- const p = (async () => {
206
- const r = await api('POST', `/api/sessions/${sessionId}/resume`);
207
- await loadSessions();
208
- return r.launched;
209
- })();
210
- resumeInFlight.set(sessionId, p);
211
- p.finally(() => { resumeInFlight.delete(sessionId); });
212
- return p;
213
- }
214
-
215
- export async function loadWorkspaces() {
216
- const r = await api('GET', '/api/workspaces');
217
- S.workspaces.value = r.workspaces;
218
- }
219
-
220
- export async function deleteWorkspace(name) {
221
- await api('DELETE', `/api/workspaces/${encodeURIComponent(name)}`);
222
- }
223
-
224
- export async function refreshAll() {
225
- await Promise.all([
226
- loadSessions(),
227
- loadFolders(),
228
- loadWorkspaces(),
229
- ]);
230
- S.lastRefreshAt.value = Date.now();
231
- }
232
-
233
- // List existing CLI sessions discovered on disk for a given cli type.
234
- // Returns array of { cliType, cliSessionId, cwd, mtime, summary, adopted }.
235
- export async function listLocalCliSessions(cliType) {
236
- const r = await api('GET', `/api/cli-sessions/${cliType}`);
237
- return r.sessions || [];
238
- }
239
-
240
- // Adopt an existing upstream CLI session into ccsm. Returns the created
241
- // (or existing) persistedSessions record.
242
- export async function adoptSession({ cliId, cliSessionId, cwd, title, folderId }) {
243
- const r = await api('POST', '/api/sessions/adopt', { cliId, cliSessionId, cwd, title, folderId });
244
- return r;
245
- }
246
-
247
- export async function pollHealth() {
248
- const ctrl = new AbortController();
249
- const t = setTimeout(() => ctrl.abort(), 3000);
250
- try {
251
- const r = await fetch(httpBase() + '/api/health', { signal: ctrl.signal });
252
- if (!r.ok) throw new Error(`HTTP ${r.status}`);
253
- const j = await r.json();
254
- S.serverHealth.value = { state: 'online', version: j.version, pid: j.pid };
255
- } catch (e) {
256
- S.serverHealth.value = { state: 'offline', error: String(e.message || e) };
257
- } finally {
258
- clearTimeout(t);
259
- }
260
- }
1
+ // Fetch wrapper + every loader. Loaders push into signals from ./state.js.
2
+ // Cross-origin (hosted frontend → local backend) flows through httpBase().
3
+
4
+ import * as S from './state.js';
5
+ import { httpBase } from './backend.js';
6
+
7
+ export async function api(method, url, body) {
8
+ const opts = { method, headers: { 'Content-Type': 'application/json' } };
9
+ if (body !== undefined) opts.body = JSON.stringify(body);
10
+ const r = await fetch(httpBase() + url, opts);
11
+ const text = await r.text();
12
+ let json;
13
+ try { json = text ? JSON.parse(text) : {}; } catch { json = { raw: text }; }
14
+ if (!r.ok) throw new Error(json.error || `HTTP ${r.status}`);
15
+ return json;
16
+ }
17
+
18
+ export async function loadConfig() {
19
+ const [cfg, caps] = await Promise.all([
20
+ api('GET', '/api/config'),
21
+ api('GET', '/api/capabilities').catch(() => ({ webTerminal: false })),
22
+ ]);
23
+ S.config.value = cfg;
24
+ S.capabilities.value = caps;
25
+ }
26
+
27
+ // Update an existing CLI by id. patch is shallow-merged into the record.
28
+ export async function updateCli(id, patch) {
29
+ const cfg = S.config.value || (await api('GET', '/api/config'));
30
+ const target = (cfg.clis || []).find((c) => c.id === id);
31
+ // Built-in CLIs lock down identity-defining fields. UI already greys these
32
+ // out; we belt-and-braces here so a tampered request from elsewhere
33
+ // can't change them either.
34
+ if (target?.builtin) {
35
+ delete patch.command;
36
+ delete patch.id;
37
+ delete patch.builtin;
38
+ }
39
+ const toArr = (v, fallback) => Array.isArray(v) ? v :
40
+ typeof v === 'string' ? v.split(/\s+/).filter(Boolean) : fallback;
41
+ const next = {
42
+ ...cfg,
43
+ clis: (cfg.clis || []).map((c) => c.id === id ? {
44
+ ...c, ...patch,
45
+ args: toArr(patch.args, c.args),
46
+ resumeArgs: toArr(patch.resumeArgs, c.resumeArgs || []),
47
+ shell: ['direct', 'pwsh', 'cmd'].includes(patch.shell ?? c.shell) ? (patch.shell ?? c.shell) : 'direct',
48
+ } : c),
49
+ };
50
+ const saved = await api('PUT', '/api/config', next);
51
+ S.config.value = saved;
52
+ return id;
53
+ }
54
+
55
+ export async function deleteCli(id) {
56
+ const cfg = S.config.value || (await api('GET', '/api/config'));
57
+ const target = (cfg.clis || []).find((c) => c.id === id);
58
+ if (target?.builtin) throw new Error(`"${target.name}" is built-in and can't be deleted`);
59
+ const clis = (cfg.clis || []).filter((c) => c.id !== id);
60
+ if (clis.length === 0) throw new Error('cannot delete the last CLI');
61
+ const next = { ...cfg, clis };
62
+ if (next.defaultCliId === id) next.defaultCliId = clis[0].id;
63
+ const saved = await api('PUT', '/api/config', next);
64
+ S.config.value = saved;
65
+ }
66
+
67
+ export async function updateRepo(name, patch) {
68
+ const cfg = S.config.value || (await api('GET', '/api/config'));
69
+ const next = {
70
+ ...cfg,
71
+ repos: (cfg.repos || []).map((r) => r.name === name ? {
72
+ ...r,
73
+ name: (patch.name ?? r.name).trim(),
74
+ url: (patch.url ?? r.url).trim(),
75
+ defaultSelected: patch.defaultSelected ?? r.defaultSelected,
76
+ } : r),
77
+ };
78
+ const saved = await api('PUT', '/api/config', next);
79
+ S.config.value = saved;
80
+ }
81
+
82
+ export async function deleteRepo(name) {
83
+ const cfg = S.config.value || (await api('GET', '/api/config'));
84
+ const next = { ...cfg, repos: (cfg.repos || []).filter((r) => r.name !== name) };
85
+ const saved = await api('PUT', '/api/config', next);
86
+ S.config.value = saved;
87
+ }
88
+
89
+ export async function setDefaultCli(id) {
90
+ const cfg = S.config.value || (await api('GET', '/api/config'));
91
+ const saved = await api('PUT', '/api/config', { ...cfg, defaultCliId: id });
92
+ S.config.value = saved;
93
+ }
94
+
95
+ // Add a new CLI to config.clis and return its id. Generates a fresh id
96
+ // from the command name + an integer suffix when collisions exist.
97
+ export async function createCli({ name, command, args, resumeArgs, shell, type }) {
98
+ const cfg = S.config.value || (await api('GET', '/api/config'));
99
+ const base = (name || command || 'cli').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'cli';
100
+ let id = base, n = 1;
101
+ while ((cfg.clis || []).some((c) => c.id === id)) { id = `${base}-${++n}`; }
102
+ const toArr = (v) => Array.isArray(v) ? v : (typeof v === 'string' ? v.split(/\s+/).filter(Boolean) : []);
103
+ const next = {
104
+ ...cfg,
105
+ clis: [...(cfg.clis || []), {
106
+ id,
107
+ name: (name || command || id).trim(),
108
+ command: (command || '').trim(),
109
+ args: toArr(args),
110
+ resumeArgs: toArr(resumeArgs),
111
+ shell: ['direct', 'pwsh', 'cmd'].includes(shell) ? shell : 'direct',
112
+ type: ['claude', 'codex', 'other'].includes(type) ? type : 'other',
113
+ }],
114
+ };
115
+ const saved = await api('PUT', '/api/config', next);
116
+ S.config.value = saved;
117
+ return id;
118
+ }
119
+
120
+ // Add a new repo to config.repos. Repos are addressed by name (which must
121
+ // be unique). Returns the name on success, throws on duplicate.
122
+ export async function createRepo({ name, url, defaultSelected }) {
123
+ const cfg = S.config.value || (await api('GET', '/api/config'));
124
+ const cleanName = (name || '').trim();
125
+ const cleanUrl = (url || '').trim();
126
+ if (!cleanName) throw new Error('repo name required');
127
+ if (!cleanUrl) throw new Error('repo url required');
128
+ if ((cfg.repos || []).some((r) => r.name === cleanName)) {
129
+ throw new Error(`repo "${cleanName}" already exists`);
130
+ }
131
+ const next = {
132
+ ...cfg,
133
+ repos: [...(cfg.repos || []), {
134
+ name: cleanName,
135
+ url: cleanUrl,
136
+ defaultSelected: !!defaultSelected,
137
+ }],
138
+ };
139
+ const saved = await api('PUT', '/api/config', next);
140
+ S.config.value = saved;
141
+ return cleanName;
142
+ }
143
+
144
+ export async function loadSessions() {
145
+ const r = await api('GET', '/api/sessions');
146
+ S.sessions.value = r.sessions || [];
147
+ try { localStorage.setItem('ccsm.sessions-cache', JSON.stringify(S.sessions.value)); } catch {}
148
+ }
149
+
150
+ export async function loadFolders() {
151
+ const r = await api('GET', '/api/folders');
152
+ S.folders.value = (r.folders || []).sort((a, b) => (a.order || 0) - (b.order || 0));
153
+ try { localStorage.setItem('ccsm.folders-cache', JSON.stringify(S.folders.value)); } catch {}
154
+ }
155
+
156
+ export async function createFolder(name) {
157
+ const r = await api('POST', '/api/folders', { name });
158
+ await loadFolders();
159
+ return r.folder;
160
+ }
161
+
162
+ export async function renameFolder(id, name) {
163
+ const r = await api('PUT', `/api/folders/${id}`, { name });
164
+ await loadFolders();
165
+ return r.folder;
166
+ }
167
+
168
+ export async function deleteFolder(id) {
169
+ await api('DELETE', `/api/folders/${id}`);
170
+ await Promise.all([loadFolders(), loadSessions()]);
171
+ }
172
+
173
+ export async function reorderFolders(ids) {
174
+ const r = await api('POST', '/api/folders/reorder', { ids });
175
+ await loadFolders();
176
+ return r.folders;
177
+ }
178
+
179
+ export async function setSessionFolder(sessionId, folderId) {
180
+ await api('PUT', `/api/sessions/${sessionId}`, { folderId: folderId || null });
181
+ await loadSessions();
182
+ }
183
+
184
+ export async function setSessionTitle(sessionId, title) {
185
+ await api('PUT', `/api/sessions/${sessionId}`, { title });
186
+ await loadSessions();
187
+ }
188
+
189
+ export async function deleteSession(sessionId) {
190
+ await api('DELETE', `/api/sessions/${sessionId}`);
191
+ await loadSessions();
192
+ }
193
+
194
+ // Per-session in-flight resume promise. Sidebar.onClick and the
195
+ // SessionsPage auto-resume effect can both fire for the same exited
196
+ // session in the same tick (clicking an exited row mounts SessionsPage
197
+ // which runs its effect AND awaits Sidebar's own POST). Without this
198
+ // dedup the backend gets two concurrent /resume requests and may spawn
199
+ // two PTYs against the same record. Cleared on resolve/reject.
200
+ const resumeInFlight = new Map(); // sessionId → Promise
201
+
202
+ export function resumeSession(sessionId) {
203
+ const cached = resumeInFlight.get(sessionId);
204
+ if (cached) return cached;
205
+ const p = (async () => {
206
+ const r = await api('POST', `/api/sessions/${sessionId}/resume`);
207
+ await loadSessions();
208
+ return r.launched;
209
+ })();
210
+ resumeInFlight.set(sessionId, p);
211
+ p.finally(() => { resumeInFlight.delete(sessionId); });
212
+ return p;
213
+ }
214
+
215
+ export async function loadWorkspaces() {
216
+ const r = await api('GET', '/api/workspaces');
217
+ S.workspaces.value = r.workspaces;
218
+ }
219
+
220
+ export async function deleteWorkspace(name) {
221
+ await api('DELETE', `/api/workspaces/${encodeURIComponent(name)}`);
222
+ }
223
+
224
+ export async function refreshAll() {
225
+ await Promise.all([
226
+ loadSessions(),
227
+ loadFolders(),
228
+ loadWorkspaces(),
229
+ ]);
230
+ S.lastRefreshAt.value = Date.now();
231
+ }
232
+
233
+ // List existing CLI sessions discovered on disk for a given cli type.
234
+ // Paginated: page 0 returns all currently-active sessions + the first
235
+ // `limit` non-active (sorted mtime desc). Subsequent pages return the
236
+ // next slice of non-active sessions.
237
+ // Returns { sessions, totalActive, totalNonActive, total, offset, limit, hasMore }.
238
+ export async function listLocalCliSessions(cliType, { offset = 0, limit = 30 } = {}) {
239
+ const qs = `offset=${offset}&limit=${limit}`;
240
+ const r = await api('GET', `/api/cli-sessions/${cliType}?${qs}`);
241
+ return {
242
+ sessions: r.sessions || [],
243
+ totalActive: r.totalActive ?? 0,
244
+ totalNonActive: r.totalNonActive ?? 0,
245
+ total: r.total ?? (r.sessions?.length || 0),
246
+ offset: r.offset ?? offset,
247
+ limit: r.limit ?? limit,
248
+ hasMore: !!r.hasMore,
249
+ };
250
+ }
251
+
252
+ // Adopt an existing upstream CLI session into ccsm. Returns the created
253
+ // (or existing) persistedSessions record.
254
+ export async function adoptSession({ cliId, cliSessionId, cwd, title, folderId }) {
255
+ const r = await api('POST', '/api/sessions/adopt', { cliId, cliSessionId, cwd, title, folderId });
256
+ return r;
257
+ }
258
+
259
+ export async function pollHealth() {
260
+ const ctrl = new AbortController();
261
+ const t = setTimeout(() => ctrl.abort(), 3000);
262
+ try {
263
+ const r = await fetch(httpBase() + '/api/health', { signal: ctrl.signal });
264
+ if (!r.ok) throw new Error(`HTTP ${r.status}`);
265
+ const j = await r.json();
266
+ S.serverHealth.value = { state: 'online', version: j.version, pid: j.pid };
267
+ } catch (e) {
268
+ S.serverHealth.value = { state: 'offline', error: String(e.message || e) };
269
+ } finally {
270
+ clearTimeout(t);
271
+ }
272
+ }