@web-auto/camo 0.2.0 → 0.2.2

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 (114) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +586 -586
  3. package/bin/browser-service.mjs +11 -11
  4. package/bin/camo.mjs +22 -22
  5. package/package.json +48 -48
  6. package/scripts/build.mjs +19 -19
  7. package/scripts/bump-version.mjs +34 -34
  8. package/scripts/check-file-size.mjs +80 -80
  9. package/scripts/file-size-policy.json +12 -2
  10. package/scripts/install.mjs +76 -76
  11. package/scripts/release.sh +54 -54
  12. package/src/autoscript/action-providers/index.mjs +6 -6
  13. package/src/autoscript/impact-engine.mjs +78 -78
  14. package/src/autoscript/runtime.mjs +1017 -1017
  15. package/src/autoscript/schema.mjs +376 -376
  16. package/src/cli.mjs +405 -405
  17. package/src/commands/attach.mjs +141 -141
  18. package/src/commands/autoscript.mjs +1011 -1011
  19. package/src/commands/browser.mjs +1255 -1255
  20. package/src/commands/container.mjs +401 -401
  21. package/src/commands/cookies.mjs +69 -69
  22. package/src/commands/create.mjs +98 -98
  23. package/src/commands/devtools.mjs +349 -349
  24. package/src/commands/events.mjs +152 -152
  25. package/src/commands/highlight-mode.mjs +24 -24
  26. package/src/commands/init.mjs +68 -68
  27. package/src/commands/lifecycle.mjs +275 -275
  28. package/src/commands/mouse.mjs +45 -45
  29. package/src/commands/profile.mjs +46 -46
  30. package/src/commands/record.mjs +115 -115
  31. package/src/commands/system.mjs +14 -14
  32. package/src/commands/window.mjs +123 -123
  33. package/src/container/change-notifier.mjs +362 -362
  34. package/src/container/element-filter.mjs +143 -143
  35. package/src/container/index.mjs +3 -3
  36. package/src/container/runtime-core/checkpoint.mjs +209 -209
  37. package/src/container/runtime-core/index.mjs +21 -21
  38. package/src/container/runtime-core/operations/index.mjs +774 -774
  39. package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
  40. package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
  41. package/src/container/runtime-core/operations/viewport.mjs +189 -189
  42. package/src/container/runtime-core/search.mjs +190 -190
  43. package/src/container/runtime-core/subscription.mjs +224 -224
  44. package/src/container/runtime-core/utils.mjs +94 -94
  45. package/src/container/runtime-core/validation.mjs +127 -127
  46. package/src/container/runtime-core.mjs +1 -1
  47. package/src/container/subscription-registry.mjs +459 -459
  48. package/src/core/actions.mjs +561 -561
  49. package/src/core/browser.mjs +266 -266
  50. package/src/core/index.mjs +52 -52
  51. package/src/core/utils.mjs +91 -91
  52. package/src/events/daemon-entry.mjs +33 -33
  53. package/src/events/daemon.mjs +80 -80
  54. package/src/events/progress-log.mjs +109 -109
  55. package/src/events/ws-server.mjs +239 -239
  56. package/src/lib/client.mjs +200 -200
  57. package/src/lifecycle/cleanup.mjs +83 -83
  58. package/src/lifecycle/lock.mjs +126 -126
  59. package/src/lifecycle/session-registry.mjs +279 -279
  60. package/src/lifecycle/session-view.mjs +76 -76
  61. package/src/lifecycle/session-watchdog.mjs +281 -281
  62. package/src/services/browser-service/index.js +671 -671
  63. package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
  64. package/src/services/browser-service/internal/BrowserSession.js +325 -304
  65. package/src/services/browser-service/internal/ElementRegistry.js +60 -60
  66. package/src/services/browser-service/internal/ProfileLock.js +84 -84
  67. package/src/services/browser-service/internal/SessionManager.js +184 -184
  68. package/src/services/browser-service/internal/SessionManager.test.js +39 -39
  69. package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
  70. package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
  71. package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
  72. package/src/services/browser-service/internal/browser-session/logging.js +46 -46
  73. package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
  74. package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
  75. package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
  76. package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
  77. package/src/services/browser-service/internal/browser-session/recording.js +198 -198
  78. package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
  79. package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
  80. package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
  81. package/src/services/browser-service/internal/browser-session/types.js +14 -14
  82. package/src/services/browser-service/internal/browser-session/utils.js +95 -95
  83. package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
  84. package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
  85. package/src/services/browser-service/internal/container-matcher.js +851 -851
  86. package/src/services/browser-service/internal/container-registry.js +182 -182
  87. package/src/services/browser-service/internal/engine-manager.js +259 -259
  88. package/src/services/browser-service/internal/fingerprint.js +203 -203
  89. package/src/services/browser-service/internal/heartbeat.js +137 -137
  90. package/src/services/browser-service/internal/logging.js +46 -46
  91. package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
  92. package/src/services/browser-service/internal/pageRuntime.js +28 -28
  93. package/src/services/browser-service/internal/runtimeInjector.js +31 -31
  94. package/src/services/browser-service/internal/service-process-logger.js +140 -140
  95. package/src/services/browser-service/internal/state-bus.js +45 -45
  96. package/src/services/browser-service/internal/storage-paths.js +42 -42
  97. package/src/services/browser-service/internal/ws-server.js +1194 -1194
  98. package/src/services/browser-service/internal/ws-server.test.js +58 -58
  99. package/src/services/browser-service/server.mjs +6 -6
  100. package/src/services/controller/cli-bridge.js +93 -93
  101. package/src/services/controller/container-index.js +50 -50
  102. package/src/services/controller/container-storage.js +36 -36
  103. package/src/services/controller/controller-actions.js +207 -207
  104. package/src/services/controller/controller.js +1138 -1138
  105. package/src/services/controller/selectors.js +54 -54
  106. package/src/services/controller/transport.js +125 -125
  107. package/src/utils/args.mjs +26 -26
  108. package/src/utils/browser-service.mjs +544 -544
  109. package/src/utils/command-log.mjs +64 -64
  110. package/src/utils/config.mjs +214 -214
  111. package/src/utils/fingerprint.mjs +181 -181
  112. package/src/utils/help.mjs +216 -216
  113. package/src/utils/js-policy.mjs +13 -13
  114. package/src/utils/ws-client.mjs +30 -30
