pi-context-map 0.1.4 → 0.3.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/CHANGELOG.md +19 -22
- package/{src → extensions}/analyzer.ts +78 -50
- package/{src → extensions}/generator.ts +100 -49
- package/extensions/index.ts +89 -0
- package/extensions/insights.ts +114 -0
- package/extensions/token-counter.ts +149 -0
- package/extensions/types/pi-coding-agent.d.ts +3 -0
- package/package.json +21 -17
- package/dist/analyzer.d.ts +0 -40
- package/dist/analyzer.js +0 -117
- package/dist/generator.d.ts +0 -11
- package/dist/generator.js +0 -258
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -50
- package/docs/proposal.md +0 -41
- package/docs/spec.md +0 -57
- package/src/index.ts +0 -67
- package/src/types/pi-coding-agent.d.ts +0 -31
- package/tasks.md +0 -32
- package/tsconfig.json +0 -20
- /package/{src → extensions}/types/pi-ai.d.ts +0 -0
package/dist/index.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* pi-context-map
|
|
4
|
-
* Pi extension to visualize session context window and token distribution.
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.default = default_1;
|
|
8
|
-
const analyzer_1 = require("./analyzer");
|
|
9
|
-
const generator_1 = require("./generator");
|
|
10
|
-
function default_1(pi) {
|
|
11
|
-
const analyzer = new analyzer_1.ContextAnalyzer();
|
|
12
|
-
// Register the /context-map command
|
|
13
|
-
pi.registerCommand("context-map", {
|
|
14
|
-
description: "Generate a visual map of the current session context window.",
|
|
15
|
-
handler: (_args, ctx) => {
|
|
16
|
-
ctx.ui.notify("Analyzing session context...", "info");
|
|
17
|
-
try {
|
|
18
|
-
// 1. Extract messages and current turn
|
|
19
|
-
// Note: We assume ctx.session.messages is available.
|
|
20
|
-
// If not, we may need to fetch them via another API or use provided event data.
|
|
21
|
-
const messages = ctx.session.messages || [];
|
|
22
|
-
const currentTurn = messages.length;
|
|
23
|
-
if (messages.length === 0) {
|
|
24
|
-
ctx.ui.notify("No session history found to map.", "warning");
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
// 2. Analyze context
|
|
28
|
-
const map = analyzer.analyze(messages, currentTurn);
|
|
29
|
-
// 3. Generate HTML Report
|
|
30
|
-
const html = generator_1.ReportGenerator.generateHTML(map);
|
|
31
|
-
const reportPath = generator_1.ReportGenerator.writeReport(html);
|
|
32
|
-
ctx.ui.notify(`Context map generated successfully! \nPath: ${reportPath}`, "success");
|
|
33
|
-
// Providing a link or instruction to open the report
|
|
34
|
-
ctx.ui.notify("You can open the report.html in your browser to see the visualization.", "info");
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
38
|
-
ctx.ui.notify(`Failed to generate context map: ${message}`, "error");
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
// Optional: Notify the user when a significant amount of context is loaded
|
|
43
|
-
pi.on("session_before_compact", (event, ctx) => {
|
|
44
|
-
const { preparation } = event;
|
|
45
|
-
const tokens = preparation.tokensBefore;
|
|
46
|
-
if (tokens > 100_000) {
|
|
47
|
-
ctx.ui.notify(`High context load detected (${(tokens / 1000).toFixed(1)}k tokens). Try /context-map to see what's consuming space.`, "info");
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
package/docs/proposal.md
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# Proposal: pi-context-map
|
|
2
|
-
|
|
3
|
-
## 1. Problem Statement
|
|
4
|
-
As Pi sessions grow in complexity, the "Context Window" becomes a black box. Users and agents often lose track of:
|
|
5
|
-
- Which files are currently consuming the most tokens.
|
|
6
|
-
- When a file was last "refreshed" (read) by the agent.
|
|
7
|
-
- The distribution of tokens between history, system prompts, and tool outputs.
|
|
8
|
-
|
|
9
|
-
This leads to "context bloat," where the agent becomes sluggish or forgets critical instructions because the window is filled with stale file content.
|
|
10
|
-
|
|
11
|
-
## 2. Goal
|
|
12
|
-
Create a Pi extension that provides a **real-time, visual map of the current session's context**. It should transform the abstract concept of a "token window" into a concrete, actionable dashboard.
|
|
13
|
-
|
|
14
|
-
## 3. Core Features
|
|
15
|
-
### A. `/context-map` Command
|
|
16
|
-
A command that generates a visual report of the current context.
|
|
17
|
-
|
|
18
|
-
### B. Context Analysis
|
|
19
|
-
- **File Inventory**: List all files currently in context.
|
|
20
|
-
- **Weight Tracking**: Approximate token count per file.
|
|
21
|
-
- **Status Mapping**:
|
|
22
|
-
- `Active`: Read/Modified in the last 3 turns.
|
|
23
|
-
- `Stale`: Read 4-10 turns ago.
|
|
24
|
-
- `Legacy`: Read >10 turns ago (candidate for compaction).
|
|
25
|
-
- **Operation History**: Mark if a file was Read 🟢, Written 🟠, or Edited 🟡.
|
|
26
|
-
|
|
27
|
-
### C. Visual Output
|
|
28
|
-
The extension will generate an `index.html` report (stored in `.pi/context-map/report.html`) featuring:
|
|
29
|
-
- **Token Budget Bar**: Visual breakdown of context usage.
|
|
30
|
-
- **File Treemap/List**: Files sized by their token weight.
|
|
31
|
-
- **Temporal Timeline**: A simple timeline showing when files entered the context.
|
|
32
|
-
|
|
33
|
-
## 4. Success Criteria
|
|
34
|
-
- The user can run `/context-map` and immediately see which file is the "token hog."
|
|
35
|
-
- The user can identify "stale" files that can be removed to free up space.
|
|
36
|
-
- Zero performance degradation during normal session operation.
|
|
37
|
-
- Full compatibility with `pi-ultra-compact` (since both manage context).
|
|
38
|
-
|
|
39
|
-
## 5. Non-Goals
|
|
40
|
-
- This is a *visualization* tool, not an *automatic* context cleaner (though it provides the data needed for a user to trigger compaction).
|
|
41
|
-
- It will not modify the actual LLM context window, only report on it.
|
package/docs/spec.md
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# Technical Specification: pi-context-map
|
|
2
|
-
|
|
3
|
-
## 1. Architecture Overview
|
|
4
|
-
`pi-context-map` is a Pi extension that analyzes the session message history to derive a "map" of the active context. It operates as a read-only analysis tool triggered by a user command.
|
|
5
|
-
|
|
6
|
-
## 2. Data Extraction Logic
|
|
7
|
-
When `/context-map` is called, the extension will:
|
|
8
|
-
1. **Scan Messages**: Iterate through all messages in the current session history.
|
|
9
|
-
2. **Identify File Ops**:
|
|
10
|
-
- Scan `tool_use` blocks for `read`, `write`, `edit`, and `bash` (regex for file paths).
|
|
11
|
-
- Map each file to its most recent occurrence (turn number).
|
|
12
|
-
3. **Calculate Weights**:
|
|
13
|
-
- For each file found, retrieve its content length from the corresponding `tool_result` if available.
|
|
14
|
-
- Estimate tokens using a heuristic: $\text{tokens} \approx \text{chars} / 4$.
|
|
15
|
-
4. **Assign Status**:
|
|
16
|
-
- **Active**: Turn difference $\le 3$.
|
|
17
|
-
- **Stale**: $3 <$ Turn difference $\le 10$.
|
|
18
|
-
- **Legacy**: Turn difference $> 10$.
|
|
19
|
-
|
|
20
|
-
## 3. Component Design
|
|
21
|
-
|
|
22
|
-
### A. `ContextAnalyzer` (Class)
|
|
23
|
-
- `analyze(messages: Message[])`: Returns a `ContextMap` object.
|
|
24
|
-
- `calculateTokens(text: string)`: Returns estimated token count.
|
|
25
|
-
- `getFileMetadata(path: string)`: Tracks the operation type and timestamp.
|
|
26
|
-
|
|
27
|
-
### B. `ReportGenerator` (Class)
|
|
28
|
-
- `generateHTML(map: ContextMap)`: Produces a standalone HTML string.
|
|
29
|
-
- `writeReport(html: string)`: Saves the report to `.pi/context-map/report.html` and opens it.
|
|
30
|
-
|
|
31
|
-
### C. `ExtensionEntry` (Main)
|
|
32
|
-
- `pi.registerCommand("context-map", ...)`: The entry point.
|
|
33
|
-
- `pi.on("session_before_compact", ...)`: (Optional) Could trigger a map update before compaction.
|
|
34
|
-
|
|
35
|
-
## 4. Visual Specification (HTML Dashboard)
|
|
36
|
-
The report will be a single-file HTML dashboard with:
|
|
37
|
-
- **Header**: Session ID, Total Estimated Tokens, and Timestamp.
|
|
38
|
-
- **Context Budget**: A CSS-based stacked bar showing:
|
|
39
|
-
- `[ System ] [ History ] [ Files ] [ Tool Outputs ]`
|
|
40
|
-
- **File Grid**:
|
|
41
|
-
- Cards for each file.
|
|
42
|
-
- Size proportional to token weight.
|
|
43
|
-
- Color-coded by status (Green $\to$ Yellow $\to$ Red).
|
|
44
|
-
- Icons for operation type (👁️ for read, ✍️ for edit).
|
|
45
|
-
- **Stats Table**:
|
|
46
|
-
- File Path | Tokens | Last Turn | Status.
|
|
47
|
-
|
|
48
|
-
## 5. Implementation Details
|
|
49
|
-
- **Language**: TypeScript.
|
|
50
|
-
- **Dependencies**: `@earendil-works/pi-coding-agent`, `node:fs`, `node:path`.
|
|
51
|
-
- **Complexity**:
|
|
52
|
-
- Time: $O(N)$ where $N$ is number of messages.
|
|
53
|
-
- Space: $O(F)$ where $F$ is number of unique files in context.
|
|
54
|
-
|
|
55
|
-
## 6. Error Handling
|
|
56
|
-
- **Missing Tool Results**: If a file was read but the result is missing (e.g., due to previous compaction), mark weight as "Unknown" and status as "Stale".
|
|
57
|
-
- **Large Repos**: Limit the map to the top 100 largest files to prevent the HTML report from crashing the browser.
|
package/src/index.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* pi-context-map
|
|
3
|
-
* Pi extension to visualize session context window and token distribution.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import { ContextAnalyzer } from "./analyzer";
|
|
8
|
-
import { ReportGenerator } from "./generator";
|
|
9
|
-
|
|
10
|
-
export default function (pi: ExtensionAPI) {
|
|
11
|
-
const analyzer = new ContextAnalyzer();
|
|
12
|
-
|
|
13
|
-
// Register the /context-map command
|
|
14
|
-
pi.registerCommand("context-map", {
|
|
15
|
-
description: "Generate a visual map of the current session context window.",
|
|
16
|
-
handler: (_args, ctx) => {
|
|
17
|
-
ctx.ui.notify("Analyzing session context...", "info");
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
// 1. Extract messages and current turn
|
|
21
|
-
// Note: We assume ctx.session.messages is available.
|
|
22
|
-
// If not, we may need to fetch them via another API or use provided event data.
|
|
23
|
-
const messages = ctx.session.messages || [];
|
|
24
|
-
const currentTurn = messages.length;
|
|
25
|
-
|
|
26
|
-
if (messages.length === 0) {
|
|
27
|
-
ctx.ui.notify("No session history found to map.", "warning");
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 2. Analyze context
|
|
32
|
-
const map = analyzer.analyze(messages, currentTurn);
|
|
33
|
-
|
|
34
|
-
// 3. Generate HTML Report
|
|
35
|
-
const html = ReportGenerator.generateHTML(map);
|
|
36
|
-
const reportPath = ReportGenerator.writeReport(html);
|
|
37
|
-
|
|
38
|
-
ctx.ui.notify(
|
|
39
|
-
`Context map generated successfully! \nPath: ${reportPath}`,
|
|
40
|
-
"success",
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
// Providing a link or instruction to open the report
|
|
44
|
-
ctx.ui.notify(
|
|
45
|
-
"You can open the report.html in your browser to see the visualization.",
|
|
46
|
-
"info",
|
|
47
|
-
);
|
|
48
|
-
} catch (error) {
|
|
49
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
50
|
-
ctx.ui.notify(`Failed to generate context map: ${message}`, "error");
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
// Optional: Notify the user when a significant amount of context is loaded
|
|
56
|
-
pi.on("session_before_compact", (event, ctx) => {
|
|
57
|
-
const { preparation } = event;
|
|
58
|
-
const tokens = preparation.tokensBefore;
|
|
59
|
-
|
|
60
|
-
if (tokens > 100_000) {
|
|
61
|
-
ctx.ui.notify(
|
|
62
|
-
`High context load detected (${(tokens / 1000).toFixed(1)}k tokens). Try /context-map to see what's consuming space.`,
|
|
63
|
-
"info",
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
declare module "@earendil-works/pi-coding-agent" {
|
|
2
|
-
export interface ExtensionAPI {
|
|
3
|
-
registerCommand(
|
|
4
|
-
name: string,
|
|
5
|
-
options: {
|
|
6
|
-
description: string;
|
|
7
|
-
handler: (
|
|
8
|
-
args: string | undefined,
|
|
9
|
-
ctx: ExtensionContext,
|
|
10
|
-
) => Promise<void> | void;
|
|
11
|
-
},
|
|
12
|
-
): void;
|
|
13
|
-
on(
|
|
14
|
-
event: string,
|
|
15
|
-
handler: (event: any, ctx: ExtensionContext) => Promise<void> | void,
|
|
16
|
-
): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface ExtensionContext {
|
|
20
|
-
ui: {
|
|
21
|
-
notify(
|
|
22
|
-
message: string,
|
|
23
|
-
level: "info" | "success" | "warning" | "error",
|
|
24
|
-
): void;
|
|
25
|
-
};
|
|
26
|
-
session: {
|
|
27
|
-
messages: any[];
|
|
28
|
-
};
|
|
29
|
-
modelRegistry: any;
|
|
30
|
-
}
|
|
31
|
-
}
|
package/tasks.md
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# Implementation Tasks: pi-context-map
|
|
2
|
-
|
|
3
|
-
## Phase 1: Foundation
|
|
4
|
-
- [ ] Initialize `package.json` and TypeScript config.
|
|
5
|
-
- [ ] Create project folder structure (`src/`, `docs/`).
|
|
6
|
-
|
|
7
|
-
## Phase 2: Core Logic (`ContextAnalyzer`)
|
|
8
|
-
- [ ] Implement message scanning logic to find file operations.
|
|
9
|
-
- [ ] Implement token estimation heuristic.
|
|
10
|
-
- [ ] Implement status assignment (Active/Stale/Legacy).
|
|
11
|
-
- [ ] Create unit tests for analyzer logic.
|
|
12
|
-
|
|
13
|
-
## Phase 3: Visualization (`ReportGenerator`)
|
|
14
|
-
- [ ] Design the HTML dashboard template.
|
|
15
|
-
- [ ] Implement data-to-HTML mapping.
|
|
16
|
-
- [ ] Implement file writing to `.pi/context-map/report.html`.
|
|
17
|
-
|
|
18
|
-
## Phase 4: Pi Integration
|
|
19
|
-
- [ ] Register `/context-map` command.
|
|
20
|
-
- [ ] Implement command handler that triggers analysis $\to$ report $\to$ notification.
|
|
21
|
-
- [ ] Add "Open Report" link in the Pi notification.
|
|
22
|
-
|
|
23
|
-
## Phase 5: QA & Polishing
|
|
24
|
-
- [ ] Test with high-token sessions.
|
|
25
|
-
- [ ] Verify accuracy of token weights.
|
|
26
|
-
- [ ] Run Pi Lens diagnostics to ensure zero blockers.
|
|
27
|
-
- [ ] Polish HTML CSS for "Architect" look.
|
|
28
|
-
|
|
29
|
-
## Phase 6: Release
|
|
30
|
-
- [ ] Update README.md.
|
|
31
|
-
- [ ] Publish to npm.
|
|
32
|
-
- [ ] Final GitHub release.
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"baseUrl": ".",
|
|
14
|
-
"paths": {
|
|
15
|
-
"*": ["src/types/*"]
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"include": ["src/**/*"],
|
|
19
|
-
"typeRoots": ["./node_modules/@types", "src/types"]
|
|
20
|
-
}
|
|
File without changes
|