@phren/cli 0.2.1 → 0.2.3
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/dist/content/summarize.d.ts +7 -2
- package/dist/content/summarize.js +45 -15
- package/dist/shared/index.js +8 -1
- package/package.json +1 -1
|
@@ -43,10 +43,15 @@ export declare function digestTopic(slug: string, file: string, bullets: Archive
|
|
|
43
43
|
export declare function structuralNow(d: TopicDigest): string;
|
|
44
44
|
/** A prose paragraph over the newest bullets, when a model is configured; "" otherwise. */
|
|
45
45
|
export declare function proseNow(d: TopicDigest, bullets: ArchivedBullet[], signal?: AbortSignal): Promise<string>;
|
|
46
|
+
/** Fingerprint of everything in a topic file except its own Now block. */
|
|
47
|
+
export declare function contentFingerprint(content: string, start: string, end: string): string;
|
|
46
48
|
/** Insert or replace a marked block. Topic files: before the first section; summary: at the end. */
|
|
47
49
|
export declare function upsertBlock(content: string, start: string, end: string, rendered: string, where: "top" | "bottom"): string;
|
|
48
|
-
/** The stamp inside an existing block, so unchanged files are not rewritten. */
|
|
49
|
-
export declare function
|
|
50
|
+
/** The stamp and fingerprint inside an existing block, so unchanged files are not rewritten. */
|
|
51
|
+
export declare function blockMeta(content: string, start: string): {
|
|
52
|
+
at: string;
|
|
53
|
+
hash: string | null;
|
|
54
|
+
} | null;
|
|
50
55
|
export interface SummarizeOptions {
|
|
51
56
|
/** Ask a configured model for prose; falls back to structural when none answers. */
|
|
52
57
|
llm?: boolean;
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import * as fs from "fs";
|
|
17
17
|
import * as path from "path";
|
|
18
|
+
import { createHash } from "crypto";
|
|
18
19
|
import { callLlm } from "./dedup.js";
|
|
19
20
|
import { readProjectTopics, topicReferencePath } from "../project-topics.js";
|
|
20
21
|
import { storeAwareProjectPath } from "../store-routing.js";
|
|
@@ -100,16 +101,37 @@ export function digestTopic(slug, file, bullets) {
|
|
|
100
101
|
};
|
|
101
102
|
}
|
|
102
103
|
function plural(n, word) {
|
|
103
|
-
if (word === "context")
|
|
104
|
-
return `${n} context note${n === 1 ? "" : "s"}`;
|
|
105
104
|
return `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
106
105
|
}
|
|
106
|
+
/** Tags are not all nouns that take an s: "18 architectures" and "8 code-qualitys" read as bugs. */
|
|
107
|
+
const TAG_PLURALS = {
|
|
108
|
+
bug: ["bug", "bugs"],
|
|
109
|
+
pitfall: ["pitfall", "pitfalls"],
|
|
110
|
+
pattern: ["pattern", "patterns"],
|
|
111
|
+
decision: ["decision", "decisions"],
|
|
112
|
+
tradeoff: ["tradeoff", "tradeoffs"],
|
|
113
|
+
context: ["context note", "context notes"],
|
|
114
|
+
architecture: ["architecture note", "architecture notes"],
|
|
115
|
+
"code-quality": ["code-quality note", "code-quality notes"],
|
|
116
|
+
competitive: ["competitive note", "competitive notes"],
|
|
117
|
+
security: ["security note", "security notes"],
|
|
118
|
+
performance: ["performance note", "performance notes"],
|
|
119
|
+
tooling: ["tooling note", "tooling notes"],
|
|
120
|
+
workflow: ["workflow note", "workflow notes"],
|
|
121
|
+
api: ["API note", "API notes"],
|
|
122
|
+
};
|
|
123
|
+
function tagCount(n, tag) {
|
|
124
|
+
const forms = TAG_PLURALS[tag];
|
|
125
|
+
if (forms)
|
|
126
|
+
return `${n} ${n === 1 ? forms[0] : forms[1]}`;
|
|
127
|
+
return `${n} ${tag}`;
|
|
128
|
+
}
|
|
107
129
|
/** The paragraph that can always be written, no model required. */
|
|
108
130
|
export function structuralNow(d) {
|
|
109
131
|
if (!d.bullets)
|
|
110
132
|
return "Nothing archived under this topic yet.";
|
|
111
133
|
const span = d.first && d.last ? (d.first === d.last ? `archived on ${d.first}` : `archived between ${d.first} and ${d.last}`) : "";
|
|
112
|
-
const tagLine = Object.entries(d.tags).filter(([t]) => t !== "untagged").sort((a, b) => b[1] - a[1]).slice(0, 4).map(([t, n]) =>
|
|
134
|
+
const tagLine = Object.entries(d.tags).filter(([t]) => t !== "untagged").sort((a, b) => b[1] - a[1]).slice(0, 4).map(([t, n]) => tagCount(n, t)).join(", ");
|
|
113
135
|
const parts = [`${plural(d.bullets, "finding")}${span ? `, ${span}` : ""}${tagLine ? `: ${tagLine}` : ""}.`];
|
|
114
136
|
if (d.mentioned.length)
|
|
115
137
|
parts.push(`Keeps coming back to ${d.mentioned.join(", ")}.`);
|
|
@@ -130,8 +152,15 @@ export async function proseNow(d, bullets, signal) {
|
|
|
130
152
|
return "";
|
|
131
153
|
}
|
|
132
154
|
}
|
|
133
|
-
function block(start, end, heading, body, stamp) {
|
|
134
|
-
return `${start.replace("-->", `at=${stamp} -->`)}\n${heading}\n\n${body}\n${end}`;
|
|
155
|
+
function block(start, end, heading, body, stamp, hash = "") {
|
|
156
|
+
return `${start.replace("-->", `at=${stamp}${hash ? ` hash=${hash}` : ""} -->`)}\n${heading}\n\n${body}\n${end}`;
|
|
157
|
+
}
|
|
158
|
+
/** Fingerprint of everything in a topic file except its own Now block. */
|
|
159
|
+
export function contentFingerprint(content, start, end) {
|
|
160
|
+
const s = content.indexOf(start.replace("-->", ""));
|
|
161
|
+
const e = content.indexOf(end);
|
|
162
|
+
const rest = s !== -1 && e !== -1 && e > s ? content.slice(0, s) + content.slice(e + end.length) : content;
|
|
163
|
+
return createHash("sha1").update(rest.replace(/\s+/g, " ").trim()).digest("hex").slice(0, 12);
|
|
135
164
|
}
|
|
136
165
|
/** Insert or replace a marked block. Topic files: before the first section; summary: at the end. */
|
|
137
166
|
export function upsertBlock(content, start, end, rendered, where) {
|
|
@@ -149,10 +178,10 @@ export function upsertBlock(content, start, end, rendered, where) {
|
|
|
149
178
|
}
|
|
150
179
|
return `${content.trimEnd()}\n\n${rendered}\n`;
|
|
151
180
|
}
|
|
152
|
-
/** The stamp inside an existing block, so unchanged files are not rewritten. */
|
|
153
|
-
export function
|
|
154
|
-
const m = new RegExp(`${start.replace("-->", "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}at=([^ ]+) -->`).exec(content);
|
|
155
|
-
return m ? m[1] : null;
|
|
181
|
+
/** The stamp and fingerprint inside an existing block, so unchanged files are not rewritten. */
|
|
182
|
+
export function blockMeta(content, start) {
|
|
183
|
+
const m = new RegExp(`${start.replace("-->", "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}at=([^ ]+)(?: hash=([0-9a-f]+))? -->`).exec(content);
|
|
184
|
+
return m ? { at: m[1], hash: m[2] ?? null } : null;
|
|
156
185
|
}
|
|
157
186
|
/**
|
|
158
187
|
* Move whole "## Archived <date>" sections, oldest first, into <topic>.older.md
|
|
@@ -189,13 +218,14 @@ export function splitTopicFile(filePath, maxBullets = TOPIC_SPLIT_AT) {
|
|
|
189
218
|
export async function summarizeTopicFile(filePath, slug, opts = {}) {
|
|
190
219
|
const split = splitTopicFile(filePath);
|
|
191
220
|
const content = fs.readFileSync(filePath, "utf8");
|
|
192
|
-
const stat = fs.statSync(filePath);
|
|
193
221
|
const stamp = (opts.now ?? (() => new Date()))().toISOString();
|
|
194
|
-
const prev =
|
|
222
|
+
const prev = blockMeta(content, NOW_START);
|
|
223
|
+
const fingerprint = contentFingerprint(content, NOW_START, NOW_END);
|
|
195
224
|
const bullets = parseTopicBullets(content);
|
|
196
225
|
const digest = digestTopic(slug, filePath, bullets);
|
|
197
|
-
// The block
|
|
198
|
-
|
|
226
|
+
// The block carries a fingerprint of the rest of the file; unchanged bullets
|
|
227
|
+
// mean an unchanged summary, whatever the clock says.
|
|
228
|
+
if (!opts.force && prev?.hash === fingerprint && !split) {
|
|
199
229
|
const existing = content.slice(content.indexOf(NOW_START.replace("-->", "")), content.indexOf(NOW_END));
|
|
200
230
|
return { slug, file: filePath, bullets: bullets.length, updated: false, now: existing.split("\n").slice(2).join("\n").trim() };
|
|
201
231
|
}
|
|
@@ -204,7 +234,7 @@ export async function summarizeTopicFile(filePath, slug, opts = {}) {
|
|
|
204
234
|
now = await proseNow(digest, bullets, AbortSignal.timeout(25_000));
|
|
205
235
|
if (!now)
|
|
206
236
|
now = structuralNow(digest);
|
|
207
|
-
const next = upsertBlock(content, NOW_START, NOW_END, block(NOW_START, NOW_END, "## Now", now, stamp), "top");
|
|
237
|
+
const next = upsertBlock(content, NOW_START, NOW_END, block(NOW_START, NOW_END, "## Now", now, stamp, fingerprint), "top");
|
|
208
238
|
if (next !== content)
|
|
209
239
|
atomicWriteText(filePath, next);
|
|
210
240
|
return { slug, file: filePath, bullets: bullets.length, updated: next !== content, split: split ?? undefined, now };
|
|
@@ -277,7 +307,7 @@ export async function summarizeProject(phrenPath, project, opts = {}) {
|
|
|
277
307
|
const rendered = block(KNOWS_START, KNOWS_END, "## What phren knows", lines.join("\n"), stamp);
|
|
278
308
|
const existing = fs.existsSync(summaryPath) ? fs.readFileSync(summaryPath, "utf8") : `# ${project}\n`;
|
|
279
309
|
const next = upsertBlock(existing, KNOWS_START, KNOWS_END, rendered, "bottom");
|
|
280
|
-
const stripStamp = (s) => s.replace(/ at=[^ ]+ -->/g, " -->");
|
|
310
|
+
const stripStamp = (s) => s.replace(/ at=[^ ]+( hash=[0-9a-f]+)? -->/g, " -->");
|
|
281
311
|
const changed = stripStamp(next) !== stripStamp(existing);
|
|
282
312
|
if (changed)
|
|
283
313
|
atomicWriteText(summaryPath, next);
|
package/dist/shared/index.js
CHANGED
|
@@ -1966,7 +1966,14 @@ export function detectProject(phrenPath, cwd, profile) {
|
|
|
1966
1966
|
|| getProjectSourcePath(phrenPath, projectName);
|
|
1967
1967
|
if (!sourcePath)
|
|
1968
1968
|
continue;
|
|
1969
|
-
|
|
1969
|
+
// Exact first. Then case-insensitive: a sourcePath written on a
|
|
1970
|
+
// case-insensitive filesystem (macOS) and synced here can differ from the
|
|
1971
|
+
// real directory only in case — "/home/me/projects/x" against
|
|
1972
|
+
// "/home/me/Projects/x" — and without this the project is invisible to the
|
|
1973
|
+
// hook on that machine, with nothing to say why.
|
|
1974
|
+
const matches = resolvedCwd === sourcePath || resolvedCwd.startsWith(sourcePath + path.sep)
|
|
1975
|
+
|| resolvedCwd.toLowerCase() === sourcePath.toLowerCase()
|
|
1976
|
+
|| resolvedCwd.toLowerCase().startsWith(sourcePath.toLowerCase() + path.sep);
|
|
1970
1977
|
if (!matches)
|
|
1971
1978
|
continue;
|
|
1972
1979
|
if (!bestMatch || sourcePath.length > bestMatch.length) {
|