@@ -1,459 +1,459 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { CONFIG_DIR, loadConfig } from '../utils/config.mjs';
4
-
5
- const CONTAINER_ROOT_ENV = process.env.CAMO_CONTAINER_ROOT;
6
-
7
- export const USER_CONTAINER_ROOT = CONTAINER_ROOT_ENV || path.join(CONFIG_DIR, 'container-lib');
8
- export const SUBSCRIPTION_ROOT = path.join(CONFIG_DIR, 'container-subscriptions');
9
- export const SUBSCRIPTION_SETS_DIR = path.join(SUBSCRIPTION_ROOT, 'sets');
10
- export const SUBSCRIPTION_INDEX_FILE = path.join(SUBSCRIPTION_ROOT, 'index.json');
11
- export const SUBSCRIPTION_TARGETS_FILE = path.join(SUBSCRIPTION_ROOT, 'targets.json');
12
-
13
- function resolveStoragePaths(options = {}) {
14
- const userContainerRoot = options.userContainerRoot || USER_CONTAINER_ROOT;
15
- const subscriptionRoot = options.subscriptionRoot || SUBSCRIPTION_ROOT;
16
- const setsDir = options.setsDir || path.join(subscriptionRoot, 'sets');
17
- const indexFile = options.indexFile || path.join(subscriptionRoot, 'index.json');
18
- const targetsFile = options.targetsFile || path.join(subscriptionRoot, 'targets.json');
19
- return { userContainerRoot, subscriptionRoot, setsDir, indexFile, targetsFile };
20
- }
21
-
22
- function ensureDir(dirPath) {
23
- if (!fs.existsSync(dirPath)) {
24
- fs.mkdirSync(dirPath, { recursive: true });
25
- }
26
- }
27
-
28
- function toJsonFile(filePath, data) {
29
- ensureDir(path.dirname(filePath));
30
- fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`, 'utf8');
31
- }
32
-
33
- function readJsonFile(filePath, fallback = null) {
34
- try {
35
- if (!fs.existsSync(filePath)) return fallback;
36
- return JSON.parse(fs.readFileSync(filePath, 'utf8'));
37
- } catch {
38
- return fallback;
39
- }
40
- }
41
-
42
- function splitLegacySelector(rawSelector) {
43
- if (typeof rawSelector !== 'string') return [];
44
- return rawSelector
45
- .split(',')
46
- .map((item) => item.trim())
47
- .filter(Boolean);
48
- }
49
-
50
- function normalizeSelectors(rawSelectors) {
51
- const out = [];
52
- const pushCss = (css, extra = {}) => {
53
- if (typeof css !== 'string' || !css.trim()) return;
54
- out.push({
55
- css: css.trim(),
56
- ...(typeof extra.variant === 'string' ? { variant: extra.variant } : {}),
57
- ...(Number.isFinite(Number(extra.score)) ? { score: Number(extra.score) } : {}),
58
- });
59
- };
60
-
61
- if (Array.isArray(rawSelectors)) {
62
- for (const item of rawSelectors) {
63
- if (typeof item === 'string') {
64
- for (const css of splitLegacySelector(item)) {
65
- pushCss(css);
66
- }
67
- continue;
68
- }
69
- if (item && typeof item === 'object') {
70
- if (typeof item.css === 'string') {
71
- pushCss(item.css, item);
72
- } else if (typeof item.selector === 'string') {
73
- for (const css of splitLegacySelector(item.selector)) {
74
- pushCss(css, item);
75
- }
76
- } else if (typeof item.id === 'string' && item.id) {
77
- pushCss(`#${item.id}`, item);
78
- } else if (Array.isArray(item.classes) && item.classes.length > 0) {
79
- pushCss(`.${item.classes.filter(Boolean).join('.')}`, item);
80
- }
81
- }
82
- }
83
- } else if (typeof rawSelectors === 'string') {
84
- for (const css of splitLegacySelector(rawSelectors)) {
85
- pushCss(css);
86
- }
87
- } else if (rawSelectors && typeof rawSelectors === 'object') {
88
- if (typeof rawSelectors.css === 'string') {
89
- pushCss(rawSelectors.css, rawSelectors);
90
- } else if (typeof rawSelectors.selector === 'string') {
91
- for (const css of splitLegacySelector(rawSelectors.selector)) {
92
- pushCss(css, rawSelectors);
93
- }
94
- }
95
- }
96
-
97
- const dedup = new Map();
98
- for (const item of out) {
99
- const key = `${item.css}::${item.variant || ''}::${item.score || ''}`;
100
- if (!dedup.has(key)) dedup.set(key, item);
101
- }
102
- return Array.from(dedup.values());
103
- }
104
-
105
- function normalizeChildren(rawChildren) {
106
- if (!Array.isArray(rawChildren)) return [];
107
- return rawChildren.filter((item) => typeof item === 'string' && item.trim()).map((item) => item.trim());
108
- }
109
-
110
- function normalizePagePatterns(raw) {
111
- const out = [];
112
- const fromArray = Array.isArray(raw.page_patterns) ? raw.page_patterns : [];
113
- const fromCamel = Array.isArray(raw.pagePatterns) ? raw.pagePatterns : [];
114
- const fromUrl = typeof raw.page_url === 'string' && raw.page_url.trim() ? [raw.page_url.trim()] : [];
115
- for (const item of [...fromArray, ...fromCamel, ...fromUrl]) {
116
- if (typeof item === 'string' && item.trim()) out.push(item.trim());
117
- }
118
- return Array.from(new Set(out));
119
- }
120
-
121
- function parseCssFingerprint(css) {
122
- const normalizedCss = typeof css === 'string' ? css.trim() : '';
123
- if (!normalizedCss) {
124
- return { css: null, tag: null, id: null, classes: [] };
125
- }
126
- const tagMatch = normalizedCss.match(/^[a-zA-Z][\w-]*/);
127
- const idMatch = normalizedCss.match(/#([\w-]+)/);
128
- const classMatches = normalizedCss.match(/\.([\w-]+)/g) || [];
129
- return {
130
- css: normalizedCss,
131
- tag: tagMatch ? tagMatch[0].toLowerCase() : null,
132
- id: idMatch ? idMatch[1] : null,
133
- classes: classMatches.map((item) => item.slice(1)),
134
- };
135
- }
136
-
137
- function sanitizeFileName(name) {
138
- return String(name || '')
139
- .replace(/[^\w.-]/g, '_')
140
- .slice(0, 180);
141
- }
142
-
143
- function inferSiteKey(containerId, fallbackSiteKey) {
144
- if (typeof containerId === 'string') {
145
- const under = containerId.match(/^([a-z0-9-]+)_/i);
146
- if (under?.[1]) return under[1].toLowerCase();
147
- }
148
- return String(fallbackSiteKey || 'default').toLowerCase();
149
- }
150
-
151
- function buildSetDefinition({ id, siteKey, sourceRelPath, raw }) {
152
- const selectors = normalizeSelectors(raw.selectors || raw.selector);
153
- const pagePatterns = normalizePagePatterns(raw);
154
- const sourceKind = sourceRelPath.endsWith('containers.json') ? 'legacy-map' : 'tree-v2';
155
- const containerPath = sourceKind === 'tree-v2'
156
- ? sourceRelPath.replace(/\/container\.json$/, '')
157
- : `${siteKey}/${id.replace(/\./g, '/')}`;
158
-
159
- const markers = [
160
- {
161
- markerType: 'path',
162
- siteKey: inferSiteKey(id, siteKey),
163
- containerId: id,
164
- containerPath,
165
- },
166
- ...selectors.map((selectorDef, idx) => ({
167
- markerType: 'url_dom',
168
- siteKey: inferSiteKey(id, siteKey),
169
- containerId: id,
170
- markerId: `${id}#${idx + 1}`,
171
- urlPatterns: pagePatterns,
172
- dom: {
173
- ...parseCssFingerprint(selectorDef.css),
174
- variant: selectorDef.variant || null,
175
- score: Number.isFinite(Number(selectorDef.score)) ? Number(selectorDef.score) : null,
176
- },
177
- })),
178
- ];
179
-
180
- return {
181
- id,
182
- siteKey: inferSiteKey(id, siteKey),
183
- name: raw.name || id,
184
- type: raw.type || 'container',
185
- pagePatterns,
186
- selectors,
187
- selectorCount: selectors.length,
188
- children: normalizeChildren(raw.children),
189
- capabilities: Array.isArray(raw.capabilities) ? raw.capabilities : [],
190
- markers,
191
- markerCount: markers.length,
192
- metadata: raw.metadata && typeof raw.metadata === 'object' ? raw.metadata : {},
193
- source: {
194
- kind: sourceKind,
195
- path: sourceRelPath,
196
- },
197
- };
198
- }
199
-
200
- function collectJsonFiles(dirPath) {
201
- const out = [];
202
- const stack = [dirPath];
203
- while (stack.length > 0) {
204
- const dir = stack.pop();
205
- let entries = [];
206
- try {
207
- entries = fs.readdirSync(dir, { withFileTypes: true });
208
- } catch {
209
- continue;
210
- }
211
- for (const entry of entries) {
212
- const fullPath = path.join(dir, entry.name);
213
- if (entry.isDirectory()) {
214
- stack.push(fullPath);
215
- } else if (entry.isFile() && entry.name.endsWith('.json')) {
216
- out.push(fullPath);
217
- }
218
- }
219
- }
220
- return out;
221
- }
222
-
223
- function detectContainerLibraryRoot(explicitRoot) {
224
- if (explicitRoot) return explicitRoot;
225
- const cfg = loadConfig();
226
- const root = cfg.repoRoot || process.env.CAMO_CONTAINER_ROOT || process.cwd();
227
- if (!root) return null;
228
- const candidate = path.join(root, 'container-library');
229
- return fs.existsSync(candidate) ? candidate : null;
230
- }
231
-
232
- function bootstrapUserContainerRoot(containerLibraryRoot, options = {}) {
233
- const force = options.force === true;
234
- const userContainerRoot = options.userContainerRoot || USER_CONTAINER_ROOT;
235
- ensureDir(userContainerRoot);
236
- const topLevel = fs.readdirSync(containerLibraryRoot, { withFileTypes: true });
237
- let copiedEntries = 0;
238
- for (const entry of topLevel) {
239
- if (!entry.isDirectory()) continue;
240
- const src = path.join(containerLibraryRoot, entry.name);
241
- const dst = path.join(userContainerRoot, entry.name);
242
- if (fs.existsSync(dst) && !force) continue;
243
- fs.cpSync(src, dst, { recursive: true, force });
244
- copiedEntries += 1;
245
- }
246
- return { root: userContainerRoot, copiedEntries };
247
- }
248
-
249
- export function initContainerSubscriptionDirectory(options = {}) {
250
- const storage = resolveStoragePaths(options);
251
- const containerLibraryRoot = detectContainerLibraryRoot(options.containerLibraryRoot);
252
- if (!containerLibraryRoot || !fs.existsSync(containerLibraryRoot)) {
253
- throw new Error(
254
- 'container-library not found. Set CAMO_CONTAINER_ROOT or run `camo config repo-root <path>` first.',
255
- );
256
- }
257
-
258
- ensureDir(storage.subscriptionRoot);
259
- ensureDir(storage.setsDir);
260
- const boot = bootstrapUserContainerRoot(containerLibraryRoot, {
261
- force: options.force === true,
262
- userContainerRoot: storage.userContainerRoot,
263
- });
264
-
265
- const jsonFiles = collectJsonFiles(containerLibraryRoot);
266
- const setMap = new Map();
267
-
268
- for (const filePath of jsonFiles) {
269
- const rel = path.relative(containerLibraryRoot, filePath).replace(/\\/g, '/');
270
- const raw = readJsonFile(filePath, null);
271
- if (!raw || typeof raw !== 'object') continue;
272
- const siteKey = rel.split('/')[0] || 'default';
273
-
274
- if (filePath.endsWith(`${path.sep}container.json`) || rel.endsWith('/container.json')) {
275
- const id = typeof raw.id === 'string' && raw.id.trim() ? raw.id.trim() : null;
276
- if (!id) continue;
277
- setMap.set(id, buildSetDefinition({ id, siteKey, sourceRelPath: rel, raw }));
278
- continue;
279
- }
280
-
281
- if (filePath.endsWith(`${path.sep}containers.json`) || rel.endsWith('/containers.json')) {
282
- const containers = raw.containers;
283
- if (!containers || typeof containers !== 'object') continue;
284
- for (const [id, def] of Object.entries(containers)) {
285
- if (!id || !def || typeof def !== 'object') continue;
286
- if (setMap.has(id)) continue;
287
- setMap.set(id, buildSetDefinition({ id, siteKey, sourceRelPath: rel, raw: def }));
288
- }
289
- }
290
- }
291
-
292
- const sets = Array.from(setMap.values()).sort((a, b) => a.id.localeCompare(b.id));
293
- for (const set of sets) {
294
- const fileName = `${sanitizeFileName(set.id)}.json`;
295
- toJsonFile(path.join(storage.setsDir, fileName), set);
296
- }
297
-
298
- const bySite = {};
299
- for (const set of sets) {
300
- if (!bySite[set.siteKey]) bySite[set.siteKey] = [];
301
- bySite[set.siteKey].push(set.id);
302
- }
303
-
304
- const index = {
305
- version: 1,
306
- generatedAt: new Date().toISOString(),
307
- sourceRoot: containerLibraryRoot,
308
- userContainerRoot: storage.userContainerRoot,
309
- setCount: sets.length,
310
- sites: Object.fromEntries(
311
- Object.entries(bySite).map(([site, ids]) => [site, { count: ids.length, sets: ids.sort() }]),
312
- ),
313
- sets: sets.map((set) => ({
314
- id: set.id,
315
- siteKey: set.siteKey,
316
- name: set.name,
317
- selectorCount: set.selectorCount,
318
- markerCount: set.markerCount,
319
- source: set.source,
320
- file: `sets/${sanitizeFileName(set.id)}.json`,
321
- })),
322
- };
323
- toJsonFile(storage.indexFile, index);
324
-
325
- return {
326
- ok: true,
327
- containerLibraryRoot,
328
- userContainerRoot: boot.root,
329
- copiedEntries: boot.copiedEntries,
330
- subscriptionRoot: storage.subscriptionRoot,
331
- setCount: sets.length,
332
- siteCount: Object.keys(bySite).length,
333
- index: storage.indexFile,
334
- };
335
- }
336
-
337
- export function listSubscriptionSets(options = {}) {
338
- const storage = resolveStoragePaths(options);
339
- const index = readJsonFile(storage.indexFile, null);
340
- if (!index?.sets) {
341
- throw new Error('Subscription index not found. Run: camo container init');
342
- }
343
- const site = options.site ? String(options.site).toLowerCase() : null;
344
- const sets = site
345
- ? index.sets.filter((item) => String(item.siteKey).toLowerCase() === site)
346
- : index.sets;
347
- return { ok: true, site, count: sets.length, sets };
348
- }
349
-
350
- function loadSubscriptionSetById(setId, options = {}) {
351
- const storage = resolveStoragePaths(options);
352
- const filePath = path.join(storage.setsDir, `${sanitizeFileName(setId)}.json`);
353
- return readJsonFile(filePath, null);
354
- }
355
-
356
- function readTargetsDoc(options = {}) {
357
- const storage = resolveStoragePaths(options);
358
- return readJsonFile(storage.targetsFile, {
359
- version: 1,
360
- updatedAt: null,
361
- profiles: {},
362
- });
363
- }
364
-
365
- export function registerSubscriptionTargets(profileId, setIds, options = {}) {
366
- if (!profileId || typeof profileId !== 'string') {
367
- throw new Error('profileId is required');
368
- }
369
- if (!Array.isArray(setIds) || setIds.length === 0) {
370
- throw new Error('at least one setId is required');
371
- }
372
-
373
- const storage = resolveStoragePaths(options);
374
- const doc = readTargetsDoc(options);
375
- const existing = doc.profiles[profileId] || { setIds: [], targets: [] };
376
- const resolvedSetIds = options.append ? new Set(existing.setIds || []) : new Set();
377
- for (const id of setIds) {
378
- resolvedSetIds.add(id);
379
- }
380
-
381
- const missingSetIds = [];
382
- const targets = [];
383
- for (const id of Array.from(resolvedSetIds)) {
384
- const setDef = loadSubscriptionSetById(id, options);
385
- if (!setDef) {
386
- missingSetIds.push(id);
387
- continue;
388
- }
389
- const markerDefs = Array.isArray(setDef.markers) ? setDef.markers : [];
390
- for (const marker of markerDefs) {
391
- if (!marker || typeof marker !== 'object') continue;
392
- targets.push({
393
- setId: setDef.id,
394
- siteKey: setDef.siteKey,
395
- markerType: marker.markerType || 'path',
396
- containerId: setDef.id,
397
- containerPath: marker.containerPath || null,
398
- urlPatterns: Array.isArray(marker.urlPatterns) ? marker.urlPatterns : [],
399
- dom: marker.dom || null,
400
- });
401
- }
402
- }
403
-
404
- const dedup = new Map();
405
- for (const item of targets) {
406
- const key = item.markerType === 'path'
407
- ? `path::${item.containerPath || ''}::${item.containerId}`
408
- : `url_dom::${item.containerId}::${item.dom?.css || ''}`;
409
- if (!dedup.has(key)) dedup.set(key, item);
410
- }
411
-
412
- const allTargets = Array.from(dedup.values());
413
- const selectors = allTargets
414
- .filter((item) => item.markerType === 'url_dom' && item.dom?.css)
415
- .map((item) => ({
416
- setId: item.setId,
417
- siteKey: item.siteKey,
418
- css: item.dom.css,
419
- variant: item.dom?.variant || null,
420
- }));
421
-
422
- doc.profiles[profileId] = {
423
- setIds: Array.from(resolvedSetIds).sort(),
424
- targets: allTargets,
425
- selectors,
426
- updatedAt: new Date().toISOString(),
427
- };
428
- doc.updatedAt = new Date().toISOString();
429
- toJsonFile(storage.targetsFile, doc);
430
-
431
- return {
432
- ok: true,
433
- profileId,
434
- setCount: doc.profiles[profileId].setIds.length,
435
- targetCount: doc.profiles[profileId].targets.length,
436
- selectorCount: doc.profiles[profileId].selectors.length,
437
- missingSetIds,
438
- path: storage.targetsFile,
439
- };
440
- }
441
-
442
- export function getRegisteredTargets(profileId, options = {}) {
443
- const storage = resolveStoragePaths(options);
444
- const doc = readTargetsDoc(options);
445
- if (profileId) {
446
- const profile = doc.profiles[profileId] || null;
447
- return {
448
- ok: true,
449
- profileId,
450
- profile,
451
- path: storage.targetsFile,
452
- };
453
- }
454
- return {
455
- ok: true,
456
- profiles: doc.profiles,
457
- path: storage.targetsFile,
458
- };
459
- }
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { CONFIG_DIR, loadConfig } from '../utils/config.mjs';
4
+
5
+ const CONTAINER_ROOT_ENV = process.env.CAMO_CONTAINER_ROOT;
6
+
7
+ export const USER_CONTAINER_ROOT = CONTAINER_ROOT_ENV || path.join(CONFIG_DIR, 'container-lib');
8
+ export const SUBSCRIPTION_ROOT = path.join(CONFIG_DIR, 'container-subscriptions');
9
+ export const SUBSCRIPTION_SETS_DIR = path.join(SUBSCRIPTION_ROOT, 'sets');
10
+ export const SUBSCRIPTION_INDEX_FILE = path.join(SUBSCRIPTION_ROOT, 'index.json');
11
+ export const SUBSCRIPTION_TARGETS_FILE = path.join(SUBSCRIPTION_ROOT, 'targets.json');
12
+
13
+ function resolveStoragePaths(options = {}) {
14
+ const userContainerRoot = options.userContainerRoot || USER_CONTAINER_ROOT;
15
+ const subscriptionRoot = options.subscriptionRoot || SUBSCRIPTION_ROOT;
16
+ const setsDir = options.setsDir || path.join(subscriptionRoot, 'sets');
17
+ const indexFile = options.indexFile || path.join(subscriptionRoot, 'index.json');
18
+ const targetsFile = options.targetsFile || path.join(subscriptionRoot, 'targets.json');
19
+ return { userContainerRoot, subscriptionRoot, setsDir, indexFile, targetsFile };
20
+ }
21
+
22
+ function ensureDir(dirPath) {
23
+ if (!fs.existsSync(dirPath)) {
24
+ fs.mkdirSync(dirPath, { recursive: true });
25
+ }
26
+ }
27
+
28
+ function toJsonFile(filePath, data) {
29
+ ensureDir(path.dirname(filePath));
30
+ fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`, 'utf8');
31
+ }
32
+
33
+ function readJsonFile(filePath, fallback = null) {
34
+ try {
35
+ if (!fs.existsSync(filePath)) return fallback;
36
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
37
+ } catch {
38
+ return fallback;
39
+ }
40
+ }
41
+
42
+ function splitLegacySelector(rawSelector) {
43
+ if (typeof rawSelector !== 'string') return [];
44
+ return rawSelector
45
+ .split(',')
46
+ .map((item) => item.trim())
47
+ .filter(Boolean);
48
+ }
49
+
50
+ function normalizeSelectors(rawSelectors) {
51
+ const out = [];
52
+ const pushCss = (css, extra = {}) => {
53
+ if (typeof css !== 'string' || !css.trim()) return;
54
+ out.push({
55
+ css: css.trim(),
56
+ ...(typeof extra.variant === 'string' ? { variant: extra.variant } : {}),
57
+ ...(Number.isFinite(Number(extra.score)) ? { score: Number(extra.score) } : {}),
58
+ });
59
+ };
60
+
61
+ if (Array.isArray(rawSelectors)) {
62
+ for (const item of rawSelectors) {
63
+ if (typeof item === 'string') {
64
+ for (const css of splitLegacySelector(item)) {
65
+ pushCss(css);
66
+ }
67
+ continue;
68
+ }
69
+ if (item && typeof item === 'object') {
70
+ if (typeof item.css === 'string') {
71
+ pushCss(item.css, item);
72
+ } else if (typeof item.selector === 'string') {
73
+ for (const css of splitLegacySelector(item.selector)) {
74
+ pushCss(css, item);
75
+ }
76
+ } else if (typeof item.id === 'string' && item.id) {
77
+ pushCss(`#${item.id}`, item);
78
+ } else if (Array.isArray(item.classes) && item.classes.length > 0) {
79
+ pushCss(`.${item.classes.filter(Boolean).join('.')}`, item);
80
+ }
81
+ }
82
+ }
83
+ } else if (typeof rawSelectors === 'string') {
84
+ for (const css of splitLegacySelector(rawSelectors)) {
85
+ pushCss(css);
86
+ }
87
+ } else if (rawSelectors && typeof rawSelectors === 'object') {
88
+ if (typeof rawSelectors.css === 'string') {
89
+ pushCss(rawSelectors.css, rawSelectors);
90
+ } else if (typeof rawSelectors.selector === 'string') {
91
+ for (const css of splitLegacySelector(rawSelectors.selector)) {
92
+ pushCss(css, rawSelectors);
93
+ }
94
+ }
95
+ }
96
+
97
+ const dedup = new Map();
98
+ for (const item of out) {
99
+ const key = `${item.css}::${item.variant || ''}::${item.score || ''}`;
100
+ if (!dedup.has(key)) dedup.set(key, item);
101
+ }
102
+ return Array.from(dedup.values());
103
+ }
104
+
105
+ function normalizeChildren(rawChildren) {
106
+ if (!Array.isArray(rawChildren)) return [];
107
+ return rawChildren.filter((item) => typeof item === 'string' && item.trim()).map((item) => item.trim());
108
+ }
109
+
110
+ function normalizePagePatterns(raw) {
111
+ const out = [];
112
+ const fromArray = Array.isArray(raw.page_patterns) ? raw.page_patterns : [];
113
+ const fromCamel = Array.isArray(raw.pagePatterns) ? raw.pagePatterns : [];
114
+ const fromUrl = typeof raw.page_url === 'string' && raw.page_url.trim() ? [raw.page_url.trim()] : [];
115
+ for (const item of [...fromArray, ...fromCamel, ...fromUrl]) {
116
+ if (typeof item === 'string' && item.trim()) out.push(item.trim());
117
+ }
118
+ return Array.from(new Set(out));
119
+ }
120
+
121
+ function parseCssFingerprint(css) {
122
+ const normalizedCss = typeof css === 'string' ? css.trim() : '';
123
+ if (!normalizedCss) {
124
+ return { css: null, tag: null, id: null, classes: [] };
125
+ }
126
+ const tagMatch = normalizedCss.match(/^[a-zA-Z][\w-]*/);
127
+ const idMatch = normalizedCss.match(/#([\w-]+)/);
128
+ const classMatches = normalizedCss.match(/\.([\w-]+)/g) || [];
129
+ return {
130
+ css: normalizedCss,
131
+ tag: tagMatch ? tagMatch[0].toLowerCase() : null,
132
+ id: idMatch ? idMatch[1] : null,
133
+ classes: classMatches.map((item) => item.slice(1)),
134
+ };
135
+ }
136
+
137
+ function sanitizeFileName(name) {
138
+ return String(name || '')
139
+ .replace(/[^\w.-]/g, '_')
140
+ .slice(0, 180);
141
+ }
142
+
143
+ function inferSiteKey(containerId, fallbackSiteKey) {
144
+ if (typeof containerId === 'string') {
145
+ const under = containerId.match(/^([a-z0-9-]+)_/i);
146
+ if (under?.[1]) return under[1].toLowerCase();
147
+ }
148
+ return String(fallbackSiteKey || 'default').toLowerCase();
149
+ }
150
+
151
+ function buildSetDefinition({ id, siteKey, sourceRelPath, raw }) {
152
+ const selectors = normalizeSelectors(raw.selectors || raw.selector);
153
+ const pagePatterns = normalizePagePatterns(raw);
154
+ const sourceKind = sourceRelPath.endsWith('containers.json') ? 'legacy-map' : 'tree-v2';
155
+ const containerPath = sourceKind === 'tree-v2'
156
+ ? sourceRelPath.replace(/\/container\.json$/, '')
157
+ : `${siteKey}/${id.replace(/\./g, '/')}`;
158
+
159
+ const markers = [
160
+ {
161
+ markerType: 'path',
162
+ siteKey: inferSiteKey(id, siteKey),
163
+ containerId: id,
164
+ containerPath,
165
+ },
166
+ ...selectors.map((selectorDef, idx) => ({
167
+ markerType: 'url_dom',
168
+ siteKey: inferSiteKey(id, siteKey),
169
+ containerId: id,
170
+ markerId: `${id}#${idx + 1}`,
171
+ urlPatterns: pagePatterns,
172
+ dom: {
173
+ ...parseCssFingerprint(selectorDef.css),
174
+ variant: selectorDef.variant || null,
175
+ score: Number.isFinite(Number(selectorDef.score)) ? Number(selectorDef.score) : null,
176
+ },
177
+ })),
178
+ ];
179
+
180
+ return {
181
+ id,
182
+ siteKey: inferSiteKey(id, siteKey),
183
+ name: raw.name || id,
184
+ type: raw.type || 'container',
185
+ pagePatterns,
186
+ selectors,
187
+ selectorCount: selectors.length,
188
+ children: normalizeChildren(raw.children),
189
+ capabilities: Array.isArray(raw.capabilities) ? raw.capabilities : [],
190
+ markers,
191
+ markerCount: markers.length,
192
+ metadata: raw.metadata && typeof raw.metadata === 'object' ? raw.metadata : {},
193
+ source: {
194
+ kind: sourceKind,
195
+ path: sourceRelPath,
196
+ },
197
+ };
198
+ }
199
+
200
+ function collectJsonFiles(dirPath) {
201
+ const out = [];
202
+ const stack = [dirPath];
203
+ while (stack.length > 0) {
204
+ const dir = stack.pop();
205
+ let entries = [];
206
+ try {
207
+ entries = fs.readdirSync(dir, { withFileTypes: true });
208
+ } catch {
209
+ continue;
210
+ }
211
+ for (const entry of entries) {
212
+ const fullPath = path.join(dir, entry.name);
213
+ if (entry.isDirectory()) {
214
+ stack.push(fullPath);
215
+ } else if (entry.isFile() && entry.name.endsWith('.json')) {
216
+ out.push(fullPath);
217
+ }
218
+ }
219
+ }
220
+ return out;
221
+ }
222
+
223
+ function detectContainerLibraryRoot(explicitRoot) {
224
+ if (explicitRoot) return explicitRoot;
225
+ const cfg = loadConfig();
226
+ const root = cfg.repoRoot || process.env.CAMO_CONTAINER_ROOT || process.cwd();
227
+ if (!root) return null;
228
+ const candidate = path.join(root, 'container-library');
229
+ return fs.existsSync(candidate) ? candidate : null;
230
+ }
231
+
232
+ function bootstrapUserContainerRoot(containerLibraryRoot, options = {}) {
233
+ const force = options.force === true;
234
+ const userContainerRoot = options.userContainerRoot || USER_CONTAINER_ROOT;
235
+ ensureDir(userContainerRoot);
236
+ const topLevel = fs.readdirSync(containerLibraryRoot, { withFileTypes: true });
237
+ let copiedEntries = 0;
238
+ for (const entry of topLevel) {
239
+ if (!entry.isDirectory()) continue;
240
+ const src = path.join(containerLibraryRoot, entry.name);
241
+ const dst = path.join(userContainerRoot, entry.name);
242
+ if (fs.existsSync(dst) && !force) continue;
243
+ fs.cpSync(src, dst, { recursive: true, force });
244
+ copiedEntries += 1;
245
+ }
246
+ return { root: userContainerRoot, copiedEntries };
247
+ }
248
+
249
+ export function initContainerSubscriptionDirectory(options = {}) {
250
+ const storage = resolveStoragePaths(options);
251
+ const containerLibraryRoot = detectContainerLibraryRoot(options.containerLibraryRoot);
252
+ if (!containerLibraryRoot || !fs.existsSync(containerLibraryRoot)) {
253
+ throw new Error(
254
+ 'container-library not found. Set CAMO_CONTAINER_ROOT or run `camo config repo-root <path>` first.',
255
+ );
256
+ }
257
+
258
+ ensureDir(storage.subscriptionRoot);
259
+ ensureDir(storage.setsDir);
260
+ const boot = bootstrapUserContainerRoot(containerLibraryRoot, {
261
+ force: options.force === true,
262
+ userContainerRoot: storage.userContainerRoot,
263
+ });
264
+
265
+ const jsonFiles = collectJsonFiles(containerLibraryRoot);
266
+ const setMap = new Map();
267
+
268
+ for (const filePath of jsonFiles) {
269
+ const rel = path.relative(containerLibraryRoot, filePath).replace(/\\/g, '/');
270
+ const raw = readJsonFile(filePath, null);
271
+ if (!raw || typeof raw !== 'object') continue;
272
+ const siteKey = rel.split('/')[0] || 'default';
273
+
274
+ if (filePath.endsWith(`${path.sep}container.json`) || rel.endsWith('/container.json')) {
275
+ const id = typeof raw.id === 'string' && raw.id.trim() ? raw.id.trim() : null;
276
+ if (!id) continue;
277
+ setMap.set(id, buildSetDefinition({ id, siteKey, sourceRelPath: rel, raw }));
278
+ continue;
279
+ }
280
+
281
+ if (filePath.endsWith(`${path.sep}containers.json`) || rel.endsWith('/containers.json')) {
282
+ const containers = raw.containers;
283
+ if (!containers || typeof containers !== 'object') continue;
284
+ for (const [id, def] of Object.entries(containers)) {
285
+ if (!id || !def || typeof def !== 'object') continue;
286
+ if (setMap.has(id)) continue;
287
+ setMap.set(id, buildSetDefinition({ id, siteKey, sourceRelPath: rel, raw: def }));
288
+ }
289
+ }
290
+ }
291
+
292
+ const sets = Array.from(setMap.values()).sort((a, b) => a.id.localeCompare(b.id));
293
+ for (const set of sets) {
294
+ const fileName = `${sanitizeFileName(set.id)}.json`;
295
+ toJsonFile(path.join(storage.setsDir, fileName), set);
296
+ }
297
+
298
+ const bySite = {};
299
+ for (const set of sets) {
300
+ if (!bySite[set.siteKey]) bySite[set.siteKey] = [];
301
+ bySite[set.siteKey].push(set.id);
302
+ }
303
+
304
+ const index = {
305
+ version: 1,
306
+ generatedAt: new Date().toISOString(),
307
+ sourceRoot: containerLibraryRoot,
308
+ userContainerRoot: storage.userContainerRoot,
309
+ setCount: sets.length,
310
+ sites: Object.fromEntries(
311
+ Object.entries(bySite).map(([site, ids]) => [site, { count: ids.length, sets: ids.sort() }]),
312
+ ),
313
+ sets: sets.map((set) => ({
314
+ id: set.id,
315
+ siteKey: set.siteKey,
316
+ name: set.name,
317
+ selectorCount: set.selectorCount,
318
+ markerCount: set.markerCount,
319
+ source: set.source,
320
+ file: `sets/${sanitizeFileName(set.id)}.json`,
321
+ })),
322
+ };
323
+ toJsonFile(storage.indexFile, index);
324
+
325
+ return {
326
+ ok: true,
327
+ containerLibraryRoot,
328
+ userContainerRoot: boot.root,
329
+ copiedEntries: boot.copiedEntries,
330
+ subscriptionRoot: storage.subscriptionRoot,
331
+ setCount: sets.length,
332
+ siteCount: Object.keys(bySite).length,
333
+ index: storage.indexFile,
334
+ };
335
+ }
336
+
337
+ export function listSubscriptionSets(options = {}) {
338
+ const storage = resolveStoragePaths(options);
339
+ const index = readJsonFile(storage.indexFile, null);
340
+ if (!index?.sets) {
341
+ throw new Error('Subscription index not found. Run: camo container init');
342
+ }
343
+ const site = options.site ? String(options.site).toLowerCase() : null;
344
+ const sets = site
345
+ ? index.sets.filter((item) => String(item.siteKey).toLowerCase() === site)
346
+ : index.sets;
347
+ return { ok: true, site, count: sets.length, sets };
348
+ }
349
+
350
+ function loadSubscriptionSetById(setId, options = {}) {
351
+ const storage = resolveStoragePaths(options);
352
+ const filePath = path.join(storage.setsDir, `${sanitizeFileName(setId)}.json`);
353
+ return readJsonFile(filePath, null);
354
+ }
355
+
356
+ function readTargetsDoc(options = {}) {
357
+ const storage = resolveStoragePaths(options);
358
+ return readJsonFile(storage.targetsFile, {
359
+ version: 1,
360
+ updatedAt: null,
361
+ profiles: {},
362
+ });
363
+ }
364
+
365
+ export function registerSubscriptionTargets(profileId, setIds, options = {}) {
366
+ if (!profileId || typeof profileId !== 'string') {
367
+ throw new Error('profileId is required');
368
+ }
369
+ if (!Array.isArray(setIds) || setIds.length === 0) {
370
+ throw new Error('at least one setId is required');
371
+ }
372
+
373
+ const storage = resolveStoragePaths(options);
374
+ const doc = readTargetsDoc(options);
375
+ const existing = doc.profiles[profileId] || { setIds: [], targets: [] };
376
+ const resolvedSetIds = options.append ? new Set(existing.setIds || []) : new Set();
377
+ for (const id of setIds) {
378
+ resolvedSetIds.add(id);
379
+ }
380
+
381
+ const missingSetIds = [];
382
+ const targets = [];
383
+ for (const id of Array.from(resolvedSetIds)) {
384
+ const setDef = loadSubscriptionSetById(id, options);
385
+ if (!setDef) {
386
+ missingSetIds.push(id);
387
+ continue;
388
+ }
389
+ const markerDefs = Array.isArray(setDef.markers) ? setDef.markers : [];
390
+ for (const marker of markerDefs) {
391
+ if (!marker || typeof marker !== 'object') continue;
392
+ targets.push({
393
+ setId: setDef.id,
394
+ siteKey: setDef.siteKey,
395
+ markerType: marker.markerType || 'path',
396
+ containerId: setDef.id,
397
+ containerPath: marker.containerPath || null,
398
+ urlPatterns: Array.isArray(marker.urlPatterns) ? marker.urlPatterns : [],
399
+ dom: marker.dom || null,
400
+ });
401
+ }
402
+ }
403
+
404
+ const dedup = new Map();
405
+ for (const item of targets) {
406
+ const key = item.markerType === 'path'
407
+ ? `path::${item.containerPath || ''}::${item.containerId}`
408
+ : `url_dom::${item.containerId}::${item.dom?.css || ''}`;
409
+ if (!dedup.has(key)) dedup.set(key, item);
410
+ }
411
+
412
+ const allTargets = Array.from(dedup.values());
413
+ const selectors = allTargets
414
+ .filter((item) => item.markerType === 'url_dom' && item.dom?.css)
415
+ .map((item) => ({
416
+ setId: item.setId,
417
+ siteKey: item.siteKey,
418
+ css: item.dom.css,
419
+ variant: item.dom?.variant || null,
420
+ }));
421
+
422
+ doc.profiles[profileId] = {
423
+ setIds: Array.from(resolvedSetIds).sort(),
424
+ targets: allTargets,
425
+ selectors,
426
+ updatedAt: new Date().toISOString(),
427
+ };
428
+ doc.updatedAt = new Date().toISOString();
429
+ toJsonFile(storage.targetsFile, doc);
430
+
431
+ return {
432
+ ok: true,
433
+ profileId,
434
+ setCount: doc.profiles[profileId].setIds.length,
435
+ targetCount: doc.profiles[profileId].targets.length,
436
+ selectorCount: doc.profiles[profileId].selectors.length,
437
+ missingSetIds,
438
+ path: storage.targetsFile,
439
+ };
440
+ }
441
+
442
+ export function getRegisteredTargets(profileId, options = {}) {
443
+ const storage = resolveStoragePaths(options);
444
+ const doc = readTargetsDoc(options);
445
+ if (profileId) {
446
+ const profile = doc.profiles[profileId] || null;
447
+ return {
448
+ ok: true,
449
+ profileId,
450
+ profile,
451
+ path: storage.targetsFile,
452
+ };
453
+ }
454
+ return {
455
+ ok: true,
456
+ profiles: doc.profiles,
457
+ path: storage.targetsFile,
458
+ };
459
+ }