@designerbyron/lifecode-rag 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +191 -2
- package/bin/lifecode-rag.mjs +449 -0
- package/client.js +2801 -0
- package/cordis.patch.yml +3 -0
- package/docs/ARCHITECTURE-v0.2.md +269 -0
- package/docs/AUTO-DISCOVERY.md +111 -0
- package/docs/AUTOMATION-POLICIES.md +232 -0
- package/docs/CONTRATO-v0.1.md +195 -0
- package/docs/EMBEDDING-PROVIDERS.md +105 -0
- package/docs/OBSERVABILITY.md +241 -0
- package/docs/PA-UI-0-API-CONTRACT.md +802 -0
- package/docs/PERFORMANCE-BUDGETS.md +118 -0
- package/docs/PROJECT-AWARE-ARCHITECTURE.md +580 -0
- package/docs/PROJECT-AWARE-DATA-CONTRACT.md +411 -0
- package/docs/PROJECT-AWARE-ERROR-CONTRACT.md +112 -0
- package/docs/PROJECT-AWARE-SESSION-CONTRACT.md +120 -0
- package/docs/WORKSPACE-API.md +239 -0
- package/icon.svg +7 -0
- package/index.js +198 -0
- package/lib/audit-export.mjs +70 -0
- package/lib/audit.mjs +120 -0
- package/lib/auto-context.mjs +322 -0
- package/lib/automation.mjs +214 -0
- package/lib/chunk.mjs +100 -0
- package/lib/config-resolve.mjs +119 -0
- package/lib/context.mjs +150 -0
- package/lib/conversation-events.mjs +145 -0
- package/lib/db.mjs +309 -0
- package/lib/diagnose.mjs +197 -0
- package/lib/discovery.mjs +402 -0
- package/lib/doctor.mjs +82 -0
- package/lib/embed.mjs +96 -0
- package/lib/embedders.mjs +103 -0
- package/lib/errors.mjs +227 -0
- package/lib/exec-context.mjs +238 -0
- package/lib/extract.mjs +260 -0
- package/lib/harness-workspaces.mjs +85 -0
- package/lib/hash.mjs +34 -0
- package/lib/ingest.mjs +239 -0
- package/lib/inputs.mjs +67 -0
- package/lib/library-state.mjs +252 -0
- package/lib/maintenance.mjs +113 -0
- package/lib/metrics.mjs +164 -0
- package/lib/migrate.mjs +373 -0
- package/lib/operations.mjs +132 -0
- package/lib/pa-api.mjs +517 -0
- package/lib/paths.mjs +345 -0
- package/lib/policy.mjs +125 -0
- package/lib/profile.mjs +128 -0
- package/lib/readmodel.mjs +159 -0
- package/lib/recovery.mjs +557 -0
- package/lib/registry-schema.mjs +206 -0
- package/lib/registry.mjs +406 -0
- package/lib/resolve.mjs +311 -0
- package/lib/retention.mjs +143 -0
- package/lib/revocation.mjs +124 -0
- package/lib/roots-structure.mjs +224 -0
- package/lib/schema.mjs +173 -0
- package/lib/scope-ops.mjs +379 -0
- package/lib/scope-stores.mjs +128 -0
- package/lib/search.mjs +345 -0
- package/lib/selection.mjs +150 -0
- package/lib/server.mjs +335 -0
- package/lib/session-identity.mjs +51 -0
- package/lib/sessions.mjs +343 -0
- package/lib/sync-scheduler.mjs +354 -0
- package/lib/sync.mjs +755 -0
- package/lib/tags.mjs +45 -0
- package/lib/tools.mjs +364 -0
- package/lib/trusted-roots.mjs +149 -0
- package/lib/worker-context.mjs +22 -0
- package/package.json +67 -5
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modelo de lectura del Workspace: un snapshot agregado para la pestaña nativa.
|
|
3
|
+
*
|
|
4
|
+
* Regla de privacidad: aquí sólo hay recuentos, latencias agregadas y metadatos de fuentes.
|
|
5
|
+
* Nunca se devuelven prompts, respuestas ni texto de chunks. El texto se pide explícitamente
|
|
6
|
+
* por `/sources/:id/chunks/:ordinal` y se limita en tamaño.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { statSync } from 'node:fs';
|
|
10
|
+
|
|
11
|
+
import { verifyAuditChain } from './audit.mjs';
|
|
12
|
+
import { assertAuditHead, closeDatabase, ftsIntegrityCheck, openDatabase, quickCheck } from './db.mjs';
|
|
13
|
+
import { summary as metricsSummary } from './metrics.mjs';
|
|
14
|
+
import { loadConfig, storePaths } from './paths.mjs';
|
|
15
|
+
import { RagError } from './errors.mjs';
|
|
16
|
+
|
|
17
|
+
const DAY = 86_400_000;
|
|
18
|
+
|
|
19
|
+
function safeBytes(path) {
|
|
20
|
+
try { return statSync(path).size; } catch { return 0; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Abre en sólo lectura lógica; nunca crea el almacén desde el panel. */
|
|
24
|
+
export function withReadOnly(root, work) {
|
|
25
|
+
let opened;
|
|
26
|
+
try {
|
|
27
|
+
opened = openDatabase({ root, create: false });
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error instanceof RagError && error.code === 'store-not-initialized') return null;
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
return work(opened);
|
|
34
|
+
} finally {
|
|
35
|
+
closeDatabase(opened.db);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildSnapshot({ root = undefined, now = Date.now() } = {}) {
|
|
40
|
+
const { config } = loadConfig(root);
|
|
41
|
+
const paths = storePaths(root);
|
|
42
|
+
const snapshot = {
|
|
43
|
+
generatedAt: now,
|
|
44
|
+
store: {
|
|
45
|
+
path: paths.database,
|
|
46
|
+
initialized: false,
|
|
47
|
+
schemaVersion: null,
|
|
48
|
+
databaseBytes: safeBytes(paths.database),
|
|
49
|
+
auditBytes: safeBytes(paths.audit),
|
|
50
|
+
},
|
|
51
|
+
counts: { sources: 0, chunks: 0, vectors: 0, tags: 0, collections: 0 },
|
|
52
|
+
activity: { last: null, recent: [] },
|
|
53
|
+
metrics: [],
|
|
54
|
+
config: {
|
|
55
|
+
strategy: config.search.strategy,
|
|
56
|
+
limit: config.search.limit,
|
|
57
|
+
embedding: { provider: config.embedding.provider, dim: config.embedding.dim, kind: config.embedding.provider === 'local-hash-v1' ? 'lexical' : 'semantic' },
|
|
58
|
+
automation: { enabled: config.automation.enabled, intervalMs: config.automation.intervalMs, watchRoots: config.automation.watchRoots.length },
|
|
59
|
+
metrics: { enabled: config.metrics.enabled, retentionDays: config.metrics.retentionDays },
|
|
60
|
+
roots: config.roots.extra.length,
|
|
61
|
+
},
|
|
62
|
+
health: { ok: true, checks: [] },
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const store = withReadOnly(root, ({ db }) => {
|
|
66
|
+
const version = db.prepare('PRAGMA user_version').get();
|
|
67
|
+
const counts = {
|
|
68
|
+
sources: db.prepare('SELECT COUNT(*) AS n FROM sources').get().n,
|
|
69
|
+
chunks: db.prepare('SELECT COUNT(*) AS n FROM chunks').get().n,
|
|
70
|
+
vectors: db.prepare('SELECT COUNT(*) AS n FROM vectors').get().n,
|
|
71
|
+
tags: db.prepare('SELECT COUNT(*) AS n FROM source_tags').get().n,
|
|
72
|
+
collections: db.prepare('SELECT COUNT(DISTINCT collection) AS n FROM sources').get().n,
|
|
73
|
+
};
|
|
74
|
+
const recent = db.prepare('SELECT seq, at, tool, action, target, outcome FROM audit ORDER BY seq DESC LIMIT 20').all();
|
|
75
|
+
const last = recent[0]
|
|
76
|
+
? { at: recent[0].at, tool: recent[0].tool, action: recent[0].action, outcome: recent[0].outcome }
|
|
77
|
+
: null;
|
|
78
|
+
let metrics = [];
|
|
79
|
+
try { metrics = metricsSummary(db, { days: 7, now }).metrics; } catch { /* métricas opcionales */ }
|
|
80
|
+
return { version: version.user_version, counts, recent, last, metrics };
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (!store) {
|
|
84
|
+
snapshot.health = { ok: true, checks: [{ name: 'almacen', status: 'warn', detail: 'sin inicializar' }] };
|
|
85
|
+
return snapshot;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
snapshot.store.initialized = true;
|
|
89
|
+
snapshot.store.schemaVersion = store.version;
|
|
90
|
+
snapshot.counts = store.counts;
|
|
91
|
+
snapshot.activity = { last: store.last, recent: store.recent };
|
|
92
|
+
snapshot.metrics = store.metrics;
|
|
93
|
+
|
|
94
|
+
const checks = [];
|
|
95
|
+
const opened = openDatabase({ root, create: false });
|
|
96
|
+
try {
|
|
97
|
+
try {
|
|
98
|
+
quickCheck(opened.db);
|
|
99
|
+
checks.push({ name: 'sqlite', status: 'ok', detail: 'PRAGMA quick_check' });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
checks.push({ name: 'sqlite', status: 'fail', detail: error.message });
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
ftsIntegrityCheck(opened.db);
|
|
105
|
+
checks.push({ name: 'fts', status: 'ok', detail: 'integridad FTS5' });
|
|
106
|
+
} catch (error) {
|
|
107
|
+
checks.push({ name: 'fts', status: 'fail', detail: error.message });
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
assertAuditHead(opened.db);
|
|
111
|
+
const chain = verifyAuditChain(opened.db);
|
|
112
|
+
checks.push({ name: 'auditoria', status: chain.ok ? 'ok' : 'fail', detail: chain.ok ? `${chain.count} entradas` : `rota en ${chain.brokenAt ?? '?'}` });
|
|
113
|
+
} catch (error) {
|
|
114
|
+
checks.push({ name: 'auditoria', status: 'fail', detail: error.message });
|
|
115
|
+
}
|
|
116
|
+
} finally {
|
|
117
|
+
closeDatabase(opened.db);
|
|
118
|
+
}
|
|
119
|
+
snapshot.health = { ok: checks.every((check) => check.status !== 'fail'), checks };
|
|
120
|
+
return snapshot;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Lista de fuentes con recuento de chunks, sin texto. */
|
|
124
|
+
export function listSourceRows(db, { collection = null, tag = null, limit = 50, offset = 0 } = {}) {
|
|
125
|
+
const where = [];
|
|
126
|
+
const params = [];
|
|
127
|
+
if (collection) { where.push('s.collection = ?'); params.push(collection); }
|
|
128
|
+
if (tag) { where.push('EXISTS (SELECT 1 FROM source_tags t WHERE t.source_id = s.id AND t.tag = ?)'); params.push(tag); }
|
|
129
|
+
const clause = where.length ? `WHERE ${where.join(' AND ')}` : '';
|
|
130
|
+
const total = db.prepare(`SELECT COUNT(*) AS n FROM sources s ${clause}`).get(...params).n;
|
|
131
|
+
const rows = db.prepare(`SELECT s.id, s.uri, s.title, s.collection, s.bytes, s.indexed_at,
|
|
132
|
+
(SELECT COUNT(*) FROM chunks c WHERE c.source_id = s.id) AS chunks
|
|
133
|
+
FROM sources s ${clause} ORDER BY s.indexed_at DESC, s.id ASC LIMIT ? OFFSET ?`).all(...params, limit, offset)
|
|
134
|
+
.map((row) => ({
|
|
135
|
+
id: row.id,
|
|
136
|
+
uri: row.uri,
|
|
137
|
+
title: row.title,
|
|
138
|
+
collection: row.collection,
|
|
139
|
+
bytes: row.bytes,
|
|
140
|
+
chunks: row.chunks,
|
|
141
|
+
updatedAt: row.indexed_at,
|
|
142
|
+
}));
|
|
143
|
+
return { total, rows, limit, offset };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function activityFeed(db, { limit = 40 } = {}) {
|
|
147
|
+
const rows = db.prepare('SELECT seq, at, actor, tool, action, target, outcome FROM audit ORDER BY seq DESC LIMIT ?').all(limit);
|
|
148
|
+
return rows.map((row) => ({
|
|
149
|
+
seq: row.seq,
|
|
150
|
+
at: row.at,
|
|
151
|
+
actor: row.actor,
|
|
152
|
+
tool: row.tool,
|
|
153
|
+
action: row.action,
|
|
154
|
+
target: row.target,
|
|
155
|
+
outcome: row.outcome,
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { DAY };
|