@aeondave/pi-persona-mind 0.9.6 → 0.9.8
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/AGENTS.md +137 -135
- package/CHANGELOG.md +287 -270
- package/LICENSE +24 -24
- package/README.md +84 -171
- package/docs/DESIGN.md +605 -573
- package/docs/MEMORY-RESEARCH.md +163 -163
- package/package.json +15 -6
- package/src/core/auto-optimize.ts +323 -249
- package/src/core/backlog.ts +228 -228
- package/src/core/blocked.ts +38 -38
- package/src/core/capture.ts +155 -154
- package/src/core/context-budget.ts +74 -74
- package/src/core/handoff-store.ts +111 -111
- package/src/core/handoff.ts +70 -70
- package/src/core/host-compat.ts +16 -16
- package/src/core/ids.ts +59 -59
- package/src/core/inject.ts +247 -247
- package/src/core/knowledge-review.ts +107 -107
- package/src/core/memory-read.ts +34 -34
- package/src/core/memory.ts +321 -321
- package/src/core/migrate.ts +713 -713
- package/src/core/mind-settings.ts +54 -54
- package/src/core/optimize-history.ts +163 -163
- package/src/core/optimize-service.ts +189 -180
- package/src/core/optimize.ts +127 -127
- package/src/core/retrieval.ts +164 -164
- package/src/core/scanner.ts +146 -146
- package/src/core/scope.ts +201 -205
- package/src/core/service.ts +720 -720
- package/src/core/session-memory.ts +71 -71
- package/src/core/store.ts +1002 -1002
- package/src/extension/activity.ts +79 -79
- package/src/extension/auto-optimize-session.ts +143 -143
- package/src/extension/auto-optimize.ts +565 -569
- package/src/extension/handoff.ts +23 -23
- package/src/extension/knowledge-review.ts +229 -229
- package/src/extension/memory-inspector.ts +82 -82
- package/src/extension/mind-actions.ts +38 -38
- package/src/extension/mind-worker.ts +32 -32
- package/src/extension/persona-dependency.ts +76 -76
- package/src/extension/reference-anchor.ts +48 -0
- package/src/extension/review-evidence.ts +222 -222
- package/src/extension/worker-memory-anchor.ts +28 -28
- package/src/index.ts +1357 -1310
- package/src/session-memory.ts +127 -122
- package/src/tools/backlog.ts +126 -126
- package/src/tools/memory-handoff.ts +30 -30
- package/src/tools/memory-history.ts +46 -46
- package/src/tools/memory.ts +6 -4
- package/src/ui/card-frame.ts +161 -161
- package/src/ui/inspector-frame.ts +44 -44
- package/src/ui/memory-inspector.ts +464 -464
- package/src/ui/mind-result-card.ts +48 -48
- package/src/ui/presentation.ts +87 -87
- package/src/ui/session-note-card.ts +58 -56
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { cardFrameDetailHint, cardFramePresentation, type MindCardUI } from "./card-frame.ts";
|
|
3
|
-
|
|
4
|
-
export const MIND_RESULT_ENTRY_TYPE = "pi-persona-mind-result";
|
|
5
|
-
const MAX_TITLE_CHARS = 160;
|
|
6
|
-
const MAX_BODY_CHARS = 1_000_000;
|
|
7
|
-
const AUTOMATIC_NO_OP_PREFIX = "Bounded coverage: one selected record window; bounded review found no safe consolidation in this record set.\nWorker ";
|
|
8
|
-
interface AutomaticResultContext {
|
|
9
|
-
automatic: true;
|
|
10
|
-
target: "project" | "project-long" | "persona";
|
|
11
|
-
phase: "result";
|
|
12
|
-
summary: string;
|
|
13
|
-
attention: boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function automaticContext(value: unknown): AutomaticResultContext | undefined {
|
|
17
|
-
if (!value || typeof value !== "object") return undefined;
|
|
18
|
-
const context = value as Record<string, unknown>;
|
|
19
|
-
if (context.automatic !== true || context.phase !== "result" || (context.target !== "project" && context.target !== "project-long" && context.target !== "persona") || typeof context.summary !== "string" || typeof context.attention !== "boolean") return undefined;
|
|
20
|
-
return context as unknown as AutomaticResultContext;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function cardTitle(title: string): string {
|
|
24
|
-
const automatic = /^Mind automatic (?:optimization|result|proposal)( — .+)?$/.exec(title);
|
|
25
|
-
if (automatic) return `mind · automatic review${automatic[1] ?? ""}`;
|
|
26
|
-
return title.replace(/^(Mind|Memory|Automatic memory|Staged memory)\b/, (prefix) => prefix[0]!.toLowerCase() + prefix.slice(1));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Register once per factory; command output stays display-only and can be expanded without loss. */
|
|
30
|
-
export function registerMindResultRenderer(pi: Pick<ExtensionAPI, "registerEntryRenderer">, cardUI: MindCardUI): void {
|
|
31
|
-
pi.registerEntryRenderer<{ title: string; content: string; reportContext?: unknown }>(MIND_RESULT_ENTRY_TYPE, (entry, { expanded, keyHint }, theme) => {
|
|
32
|
-
const data = entry.data;
|
|
33
|
-
if (!data || typeof data.title !== "string" || data.title.length < 1 || data.title.length > MAX_TITLE_CHARS || typeof data.content !== "string" || data.content.length > MAX_BODY_CHARS) return undefined;
|
|
34
|
-
const report = automaticContext(data.reportContext);
|
|
35
|
-
const legacyAutomatic = /^Mind automatic (?:optimization|result|proposal)(?: — .+)?$/.test(data.title);
|
|
36
|
-
const manualNoOp = /^Mind optimization result(?: — .+)?$/.test(data.title) && data.content.startsWith(AUTOMATIC_NO_OP_PREFIX);
|
|
37
|
-
const presentation = report || legacyAutomatic
|
|
38
|
-
? { title: report ? `mind · ${report.target} · ${report.summary}` : cardTitle(data.title), state: report?.attention ? "attention" as const : "neutral" as const, full: data.content, preview: () => ({ text: "", truncated: true }) }
|
|
39
|
-
: cardFramePresentation(cardTitle(data.title), data.content, manualNoOp ? "One selected window reviewed; no consolidation proposed" : undefined);
|
|
40
|
-
return cardUI.renderCard(presentation, {
|
|
41
|
-
expanded,
|
|
42
|
-
theme,
|
|
43
|
-
outputPad: cardUI.outputPad(),
|
|
44
|
-
detailHint: cardFrameDetailHint(keyHint),
|
|
45
|
-
expansionKey: entry,
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
}
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { cardFrameDetailHint, cardFramePresentation, type MindCardUI } from "./card-frame.ts";
|
|
3
|
+
|
|
4
|
+
export const MIND_RESULT_ENTRY_TYPE = "pi-persona-mind-result";
|
|
5
|
+
const MAX_TITLE_CHARS = 160;
|
|
6
|
+
const MAX_BODY_CHARS = 1_000_000;
|
|
7
|
+
const AUTOMATIC_NO_OP_PREFIX = "Bounded coverage: one selected record window; bounded review found no safe consolidation in this record set.\nWorker ";
|
|
8
|
+
interface AutomaticResultContext {
|
|
9
|
+
automatic: true;
|
|
10
|
+
target: "project" | "project-long" | "persona";
|
|
11
|
+
phase: "result";
|
|
12
|
+
summary: string;
|
|
13
|
+
attention: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function automaticContext(value: unknown): AutomaticResultContext | undefined {
|
|
17
|
+
if (!value || typeof value !== "object") return undefined;
|
|
18
|
+
const context = value as Record<string, unknown>;
|
|
19
|
+
if (context.automatic !== true || context.phase !== "result" || (context.target !== "project" && context.target !== "project-long" && context.target !== "persona") || typeof context.summary !== "string" || typeof context.attention !== "boolean") return undefined;
|
|
20
|
+
return context as unknown as AutomaticResultContext;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function cardTitle(title: string): string {
|
|
24
|
+
const automatic = /^Mind automatic (?:optimization|result|proposal)( — .+)?$/.exec(title);
|
|
25
|
+
if (automatic) return `mind · automatic review${automatic[1] ?? ""}`;
|
|
26
|
+
return title.replace(/^(Mind|Memory|Automatic memory|Staged memory)\b/, (prefix) => prefix[0]!.toLowerCase() + prefix.slice(1));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Register once per factory; command output stays display-only and can be expanded without loss. */
|
|
30
|
+
export function registerMindResultRenderer(pi: Pick<ExtensionAPI, "registerEntryRenderer">, cardUI: MindCardUI): void {
|
|
31
|
+
pi.registerEntryRenderer<{ title: string; content: string; reportContext?: unknown }>(MIND_RESULT_ENTRY_TYPE, (entry, { expanded, keyHint }, theme) => {
|
|
32
|
+
const data = entry.data;
|
|
33
|
+
if (!data || typeof data.title !== "string" || data.title.length < 1 || data.title.length > MAX_TITLE_CHARS || typeof data.content !== "string" || data.content.length > MAX_BODY_CHARS) return undefined;
|
|
34
|
+
const report = automaticContext(data.reportContext);
|
|
35
|
+
const legacyAutomatic = /^Mind automatic (?:optimization|result|proposal)(?: — .+)?$/.test(data.title);
|
|
36
|
+
const manualNoOp = /^Mind optimization result(?: — .+)?$/.test(data.title) && data.content.startsWith(AUTOMATIC_NO_OP_PREFIX);
|
|
37
|
+
const presentation = report || legacyAutomatic
|
|
38
|
+
? { title: report ? `mind · ${report.target} · ${report.summary}` : cardTitle(data.title), state: report?.attention ? "attention" as const : "neutral" as const, full: data.content, preview: () => ({ text: "", truncated: true }) }
|
|
39
|
+
: cardFramePresentation(cardTitle(data.title), data.content, manualNoOp ? "One selected window reviewed; no consolidation proposed" : undefined);
|
|
40
|
+
return cardUI.renderCard(presentation, {
|
|
41
|
+
expanded,
|
|
42
|
+
theme,
|
|
43
|
+
outputPad: cardUI.outputPad(),
|
|
44
|
+
detailHint: cardFrameDetailHint(keyHint),
|
|
45
|
+
expansionKey: entry,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
package/src/ui/presentation.ts
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Human-facing transcript chrome. These helpers never change the model-visible tool payload;
|
|
3
|
-
* they only bound what a collapsed Pi card paints.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { sliceByColumn, stripTerminalSequences, Text, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
|
-
|
|
8
|
-
export const MAX_COLLAPSED_CARD_LINES = 3;
|
|
9
|
-
export const MAX_COLLAPSED_CARD_CHARS = 100;
|
|
10
|
-
const C0_AND_DEL = /[\u0000-\u0008\u000B-\u001F\u007F]/g;
|
|
11
|
-
|
|
12
|
-
export interface CompactVisibleTextOptions {
|
|
13
|
-
maxLines?: number;
|
|
14
|
-
/** Bound is terminal display columns, matching pi-persona's card chrome. */
|
|
15
|
-
maxLineChars?: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface CompactVisibleTextResult {
|
|
19
|
-
text: string;
|
|
20
|
-
truncated: boolean;
|
|
21
|
-
omittedLines: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface CardTheme {
|
|
25
|
-
fg: (name: "accent" | "toolOutput" | "dim", text: string) => string;
|
|
26
|
-
bold: (text: string) => string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function sanitizeCardText(text: string): string {
|
|
30
|
-
return stripTerminalSequences(text.replace(/\r\n?/g, "\n")).replace(C0_AND_DEL, "");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Bound a collapsed card: a few semantic lines, each clipped by column, then a drill-down marker. */
|
|
34
|
-
export function compactVisibleText(input: string, opts: CompactVisibleTextOptions = {}): CompactVisibleTextResult {
|
|
35
|
-
const maxLines = Math.max(1, Math.floor(opts.maxLines ?? MAX_COLLAPSED_CARD_LINES));
|
|
36
|
-
const requestedColumns = opts.maxLineChars ?? MAX_COLLAPSED_CARD_CHARS;
|
|
37
|
-
const maxLineColumns = Number.isFinite(requestedColumns) ? Math.max(1, Math.floor(requestedColumns)) : MAX_COLLAPSED_CARD_CHARS;
|
|
38
|
-
const source = sanitizeCardText(input)
|
|
39
|
-
.split("\n")
|
|
40
|
-
.map((line) => line.trim())
|
|
41
|
-
.filter((line) => line.length > 0);
|
|
42
|
-
|
|
43
|
-
let lineTruncated = false;
|
|
44
|
-
const clamp = (line: string): string => {
|
|
45
|
-
if (visibleWidth(line) <= maxLineColumns) return line;
|
|
46
|
-
lineTruncated = true;
|
|
47
|
-
return `${sliceByColumn(line, 0, maxLineColumns - visibleWidth("…"), true)}…`;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
if (source.length > maxLines) {
|
|
51
|
-
const contentLines = Math.max(0, maxLines - 1);
|
|
52
|
-
const omittedLines = source.length - contentLines;
|
|
53
|
-
const visible = source.slice(0, contentLines).map(clamp);
|
|
54
|
-
visible.push(clamp(`… +${omittedLines} more line${omittedLines === 1 ? "" : "s"}`));
|
|
55
|
-
return { text: visible.join("\n") || "(no output)", truncated: true, omittedLines };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const visible = source.map(clamp);
|
|
59
|
-
return {
|
|
60
|
-
text: visible.join("\n") || "(no output)",
|
|
61
|
-
truncated: lineTruncated,
|
|
62
|
-
omittedLines: 0,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Optional expansion hint supplied by Pi's active renderer context. */
|
|
67
|
-
export type DetailHint = () => string;
|
|
68
|
-
|
|
69
|
-
function formatExpandableBody(body: string, expanded: boolean, theme: CardTheme, detailHint?: DetailHint, summary?: string): string {
|
|
70
|
-
const full = sanitizeCardText(body).trim() || "(no output)";
|
|
71
|
-
if (expanded) return theme.fg("toolOutput", full);
|
|
72
|
-
const compact = summary === undefined ? full : sanitizeCardText(summary).trim();
|
|
73
|
-
const preview = compactVisibleText(compact);
|
|
74
|
-
const hidden = preview.truncated || compact !== full;
|
|
75
|
-
const hint = hidden && detailHint ? `\n${theme.fg("dim", sanitizeCardText(detailHint()))}` : "";
|
|
76
|
-
return `${theme.fg("toolOutput", preview.text)}${hint}`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Result body only: Pi owns the title/shell. An optional compact summary never replaces expanded evidence. */
|
|
80
|
-
export function renderExpandableResult(body: string, expanded: boolean, theme: CardTheme, detailHint?: DetailHint, summary?: string): Text {
|
|
81
|
-
return new Text(formatExpandableBody(body, expanded, theme, detailHint, summary), 0, 0);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function toolResultText(result: { content?: ReadonlyArray<{ type: string; text?: string }> }): string {
|
|
85
|
-
const first = result.content?.find((part) => part.type === "text");
|
|
86
|
-
return first?.text ?? "";
|
|
87
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Human-facing transcript chrome. These helpers never change the model-visible tool payload;
|
|
3
|
+
* they only bound what a collapsed Pi card paints.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { sliceByColumn, stripTerminalSequences, Text, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
|
+
|
|
8
|
+
export const MAX_COLLAPSED_CARD_LINES = 3;
|
|
9
|
+
export const MAX_COLLAPSED_CARD_CHARS = 100;
|
|
10
|
+
const C0_AND_DEL = /[\u0000-\u0008\u000B-\u001F\u007F]/g;
|
|
11
|
+
|
|
12
|
+
export interface CompactVisibleTextOptions {
|
|
13
|
+
maxLines?: number;
|
|
14
|
+
/** Bound is terminal display columns, matching pi-persona's card chrome. */
|
|
15
|
+
maxLineChars?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CompactVisibleTextResult {
|
|
19
|
+
text: string;
|
|
20
|
+
truncated: boolean;
|
|
21
|
+
omittedLines: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CardTheme {
|
|
25
|
+
fg: (name: "accent" | "toolOutput" | "dim", text: string) => string;
|
|
26
|
+
bold: (text: string) => string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function sanitizeCardText(text: string): string {
|
|
30
|
+
return stripTerminalSequences(text.replace(/\r\n?/g, "\n")).replace(C0_AND_DEL, "");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Bound a collapsed card: a few semantic lines, each clipped by column, then a drill-down marker. */
|
|
34
|
+
export function compactVisibleText(input: string, opts: CompactVisibleTextOptions = {}): CompactVisibleTextResult {
|
|
35
|
+
const maxLines = Math.max(1, Math.floor(opts.maxLines ?? MAX_COLLAPSED_CARD_LINES));
|
|
36
|
+
const requestedColumns = opts.maxLineChars ?? MAX_COLLAPSED_CARD_CHARS;
|
|
37
|
+
const maxLineColumns = Number.isFinite(requestedColumns) ? Math.max(1, Math.floor(requestedColumns)) : MAX_COLLAPSED_CARD_CHARS;
|
|
38
|
+
const source = sanitizeCardText(input)
|
|
39
|
+
.split("\n")
|
|
40
|
+
.map((line) => line.trim())
|
|
41
|
+
.filter((line) => line.length > 0);
|
|
42
|
+
|
|
43
|
+
let lineTruncated = false;
|
|
44
|
+
const clamp = (line: string): string => {
|
|
45
|
+
if (visibleWidth(line) <= maxLineColumns) return line;
|
|
46
|
+
lineTruncated = true;
|
|
47
|
+
return `${sliceByColumn(line, 0, maxLineColumns - visibleWidth("…"), true)}…`;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (source.length > maxLines) {
|
|
51
|
+
const contentLines = Math.max(0, maxLines - 1);
|
|
52
|
+
const omittedLines = source.length - contentLines;
|
|
53
|
+
const visible = source.slice(0, contentLines).map(clamp);
|
|
54
|
+
visible.push(clamp(`… +${omittedLines} more line${omittedLines === 1 ? "" : "s"}`));
|
|
55
|
+
return { text: visible.join("\n") || "(no output)", truncated: true, omittedLines };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const visible = source.map(clamp);
|
|
59
|
+
return {
|
|
60
|
+
text: visible.join("\n") || "(no output)",
|
|
61
|
+
truncated: lineTruncated,
|
|
62
|
+
omittedLines: 0,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Optional expansion hint supplied by Pi's active renderer context. */
|
|
67
|
+
export type DetailHint = () => string;
|
|
68
|
+
|
|
69
|
+
function formatExpandableBody(body: string, expanded: boolean, theme: CardTheme, detailHint?: DetailHint, summary?: string): string {
|
|
70
|
+
const full = sanitizeCardText(body).trim() || "(no output)";
|
|
71
|
+
if (expanded) return theme.fg("toolOutput", full);
|
|
72
|
+
const compact = summary === undefined ? full : sanitizeCardText(summary).trim();
|
|
73
|
+
const preview = compactVisibleText(compact);
|
|
74
|
+
const hidden = preview.truncated || compact !== full;
|
|
75
|
+
const hint = hidden && detailHint ? `\n${theme.fg("dim", sanitizeCardText(detailHint()))}` : "";
|
|
76
|
+
return `${theme.fg("toolOutput", preview.text)}${hint}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Result body only: Pi owns the title/shell. An optional compact summary never replaces expanded evidence. */
|
|
80
|
+
export function renderExpandableResult(body: string, expanded: boolean, theme: CardTheme, detailHint?: DetailHint, summary?: string): Text {
|
|
81
|
+
return new Text(formatExpandableBody(body, expanded, theme, detailHint, summary), 0, 0);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function toolResultText(result: { content?: ReadonlyArray<{ type: string; text?: string }> }): string {
|
|
85
|
+
const first = result.content?.find((part) => part.type === "text");
|
|
86
|
+
return first?.text ?? "";
|
|
87
|
+
}
|
|
@@ -1,56 +1,58 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
import { MAX_SESSION_NOTE_CHARS, SESSION_MEMORY_KINDS, SESSION_NOTE_ENTRY_TYPE, MAX_SESSION_SOURCE_CHARS } from "../core/session-memory.ts";
|
|
4
|
-
import { cardFrameDetailHint, cardFramePresentation, type MindCardUI } from "./card-frame.ts";
|
|
5
|
-
|
|
6
|
-
interface SessionNoteCardModel {
|
|
7
|
-
title: string;
|
|
8
|
-
body: string;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (event
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
typeof note.
|
|
26
|
-
typeof note.
|
|
27
|
-
typeof note.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
`
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { MAX_SESSION_NOTE_CHARS, SESSION_MEMORY_KINDS, SESSION_NOTE_ENTRY_TYPE, MAX_SESSION_SOURCE_CHARS } from "../core/session-memory.ts";
|
|
4
|
+
import { cardFrameDetailHint, cardFramePresentation, type MindCardUI } from "./card-frame.ts";
|
|
5
|
+
|
|
6
|
+
interface SessionNoteCardModel {
|
|
7
|
+
title: string;
|
|
8
|
+
body: string;
|
|
9
|
+
summary?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
13
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sessionNoteCardModel(value: unknown): SessionNoteCardModel | undefined {
|
|
17
|
+
const event = record(value);
|
|
18
|
+
if (event?.version !== 1) return undefined;
|
|
19
|
+
if (event.op === "forget" && typeof event.id === "string" && event.id.startsWith("session:")) {
|
|
20
|
+
return { title: "session note retired", body: `${event.id}\nNo longer active on this branch.` };
|
|
21
|
+
}
|
|
22
|
+
if (event.op !== "save") return undefined;
|
|
23
|
+
const note = record(event.note);
|
|
24
|
+
if (!note || typeof note.id !== "string" || !note.id.startsWith("session:") ||
|
|
25
|
+
typeof note.kind !== "string" || !(SESSION_MEMORY_KINDS as readonly string[]).includes(note.kind) ||
|
|
26
|
+
typeof note.text !== "string" || note.text.length === 0 || note.text.length > MAX_SESSION_NOTE_CHARS ||
|
|
27
|
+
typeof note.recordedAt !== "string" || !Number.isFinite(Date.parse(note.recordedAt)) ||
|
|
28
|
+
typeof note.originSessionId !== "string" || note.originSessionId.length === 0 || note.originSessionId.length > 256 || /[\s\u0000-\u001f\u007f]/u.test(note.originSessionId) ||
|
|
29
|
+
(note.source !== undefined && (typeof note.source !== "string" || note.source.length > MAX_SESSION_SOURCE_CHARS))) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
title: `session note · ${note.kind}`,
|
|
34
|
+
summary: note.text,
|
|
35
|
+
body: [
|
|
36
|
+
`id: ${note.id}`,
|
|
37
|
+
`recorded: ${note.recordedAt}`,
|
|
38
|
+
`origin session: ${note.originSessionId} (provenance only; not auto-resolved)`,
|
|
39
|
+
note.source === undefined ? "" : `source: ${note.source}`,
|
|
40
|
+
"",
|
|
41
|
+
note.text,
|
|
42
|
+
].filter((line) => line !== "").join("\n"),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Register a semantic standalone card for session-note save and forget events. */
|
|
47
|
+
export function registerSessionNoteRenderer(pi: Pick<ExtensionAPI, "registerEntryRenderer">, cardUI: MindCardUI): void {
|
|
48
|
+
pi.registerEntryRenderer<unknown>(SESSION_NOTE_ENTRY_TYPE, (entry, { expanded, keyHint }, theme) => {
|
|
49
|
+
const model = sessionNoteCardModel(entry.data);
|
|
50
|
+
return model ? cardUI.renderCard(cardFramePresentation(model.title, model.body, model.summary), {
|
|
51
|
+
expanded,
|
|
52
|
+
theme,
|
|
53
|
+
outputPad: cardUI.outputPad(),
|
|
54
|
+
detailHint: cardFrameDetailHint(keyHint),
|
|
55
|
+
expansionKey: entry,
|
|
56
|
+
}) : undefined;
|
|
57
|
+
});
|
|
58
|
+
}
|