@dudong2/pi-context 0.0.0-stage → 0.3.2-rev.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.
- package/LICENSE +21 -0
- package/README.md +274 -2
- package/dist/commands/context-command.d.ts +2 -0
- package/dist/commands/context-command.js +65 -0
- package/dist/commands/context-command.js.map +1 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +184 -0
- package/dist/config.js.map +1 -0
- package/dist/context-format.d.ts +20 -0
- package/dist/context-format.js +200 -0
- package/dist/context-format.js.map +1 -0
- package/dist/context-scope.d.ts +7 -0
- package/dist/context-scope.js +44 -0
- package/dist/context-scope.js.map +1 -0
- package/dist/eval/checks.d.ts +9 -0
- package/dist/eval/checks.js +241 -0
- package/dist/eval/checks.js.map +1 -0
- package/dist/eval/codemode-benchmark.d.ts +25 -0
- package/dist/eval/codemode-benchmark.js +308 -0
- package/dist/eval/codemode-benchmark.js.map +1 -0
- package/dist/eval/fixtures.d.ts +31 -0
- package/dist/eval/fixtures.js +79 -0
- package/dist/eval/fixtures.js.map +1 -0
- package/dist/eval/index.d.ts +4 -0
- package/dist/eval/index.js +56 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/report.d.ts +3 -0
- package/dist/eval/report.js +45 -0
- package/dist/eval/report.js.map +1 -0
- package/dist/eval/scenarios.d.ts +3 -0
- package/dist/eval/scenarios.js +52 -0
- package/dist/eval/scenarios.js.map +1 -0
- package/dist/eval/types.d.ts +48 -0
- package/dist/eval/types.js +2 -0
- package/dist/eval/types.js.map +1 -0
- package/dist/export-files.d.ts +9 -0
- package/dist/export-files.js +45 -0
- package/dist/export-files.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +59 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/policy.d.ts +3 -0
- package/dist/policy.js +39 -0
- package/dist/policy.js.map +1 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +39 -0
- package/dist/schema.js.map +1 -0
- package/dist/schema.sql +49 -0
- package/dist/store/maintenance.d.ts +19 -0
- package/dist/store/maintenance.js +155 -0
- package/dist/store/maintenance.js.map +1 -0
- package/dist/store/registry.d.ts +7 -0
- package/dist/store/registry.js +44 -0
- package/dist/store/registry.js.map +1 -0
- package/dist/store/retrieval.d.ts +8 -0
- package/dist/store/retrieval.js +127 -0
- package/dist/store/retrieval.js.map +1 -0
- package/dist/store/row-schemas.d.ts +73 -0
- package/dist/store/row-schemas.js +74 -0
- package/dist/store/row-schemas.js.map +1 -0
- package/dist/store.d.ts +53 -0
- package/dist/store.js +408 -0
- package/dist/store.js.map +1 -0
- package/dist/text.d.ts +11 -0
- package/dist/text.js +226 -0
- package/dist/text.js.map +1 -0
- package/dist/tool-output.d.ts +6 -0
- package/dist/tool-output.js +48 -0
- package/dist/tool-output.js.map +1 -0
- package/dist/tools/export.d.ts +2 -0
- package/dist/tools/export.js +99 -0
- package/dist/tools/export.js.map +1 -0
- package/dist/tools/get.d.ts +2 -0
- package/dist/tools/get.js +61 -0
- package/dist/tools/get.js.map +1 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.js +15 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/list.d.ts +2 -0
- package/dist/tools/list.js +72 -0
- package/dist/tools/list.js.map +1 -0
- package/dist/tools/output-schemas.d.ts +93 -0
- package/dist/tools/output-schemas.js +89 -0
- package/dist/tools/output-schemas.js.map +1 -0
- package/dist/tools/purge.d.ts +2 -0
- package/dist/tools/purge.js +73 -0
- package/dist/tools/purge.js.map +1 -0
- package/dist/tools/search.d.ts +2 -0
- package/dist/tools/search.js +63 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/stats.d.ts +2 -0
- package/dist/tools/stats.js +37 -0
- package/dist/tools/stats.js.map +1 -0
- package/dist/types.d.ts +167 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/menu.d.ts +8 -0
- package/dist/ui/menu.js +88 -0
- package/dist/ui/menu.js.map +1 -0
- package/dist/ui/settings.d.ts +7 -0
- package/dist/ui/settings.js +353 -0
- package/dist/ui/settings.js.map +1 -0
- package/package.json +64 -4
package/dist/store.js
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { redact_text } from '@spences10/pi-redact';
|
|
2
|
+
import { with_sqlite_transaction } from '@spences10/pi-sqlite-core';
|
|
3
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
5
|
+
import { dirname } from 'node:path';
|
|
6
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
7
|
+
import * as v from 'valibot';
|
|
8
|
+
import { content_hash_row_schema, duplicate_source_row_schema, list_row_schema, search_row_schema, } from './store/row-schemas.js';
|
|
9
|
+
import { get_context_capture_limits } from './config.js';
|
|
10
|
+
import { redact_input_summary } from './context-scope.js';
|
|
11
|
+
import { parse_context_retention_policy } from './policy.js';
|
|
12
|
+
import { apply_schema } from './schema.js';
|
|
13
|
+
import { context_store_cleanup, context_store_purge_to_max_stored_bytes, context_store_purge_with_details, context_store_stats, } from './store/maintenance.js';
|
|
14
|
+
import { default_context_db_path, get_context_store as get_context_store_with_ctor, maybe_store_context_output as maybe_store_context_output_with_ctor, } from './store/registry.js';
|
|
15
|
+
import { context_store_chunk_summary, context_store_get, } from './store/retrieval.js';
|
|
16
|
+
import { chunk_text, count_lines, escape_fts5_query, make_preview, relaxed_fts5_query, should_index_text, summarize_source, } from './text.js';
|
|
17
|
+
export { DEFAULT_CONTEXT_RETENTION_DAYS, parse_context_retention_policy, } from './policy.js';
|
|
18
|
+
export { default_context_db_path, is_context_sidecar_enabled, set_context_sidecar_enabled, } from './store/registry.js';
|
|
19
|
+
export { count_lines, DEFAULT_CONTEXT_MAX_BYTES, DEFAULT_CONTEXT_MAX_LINES, escape_fts5_query, make_preview, should_index_text, } from './text.js';
|
|
20
|
+
export function get_context_store(options = {}) {
|
|
21
|
+
return get_context_store_with_ctor(ContextStore, options);
|
|
22
|
+
}
|
|
23
|
+
export function maybe_store_context_output(input, options = {}) {
|
|
24
|
+
return maybe_store_context_output_with_ctor(ContextStore, input, options);
|
|
25
|
+
}
|
|
26
|
+
export class ContextStore {
|
|
27
|
+
db_path;
|
|
28
|
+
db;
|
|
29
|
+
project_path;
|
|
30
|
+
session_id;
|
|
31
|
+
max_bytes;
|
|
32
|
+
max_lines;
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
this.db_path = options.db_path ?? default_context_db_path();
|
|
35
|
+
this.project_path = options.project_path ?? process.cwd();
|
|
36
|
+
this.session_id = options.session_id ?? null;
|
|
37
|
+
const capture_limits = get_context_capture_limits();
|
|
38
|
+
this.max_bytes = options.max_bytes ?? capture_limits.max_bytes;
|
|
39
|
+
this.max_lines = options.max_lines ?? capture_limits.max_lines;
|
|
40
|
+
const dir = dirname(this.db_path);
|
|
41
|
+
if (!existsSync(dir))
|
|
42
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
43
|
+
this.db = new DatabaseSync(this.db_path, {
|
|
44
|
+
enableForeignKeyConstraints: true,
|
|
45
|
+
});
|
|
46
|
+
apply_schema(this.db);
|
|
47
|
+
}
|
|
48
|
+
configure(options = {}) {
|
|
49
|
+
if (options.project_path !== undefined)
|
|
50
|
+
this.project_path = options.project_path;
|
|
51
|
+
if (options.session_id !== undefined)
|
|
52
|
+
this.session_id = options.session_id;
|
|
53
|
+
const capture_limits = get_context_capture_limits();
|
|
54
|
+
this.max_bytes = options.max_bytes ?? capture_limits.max_bytes;
|
|
55
|
+
this.max_lines = options.max_lines ?? capture_limits.max_lines;
|
|
56
|
+
}
|
|
57
|
+
scoped_filter(alias, options = {}) {
|
|
58
|
+
const where = [];
|
|
59
|
+
const params = [];
|
|
60
|
+
if (options.session_id === null) {
|
|
61
|
+
where.push(`${alias}.session_id IS NULL`);
|
|
62
|
+
}
|
|
63
|
+
else if (options.session_id !== undefined) {
|
|
64
|
+
where.push(`${alias}.session_id = ?`);
|
|
65
|
+
params.push(options.session_id);
|
|
66
|
+
}
|
|
67
|
+
else if (!options.global && this.session_id) {
|
|
68
|
+
where.push(`${alias}.session_id = ?`);
|
|
69
|
+
params.push(this.session_id);
|
|
70
|
+
}
|
|
71
|
+
if (options.project_path === null) {
|
|
72
|
+
where.push(`${alias}.project_path IS NULL`);
|
|
73
|
+
}
|
|
74
|
+
else if (options.project_path !== undefined) {
|
|
75
|
+
where.push(`${alias}.project_path = ?`);
|
|
76
|
+
params.push(options.project_path);
|
|
77
|
+
}
|
|
78
|
+
else if (!options.global &&
|
|
79
|
+
where.length === 0 &&
|
|
80
|
+
this.project_path) {
|
|
81
|
+
where.push(`${alias}.project_path = ?`);
|
|
82
|
+
params.push(this.project_path);
|
|
83
|
+
}
|
|
84
|
+
return { where, params };
|
|
85
|
+
}
|
|
86
|
+
find_duplicate_source(content_hash, scope) {
|
|
87
|
+
const scoped = this.scoped_filter('context_sources', scope);
|
|
88
|
+
const filters = [
|
|
89
|
+
'context_sources.content_hash = ?',
|
|
90
|
+
...scoped.where,
|
|
91
|
+
];
|
|
92
|
+
const params = [
|
|
93
|
+
content_hash,
|
|
94
|
+
...scoped.params,
|
|
95
|
+
];
|
|
96
|
+
const row = v.parse(v.optional(duplicate_source_row_schema), this.db
|
|
97
|
+
.prepare(`
|
|
98
|
+
SELECT
|
|
99
|
+
context_sources.id,
|
|
100
|
+
COUNT(context_chunks.id) as chunk_count,
|
|
101
|
+
(
|
|
102
|
+
SELECT first_chunk.id FROM context_chunks first_chunk
|
|
103
|
+
WHERE first_chunk.source_id = context_sources.id
|
|
104
|
+
ORDER BY first_chunk.ordinal LIMIT 1
|
|
105
|
+
) as first_chunk_id
|
|
106
|
+
FROM context_sources
|
|
107
|
+
LEFT JOIN context_chunks ON context_chunks.source_id = context_sources.id
|
|
108
|
+
WHERE ${filters.join(' AND ')}
|
|
109
|
+
GROUP BY context_sources.id
|
|
110
|
+
ORDER BY context_sources.created_at DESC
|
|
111
|
+
LIMIT 1
|
|
112
|
+
`)
|
|
113
|
+
.get(...params));
|
|
114
|
+
return row ?? null;
|
|
115
|
+
}
|
|
116
|
+
store(input) {
|
|
117
|
+
const redaction = redact_text(input.text);
|
|
118
|
+
const text = redaction.redacted;
|
|
119
|
+
const tool_name = redact_text(input.tool_name).redacted;
|
|
120
|
+
const input_summary = input.input_summary == null
|
|
121
|
+
? null
|
|
122
|
+
: redact_input_summary(input.input_summary);
|
|
123
|
+
if (!input.force &&
|
|
124
|
+
!should_index_text(text, {
|
|
125
|
+
max_bytes: this.max_bytes,
|
|
126
|
+
max_lines: this.max_lines,
|
|
127
|
+
}))
|
|
128
|
+
return null;
|
|
129
|
+
const bytes = Buffer.byteLength(text, 'utf8');
|
|
130
|
+
const lines = count_lines(text);
|
|
131
|
+
const created_at = Date.now();
|
|
132
|
+
const content_hash = createHash('sha256')
|
|
133
|
+
.update(text)
|
|
134
|
+
.digest('hex');
|
|
135
|
+
const session_id = input.session_id ?? this.session_id;
|
|
136
|
+
const project_path = input.project_path ?? this.project_path;
|
|
137
|
+
const preview = make_preview(text);
|
|
138
|
+
const duplicate = this.find_duplicate_source(content_hash, {
|
|
139
|
+
project_path,
|
|
140
|
+
session_id,
|
|
141
|
+
});
|
|
142
|
+
if (duplicate) {
|
|
143
|
+
const provisional = {
|
|
144
|
+
source_id: duplicate.id,
|
|
145
|
+
bytes,
|
|
146
|
+
lines,
|
|
147
|
+
preview,
|
|
148
|
+
receipt: '',
|
|
149
|
+
chunk_count: duplicate.chunk_count,
|
|
150
|
+
first_chunk_id: duplicate.first_chunk_id,
|
|
151
|
+
returned_bytes: 0,
|
|
152
|
+
project_path,
|
|
153
|
+
session_id,
|
|
154
|
+
deduped: true,
|
|
155
|
+
capture_max_bytes: this.max_bytes,
|
|
156
|
+
capture_max_lines: this.max_lines,
|
|
157
|
+
};
|
|
158
|
+
const receipt = summarize_source(provisional, tool_name);
|
|
159
|
+
const returned_bytes = Buffer.byteLength(receipt, 'utf8');
|
|
160
|
+
with_sqlite_transaction(this.db, () => {
|
|
161
|
+
this.db
|
|
162
|
+
.prepare('UPDATE context_sources SET returned_byte_count = returned_byte_count + ? WHERE id = ?')
|
|
163
|
+
.run(returned_bytes, duplicate.id);
|
|
164
|
+
}, { operation: 'Update context receipt bytes' });
|
|
165
|
+
return this.enforce_storage_limit({ ...provisional, receipt, returned_bytes });
|
|
166
|
+
}
|
|
167
|
+
const source_id = `ctx_${Date.now().toString(36)}_${randomUUID().slice(0, 8)}`;
|
|
168
|
+
const chunks = chunk_text(text, source_id);
|
|
169
|
+
const preview_bytes = Buffer.byteLength(preview, 'utf8');
|
|
170
|
+
const insert = this.db.prepare(`
|
|
171
|
+
INSERT INTO context_sources (
|
|
172
|
+
id, session_id, project_path, tool_name, input_summary, created_at,
|
|
173
|
+
byte_count, line_count, content_hash, preview_byte_count, returned_byte_count
|
|
174
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
|
|
175
|
+
`);
|
|
176
|
+
const insert_chunk = this.db.prepare(`
|
|
177
|
+
INSERT INTO context_chunks (id, source_id, ordinal, title, content, byte_count)
|
|
178
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
179
|
+
`);
|
|
180
|
+
const update_returned = this.db.prepare(`
|
|
181
|
+
UPDATE context_sources SET returned_byte_count = ? WHERE id = ?
|
|
182
|
+
`);
|
|
183
|
+
const stored = with_sqlite_transaction(this.db, () => {
|
|
184
|
+
insert.run(source_id, session_id, project_path, tool_name, input_summary, created_at, bytes, lines, content_hash, preview_bytes);
|
|
185
|
+
for (const chunk of chunks) {
|
|
186
|
+
insert_chunk.run(chunk.id, chunk.source_id, chunk.ordinal, chunk.title, chunk.content, chunk.byte_count);
|
|
187
|
+
}
|
|
188
|
+
const provisional = {
|
|
189
|
+
source_id,
|
|
190
|
+
bytes,
|
|
191
|
+
lines,
|
|
192
|
+
preview,
|
|
193
|
+
receipt: '',
|
|
194
|
+
chunk_count: chunks.length,
|
|
195
|
+
first_chunk_id: chunks[0]?.id ?? null,
|
|
196
|
+
returned_bytes: 0,
|
|
197
|
+
project_path,
|
|
198
|
+
session_id,
|
|
199
|
+
capture_max_bytes: this.max_bytes,
|
|
200
|
+
capture_max_lines: this.max_lines,
|
|
201
|
+
};
|
|
202
|
+
const receipt = summarize_source(provisional, tool_name);
|
|
203
|
+
const returned_bytes = Buffer.byteLength(receipt, 'utf8');
|
|
204
|
+
update_returned.run(returned_bytes, source_id);
|
|
205
|
+
return { ...provisional, receipt, returned_bytes };
|
|
206
|
+
}, { operation: 'Store context output' });
|
|
207
|
+
return this.enforce_storage_limit(stored);
|
|
208
|
+
}
|
|
209
|
+
enforce_storage_limit(stored) {
|
|
210
|
+
const max_bytes = parse_context_retention_policy().max_bytes;
|
|
211
|
+
if (max_bytes === null)
|
|
212
|
+
return stored;
|
|
213
|
+
this.purge_to_max_stored_bytes(max_bytes);
|
|
214
|
+
// Do not return a receipt pointing to a source the quota just evicted.
|
|
215
|
+
return this.chunk_summary(stored.source_id, { global: true }) ? stored : null;
|
|
216
|
+
}
|
|
217
|
+
list(options = {}) {
|
|
218
|
+
const limit = Math.max(1, Math.min(options.limit ?? 10, 50));
|
|
219
|
+
const offset = Math.max(0, options.offset ?? 0);
|
|
220
|
+
const scoped = this.scoped_filter('context_sources', options);
|
|
221
|
+
const filters = [...scoped.where];
|
|
222
|
+
const params = [...scoped.params];
|
|
223
|
+
if (options.source_id) {
|
|
224
|
+
filters.push('context_sources.id = ?');
|
|
225
|
+
params.push(options.source_id);
|
|
226
|
+
}
|
|
227
|
+
if (options.tool_name) {
|
|
228
|
+
filters.push('context_sources.tool_name = ?');
|
|
229
|
+
params.push(options.tool_name);
|
|
230
|
+
}
|
|
231
|
+
if (options.newer_than_days !== undefined) {
|
|
232
|
+
filters.push('context_sources.created_at >= ?');
|
|
233
|
+
params.push(Date.now() - options.newer_than_days * 24 * 60 * 60 * 1000);
|
|
234
|
+
}
|
|
235
|
+
if (options.older_than_days !== undefined) {
|
|
236
|
+
filters.push('context_sources.created_at < ?');
|
|
237
|
+
params.push(Date.now() - options.older_than_days * 24 * 60 * 60 * 1000);
|
|
238
|
+
}
|
|
239
|
+
params.push(limit, offset);
|
|
240
|
+
const where_clause = filters.length
|
|
241
|
+
? `WHERE ${filters.join(' AND ')}`
|
|
242
|
+
: '';
|
|
243
|
+
const stmt = this.db.prepare(`
|
|
244
|
+
SELECT
|
|
245
|
+
context_sources.id as source_id,
|
|
246
|
+
context_sources.created_at,
|
|
247
|
+
context_sources.project_path,
|
|
248
|
+
context_sources.session_id,
|
|
249
|
+
context_sources.tool_name,
|
|
250
|
+
context_sources.input_summary,
|
|
251
|
+
context_sources.byte_count,
|
|
252
|
+
context_sources.line_count,
|
|
253
|
+
COUNT(context_chunks.id) as chunk_count,
|
|
254
|
+
(
|
|
255
|
+
SELECT title FROM context_chunks first_chunk
|
|
256
|
+
WHERE first_chunk.source_id = context_sources.id
|
|
257
|
+
ORDER BY ordinal LIMIT 1
|
|
258
|
+
) as first_chunk_title,
|
|
259
|
+
(
|
|
260
|
+
SELECT substr(content, 1, 240) FROM context_chunks first_chunk
|
|
261
|
+
WHERE first_chunk.source_id = context_sources.id
|
|
262
|
+
ORDER BY ordinal LIMIT 1
|
|
263
|
+
) as preview
|
|
264
|
+
FROM context_sources
|
|
265
|
+
LEFT JOIN context_chunks ON context_chunks.source_id = context_sources.id
|
|
266
|
+
${where_clause}
|
|
267
|
+
GROUP BY context_sources.id
|
|
268
|
+
ORDER BY context_sources.created_at DESC
|
|
269
|
+
LIMIT ? OFFSET ?
|
|
270
|
+
`);
|
|
271
|
+
return v
|
|
272
|
+
.parse(v.array(list_row_schema), stmt.all(...params))
|
|
273
|
+
.map((row) => ({
|
|
274
|
+
source_id: row.source_id,
|
|
275
|
+
created_at: row.created_at,
|
|
276
|
+
project_path: row.project_path,
|
|
277
|
+
session_id: row.session_id,
|
|
278
|
+
tool_name: row.tool_name,
|
|
279
|
+
input_summary: row.input_summary,
|
|
280
|
+
bytes: row.byte_count,
|
|
281
|
+
lines: row.line_count,
|
|
282
|
+
chunk_count: row.chunk_count,
|
|
283
|
+
first_chunk_title: row.first_chunk_title,
|
|
284
|
+
preview: row.preview,
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
search(query, options = {}) {
|
|
288
|
+
const limit = Math.max(1, Math.min(options.limit ?? 5, 25));
|
|
289
|
+
const strict = escape_fts5_query(query);
|
|
290
|
+
const relaxed = relaxed_fts5_query(query);
|
|
291
|
+
const results = this.search_match(strict, options, limit);
|
|
292
|
+
if (results.length >= limit || !relaxed || relaxed === strict)
|
|
293
|
+
return results;
|
|
294
|
+
const seen = new Set(results.map((result) => result.chunk_id));
|
|
295
|
+
for (const result of this.search_match(relaxed, options, limit)) {
|
|
296
|
+
if (seen.has(result.chunk_id))
|
|
297
|
+
continue;
|
|
298
|
+
results.push(result);
|
|
299
|
+
seen.add(result.chunk_id);
|
|
300
|
+
if (results.length >= limit)
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
return results;
|
|
304
|
+
}
|
|
305
|
+
search_match(match, options, limit) {
|
|
306
|
+
const scoped = this.scoped_filter('context_sources', options);
|
|
307
|
+
const filters = [...scoped.where];
|
|
308
|
+
const params = [match, ...scoped.params];
|
|
309
|
+
if (options.source_id) {
|
|
310
|
+
filters.push('context_sources.id = ?');
|
|
311
|
+
params.push(options.source_id);
|
|
312
|
+
}
|
|
313
|
+
if (options.tool_name) {
|
|
314
|
+
filters.push('context_sources.tool_name = ?');
|
|
315
|
+
params.push(options.tool_name);
|
|
316
|
+
}
|
|
317
|
+
params.push(limit);
|
|
318
|
+
const where_filters = filters.length
|
|
319
|
+
? ` AND ${filters.join(' AND ')}`
|
|
320
|
+
: '';
|
|
321
|
+
const content_expression = options.full_content
|
|
322
|
+
? 'context_chunks.content'
|
|
323
|
+
: "snippet(context_chunks_fts, 1, '', '', ' … ', 48)";
|
|
324
|
+
const stmt = this.db.prepare(`
|
|
325
|
+
SELECT
|
|
326
|
+
context_sources.id,
|
|
327
|
+
context_sources.tool_name,
|
|
328
|
+
context_sources.created_at,
|
|
329
|
+
context_sources.byte_count,
|
|
330
|
+
context_sources.line_count,
|
|
331
|
+
context_chunks.id as chunk_id,
|
|
332
|
+
context_chunks.ordinal,
|
|
333
|
+
context_chunks.title,
|
|
334
|
+
${content_expression} as content,
|
|
335
|
+
bm25(context_chunks_fts, 5.0, 1.0) as rank
|
|
336
|
+
FROM context_chunks_fts
|
|
337
|
+
JOIN context_chunks ON context_chunks.rowid = context_chunks_fts.rowid
|
|
338
|
+
JOIN context_sources ON context_sources.id = context_chunks.source_id
|
|
339
|
+
WHERE context_chunks_fts MATCH ?${where_filters}
|
|
340
|
+
ORDER BY rank
|
|
341
|
+
LIMIT ?
|
|
342
|
+
`);
|
|
343
|
+
return v
|
|
344
|
+
.parse(v.array(search_row_schema), stmt.all(...params))
|
|
345
|
+
.map((row) => ({
|
|
346
|
+
source_id: row.id,
|
|
347
|
+
chunk_id: row.chunk_id,
|
|
348
|
+
ordinal: row.ordinal,
|
|
349
|
+
title: row.title,
|
|
350
|
+
content: row.content,
|
|
351
|
+
tool_name: row.tool_name,
|
|
352
|
+
created_at: row.created_at,
|
|
353
|
+
bytes: row.byte_count,
|
|
354
|
+
lines: row.line_count,
|
|
355
|
+
rank: row.rank,
|
|
356
|
+
snippet: !options.full_content,
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
chunk_summary(source_id, options = {}) {
|
|
360
|
+
return context_store_chunk_summary(this, source_id, options);
|
|
361
|
+
}
|
|
362
|
+
get(source_id, chunk_id, options = {}) {
|
|
363
|
+
return context_store_get(this, source_id, chunk_id, options);
|
|
364
|
+
}
|
|
365
|
+
export_content(source_id, chunk_id, options = {}) {
|
|
366
|
+
const chunks = this.get(source_id, chunk_id, options);
|
|
367
|
+
const content = chunks.map((chunk) => chunk.content).join('');
|
|
368
|
+
const hash = chunk_id
|
|
369
|
+
? null
|
|
370
|
+
: this.source_content_hash(source_id, options);
|
|
371
|
+
const verified = hash
|
|
372
|
+
? createHash('sha256').update(content).digest('hex') === hash
|
|
373
|
+
: null;
|
|
374
|
+
return { chunks, content, verified };
|
|
375
|
+
}
|
|
376
|
+
source_content_hash(source_id, options = {}) {
|
|
377
|
+
const scoped = this.scoped_filter('context_sources', options);
|
|
378
|
+
const filters = ['context_sources.id = ?', ...scoped.where];
|
|
379
|
+
const params = [
|
|
380
|
+
source_id,
|
|
381
|
+
...scoped.params,
|
|
382
|
+
];
|
|
383
|
+
const row = v.parse(v.optional(content_hash_row_schema), this.db
|
|
384
|
+
.prepare(`SELECT content_hash FROM context_sources WHERE ${filters.join(' AND ')} LIMIT 1`)
|
|
385
|
+
.get(...params));
|
|
386
|
+
return row?.content_hash ?? null;
|
|
387
|
+
}
|
|
388
|
+
stats(options = { global: true }) {
|
|
389
|
+
return context_store_stats(this, options);
|
|
390
|
+
}
|
|
391
|
+
cleanup(policy = parse_context_retention_policy()) {
|
|
392
|
+
return context_store_cleanup(this, policy);
|
|
393
|
+
}
|
|
394
|
+
purge_to_max_stored_bytes(max_bytes) {
|
|
395
|
+
return context_store_purge_to_max_stored_bytes(this, max_bytes);
|
|
396
|
+
}
|
|
397
|
+
purge(options = {}) {
|
|
398
|
+
return this.purge_with_details(options).deleted;
|
|
399
|
+
}
|
|
400
|
+
purge_with_details(options = {}) {
|
|
401
|
+
return context_store_purge_with_details(this, options);
|
|
402
|
+
}
|
|
403
|
+
close() {
|
|
404
|
+
if (this.db.isOpen)
|
|
405
|
+
this.db.close();
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,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,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IACrC,CAAC;CACD"}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ContextChunk, ContextStoreOptions, StoredContextOutput } from './types.js';
|
|
2
|
+
export declare const DEFAULT_CONTEXT_MAX_BYTES: number;
|
|
3
|
+
export declare const DEFAULT_CONTEXT_MAX_LINES = 300;
|
|
4
|
+
export declare function count_lines(text: string): number;
|
|
5
|
+
export declare function should_index_text(text: string, options?: Pick<ContextStoreOptions, 'max_bytes' | 'max_lines'>): boolean;
|
|
6
|
+
export declare function escape_fts5_query(query: string): string;
|
|
7
|
+
export declare function relaxed_fts5_query(query: string): string | null;
|
|
8
|
+
export declare function make_preview(text: string, max_lines?: number, max_bytes?: number): string;
|
|
9
|
+
export declare function chunk_text(text: string, source_id: string): ContextChunk[];
|
|
10
|
+
export declare function format_bytes(bytes: number): string;
|
|
11
|
+
export declare function summarize_source(result: StoredContextOutput, tool_name: string): string;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
export const DEFAULT_CONTEXT_MAX_BYTES = 24 * 1024;
|
|
2
|
+
export const DEFAULT_CONTEXT_MAX_LINES = 300;
|
|
3
|
+
const DEFAULT_PREVIEW_LINES = 40;
|
|
4
|
+
const DEFAULT_PREVIEW_BYTES = 4 * 1024;
|
|
5
|
+
export function count_lines(text) {
|
|
6
|
+
if (!text)
|
|
7
|
+
return 0;
|
|
8
|
+
return text.split('\n').length;
|
|
9
|
+
}
|
|
10
|
+
export function should_index_text(text, options = {}) {
|
|
11
|
+
const max_bytes = options.max_bytes ?? DEFAULT_CONTEXT_MAX_BYTES;
|
|
12
|
+
const max_lines = options.max_lines ?? DEFAULT_CONTEXT_MAX_LINES;
|
|
13
|
+
return (Buffer.byteLength(text, 'utf8') > max_bytes ||
|
|
14
|
+
count_lines(text) > max_lines);
|
|
15
|
+
}
|
|
16
|
+
export function escape_fts5_query(query) {
|
|
17
|
+
const trimmed = query.trim();
|
|
18
|
+
if (!trimmed)
|
|
19
|
+
return '""';
|
|
20
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
|
21
|
+
return trimmed.replace(/"(.*)"/s, (_match, inner) => `"${inner.replace(/"/g, '""')}"`);
|
|
22
|
+
}
|
|
23
|
+
const tokens = normalized_fts_tokens(trimmed).map(format_fts_token);
|
|
24
|
+
return tokens.length > 0 ? tokens.join(' ') : '""';
|
|
25
|
+
}
|
|
26
|
+
export function relaxed_fts5_query(query) {
|
|
27
|
+
const normalized = normalized_fts_tokens(query);
|
|
28
|
+
if (normalized.length < 2)
|
|
29
|
+
return null;
|
|
30
|
+
const tokens = normalized
|
|
31
|
+
.flatMap((token) => token.value.split(/\s+/).map((value) => ({
|
|
32
|
+
value,
|
|
33
|
+
prefix: token.prefix,
|
|
34
|
+
})))
|
|
35
|
+
.filter((token) => token.value.length > 1)
|
|
36
|
+
.filter((token) => !new Set([
|
|
37
|
+
'and',
|
|
38
|
+
'or',
|
|
39
|
+
'not',
|
|
40
|
+
'the',
|
|
41
|
+
'this',
|
|
42
|
+
'that',
|
|
43
|
+
'with',
|
|
44
|
+
'from',
|
|
45
|
+
'into',
|
|
46
|
+
'specific',
|
|
47
|
+
'chunk',
|
|
48
|
+
'line',
|
|
49
|
+
]).has(token.value.toLowerCase()))
|
|
50
|
+
.slice(0, 12)
|
|
51
|
+
.map(format_fts_token);
|
|
52
|
+
if (tokens.length === 0)
|
|
53
|
+
return null;
|
|
54
|
+
return tokens.join(' OR ');
|
|
55
|
+
}
|
|
56
|
+
function normalized_fts_tokens(query) {
|
|
57
|
+
return query
|
|
58
|
+
.trim()
|
|
59
|
+
.split(/\s+/)
|
|
60
|
+
.map((token) => token.trim())
|
|
61
|
+
.filter(Boolean)
|
|
62
|
+
.map((token) => {
|
|
63
|
+
const prefix = token.endsWith('*');
|
|
64
|
+
const base = prefix ? token.slice(0, -1) : token;
|
|
65
|
+
return {
|
|
66
|
+
prefix,
|
|
67
|
+
value: base
|
|
68
|
+
.replace(/["'(){}[\]^:./\\+-]/g, ' ')
|
|
69
|
+
.trim()
|
|
70
|
+
.replace(/\s+/g, ' '),
|
|
71
|
+
};
|
|
72
|
+
})
|
|
73
|
+
.filter((token) => token.value.length > 0);
|
|
74
|
+
}
|
|
75
|
+
function format_fts_token(token) {
|
|
76
|
+
const quoted = `"${token.value.replace(/"/g, '""')}"`;
|
|
77
|
+
return token.prefix ? `${quoted}*` : quoted;
|
|
78
|
+
}
|
|
79
|
+
export function make_preview(text, max_lines = DEFAULT_PREVIEW_LINES, max_bytes = DEFAULT_PREVIEW_BYTES) {
|
|
80
|
+
const lines = text.split('\n');
|
|
81
|
+
let preview;
|
|
82
|
+
if (lines.length <= max_lines) {
|
|
83
|
+
preview = text;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const head_count = Math.ceil(max_lines / 2);
|
|
87
|
+
const tail_count = Math.floor(max_lines / 2);
|
|
88
|
+
const omitted = lines.length - head_count - tail_count;
|
|
89
|
+
preview = [
|
|
90
|
+
...lines.slice(0, head_count),
|
|
91
|
+
``,
|
|
92
|
+
`[... ${omitted} lines omitted; indexed in context sidecar ...]`,
|
|
93
|
+
``,
|
|
94
|
+
...lines.slice(-tail_count),
|
|
95
|
+
].join('\n');
|
|
96
|
+
}
|
|
97
|
+
return take_utf8_bytes(preview, max_bytes);
|
|
98
|
+
}
|
|
99
|
+
function take_utf8_bytes(text, max_bytes) {
|
|
100
|
+
if (Buffer.byteLength(text, 'utf8') <= max_bytes)
|
|
101
|
+
return text;
|
|
102
|
+
let bytes = 0;
|
|
103
|
+
let output = '';
|
|
104
|
+
for (const char of text) {
|
|
105
|
+
const char_bytes = Buffer.byteLength(char, 'utf8');
|
|
106
|
+
if (bytes + char_bytes > max_bytes)
|
|
107
|
+
break;
|
|
108
|
+
bytes += char_bytes;
|
|
109
|
+
output += char;
|
|
110
|
+
}
|
|
111
|
+
return `${output}\n[... preview truncated at ${format_bytes(max_bytes)} ...]`;
|
|
112
|
+
}
|
|
113
|
+
export function chunk_text(text, source_id) {
|
|
114
|
+
const target_bytes = 4096;
|
|
115
|
+
const chunks = split_lossless_chunks(text, target_bytes);
|
|
116
|
+
return chunks.map((content, index) => ({
|
|
117
|
+
id: `${source_id}_${String(index + 1).padStart(4, '0')}`,
|
|
118
|
+
source_id,
|
|
119
|
+
ordinal: index + 1,
|
|
120
|
+
title: first_non_empty_line(content),
|
|
121
|
+
content,
|
|
122
|
+
byte_count: Buffer.byteLength(content, 'utf8'),
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
function split_lossless_chunks(text, target_bytes) {
|
|
126
|
+
const chunks = [];
|
|
127
|
+
let rest = text;
|
|
128
|
+
while (Buffer.byteLength(rest, 'utf8') > target_bytes) {
|
|
129
|
+
const [head, tail] = split_utf8_at_byte(rest, target_bytes);
|
|
130
|
+
const break_index = preferred_break_index(head);
|
|
131
|
+
if (break_index === null) {
|
|
132
|
+
chunks.push(head);
|
|
133
|
+
rest = tail;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
chunks.push(rest.slice(0, break_index));
|
|
137
|
+
rest = rest.slice(break_index);
|
|
138
|
+
}
|
|
139
|
+
if (rest || chunks.length === 0)
|
|
140
|
+
chunks.push(rest);
|
|
141
|
+
return chunks;
|
|
142
|
+
}
|
|
143
|
+
function preferred_break_index(head) {
|
|
144
|
+
const minimum = Math.floor(head.length * 0.6);
|
|
145
|
+
for (const index of [
|
|
146
|
+
last_paragraph_break_index(head),
|
|
147
|
+
head.lastIndexOf('\n') + 1,
|
|
148
|
+
head.lastIndexOf(' ') + 1,
|
|
149
|
+
head.lastIndexOf('\t') + 1,
|
|
150
|
+
]) {
|
|
151
|
+
if (index >= minimum)
|
|
152
|
+
return index;
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
function last_paragraph_break_index(text) {
|
|
157
|
+
let index = 0;
|
|
158
|
+
for (const match of text.matchAll(/\n{2,}/g))
|
|
159
|
+
index = match.index + match[0].length;
|
|
160
|
+
return index;
|
|
161
|
+
}
|
|
162
|
+
function split_utf8_at_byte(text, max_bytes) {
|
|
163
|
+
let bytes = 0;
|
|
164
|
+
let index = 0;
|
|
165
|
+
for (const char of text) {
|
|
166
|
+
const char_bytes = Buffer.byteLength(char, 'utf8');
|
|
167
|
+
if (bytes + char_bytes > max_bytes)
|
|
168
|
+
break;
|
|
169
|
+
bytes += char_bytes;
|
|
170
|
+
index += char.length;
|
|
171
|
+
}
|
|
172
|
+
return [text.slice(0, index), text.slice(index)];
|
|
173
|
+
}
|
|
174
|
+
function first_non_empty_line(text) {
|
|
175
|
+
const line = text
|
|
176
|
+
.split('\n')
|
|
177
|
+
.map((value) => value.trim())
|
|
178
|
+
.find(Boolean);
|
|
179
|
+
return line ? line.slice(0, 120) : null;
|
|
180
|
+
}
|
|
181
|
+
export function format_bytes(bytes) {
|
|
182
|
+
if (bytes < 1024)
|
|
183
|
+
return `${bytes} B`;
|
|
184
|
+
if (bytes < 1024 * 1024)
|
|
185
|
+
return `${(bytes / 1024).toFixed(1)} KiB`;
|
|
186
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MiB`;
|
|
187
|
+
}
|
|
188
|
+
export function summarize_source(result, tool_name) {
|
|
189
|
+
const capture_reason = format_capture_reason(result);
|
|
190
|
+
return [
|
|
191
|
+
result.deduped
|
|
192
|
+
? `[context-sidecar] Duplicate large ${tool_name} output reused existing local index`
|
|
193
|
+
: `[context-sidecar] Large ${tool_name} output indexed locally`,
|
|
194
|
+
``,
|
|
195
|
+
`Source: ${result.source_id}`,
|
|
196
|
+
`Size: ${format_bytes(result.bytes)}, ${result.lines} lines, ${result.chunk_count} chunks`,
|
|
197
|
+
capture_reason,
|
|
198
|
+
result.first_chunk_id
|
|
199
|
+
? `First chunk id: ${result.first_chunk_id}`
|
|
200
|
+
: undefined,
|
|
201
|
+
``,
|
|
202
|
+
`Search snippets with context_search; use context_get for focused chunks or context_export for broad processing.`,
|
|
203
|
+
``,
|
|
204
|
+
`Preview:`,
|
|
205
|
+
make_preview(result.preview, 8, 768),
|
|
206
|
+
]
|
|
207
|
+
.filter((line) => line !== undefined)
|
|
208
|
+
.join('\n');
|
|
209
|
+
}
|
|
210
|
+
function format_capture_reason(result) {
|
|
211
|
+
if (result.capture_max_bytes === undefined ||
|
|
212
|
+
result.capture_max_lines === undefined)
|
|
213
|
+
return undefined;
|
|
214
|
+
const reasons = [];
|
|
215
|
+
if (result.bytes > result.capture_max_bytes) {
|
|
216
|
+
reasons.push(`${format_bytes(result.bytes)} exceeds ${format_bytes(result.capture_max_bytes)}`);
|
|
217
|
+
}
|
|
218
|
+
if (result.lines > result.capture_max_lines) {
|
|
219
|
+
reasons.push(`${result.lines} lines exceeds ${result.capture_max_lines}`);
|
|
220
|
+
}
|
|
221
|
+
if (reasons.length === 0) {
|
|
222
|
+
reasons.push(`forced capture; thresholds are ${format_bytes(result.capture_max_bytes)} or ${result.capture_max_lines} lines`);
|
|
223
|
+
}
|
|
224
|
+
return `Captured: ${reasons.join('; ')}.`;
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=text.js.map
|