pi-context-kit 0.4.1 → 0.4.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/CHANGELOG.md +19 -7
- package/LICENSE +1 -1
- package/README.md +110 -110
- package/context-kit.schema.json +4 -1
- package/extensions/index.ts +87 -157
- package/package.json +37 -45
- package/src/background.ts +2 -3
- package/src/config.ts +12 -41
- package/src/context.ts +43 -16
- package/src/files.ts +6 -13
- package/src/handoff.ts +3 -7
- package/src/maintain.ts +6 -19
- package/src/migrate.ts +6 -18
- package/src/note.ts +5 -6
- package/src/panel.ts +7 -21
- package/src/prune.ts +2 -1
- package/src/recall.ts +14 -37
- package/src/settings-file.ts +7 -20
- package/src/shake.ts +6 -12
- package/src/stale.ts +22 -26
- package/src/status.ts +2 -6
- package/src/tool-calls.ts +2 -3
package/src/settings-file.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
writeFileSync,
|
|
12
12
|
} from "node:fs";
|
|
13
13
|
import { dirname, join } from "node:path";
|
|
14
|
+
|
|
14
15
|
import { isRecord } from "./config.ts";
|
|
15
16
|
|
|
16
17
|
const FILE_NAME = "context-kit.json";
|
|
@@ -21,11 +22,7 @@ export interface SettingsPaths {
|
|
|
21
22
|
project: string | undefined;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export function settingsPaths(
|
|
25
|
-
agentDir: string,
|
|
26
|
-
cwd: string,
|
|
27
|
-
trusted: boolean,
|
|
28
|
-
): SettingsPaths {
|
|
25
|
+
export function settingsPaths(agentDir: string, cwd: string, trusted: boolean): SettingsPaths {
|
|
29
26
|
return {
|
|
30
27
|
global: join(agentDir, FILE_NAME),
|
|
31
28
|
project: trusted ? join(cwd, ".pi", FILE_NAME) : undefined,
|
|
@@ -67,10 +64,7 @@ export function updateSetting(path: string, key: string, value: unknown) {
|
|
|
67
64
|
}
|
|
68
65
|
|
|
69
66
|
/** Creates the file with this content. Returns false when the file exists, which it keeps. */
|
|
70
|
-
export function createSettings(
|
|
71
|
-
path: string,
|
|
72
|
-
content: Record<string, unknown>,
|
|
73
|
-
): boolean {
|
|
67
|
+
export function createSettings(path: string, content: Record<string, unknown>): boolean {
|
|
74
68
|
mkdirSync(dirname(path), { recursive: true });
|
|
75
69
|
const temporary = `${path}.${process.pid}.tmp`;
|
|
76
70
|
try {
|
|
@@ -91,11 +85,7 @@ export function createSettings(
|
|
|
91
85
|
* Writes the key where it takes effect: the project file when that file sets it, otherwise the
|
|
92
86
|
* global file. Returns the path written.
|
|
93
87
|
*/
|
|
94
|
-
export function saveSetting(
|
|
95
|
-
paths: SettingsPaths,
|
|
96
|
-
key: string,
|
|
97
|
-
value: unknown,
|
|
98
|
-
): string {
|
|
88
|
+
export function saveSetting(paths: SettingsPaths, key: string, value: unknown): string {
|
|
99
89
|
const path =
|
|
100
90
|
paths.project && readSettingsFile(paths.project)[key] !== undefined
|
|
101
91
|
? paths.project
|
|
@@ -111,13 +101,13 @@ export function readSettingsFile(path: string): Record<string, unknown> {
|
|
|
111
101
|
text = readFileSync(path, "utf8");
|
|
112
102
|
} catch (error) {
|
|
113
103
|
if (isRecord(error) && error.code === "ENOENT") return {};
|
|
114
|
-
throw new Error(`${path} cannot be read (${errorMessage(error)})
|
|
104
|
+
throw new Error(`${path} cannot be read (${errorMessage(error)})`, { cause: error });
|
|
115
105
|
}
|
|
116
106
|
let value: unknown;
|
|
117
107
|
try {
|
|
118
108
|
value = JSON.parse(text);
|
|
119
109
|
} catch (error) {
|
|
120
|
-
throw new Error(`${path} is not valid JSON (${errorMessage(error)})
|
|
110
|
+
throw new Error(`${path} is not valid JSON (${errorMessage(error)})`, { cause: error });
|
|
121
111
|
}
|
|
122
112
|
if (!isRecord(value)) throw new Error(`${path} must contain a JSON object`);
|
|
123
113
|
return value;
|
|
@@ -136,10 +126,7 @@ export function mergeSettings(
|
|
|
136
126
|
const current = result[key];
|
|
137
127
|
// A plain assignment of "__proto__" would set the prototype instead of a key.
|
|
138
128
|
Object.defineProperty(result, key, {
|
|
139
|
-
value:
|
|
140
|
-
isRecord(current) && isRecord(value)
|
|
141
|
-
? mergeSettings(current, value)
|
|
142
|
-
: value,
|
|
129
|
+
value: isRecord(current) && isRecord(value) ? mergeSettings(current, value) : value,
|
|
143
130
|
enumerable: true,
|
|
144
131
|
writable: true,
|
|
145
132
|
configurable: true,
|
package/src/shake.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
ContextEditableContent,
|
|
4
4
|
ProjectedSessionEntry,
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
6
7
|
import {
|
|
7
8
|
type Edit,
|
|
8
9
|
type EditableMessage,
|
|
@@ -29,10 +30,7 @@ const BLOCK =
|
|
|
29
30
|
* so every tool call keeps its result. Signatures stay valid, but Anthropic drops every thinking
|
|
30
31
|
* block after the first changed message.
|
|
31
32
|
*/
|
|
32
|
-
export function shake(
|
|
33
|
-
entries: readonly ProjectedSessionEntry[],
|
|
34
|
-
options: ShakeOptions,
|
|
35
|
-
): Edit[] {
|
|
33
|
+
export function shake(entries: readonly ProjectedSessionEntry[], options: ShakeOptions): Edit[] {
|
|
36
34
|
const edits: Edit[] = [];
|
|
37
35
|
let seen = 0;
|
|
38
36
|
let savings = 0;
|
|
@@ -49,9 +47,7 @@ export function shake(
|
|
|
49
47
|
savings += edit.saved;
|
|
50
48
|
edits.push(edit.edit);
|
|
51
49
|
}
|
|
52
|
-
return savings >= options.minSavings && edits.length > 0
|
|
53
|
-
? edits.reverse()
|
|
54
|
-
: [];
|
|
50
|
+
return savings >= options.minSavings && edits.length > 0 ? edits.toReversed() : [];
|
|
55
51
|
}
|
|
56
52
|
|
|
57
53
|
function shakeEdit(
|
|
@@ -63,8 +59,7 @@ function shakeEdit(
|
|
|
63
59
|
const targetId = entry.sourceEntry.id;
|
|
64
60
|
const content = shakeContent(message, targetId, options);
|
|
65
61
|
if (content === undefined) return undefined;
|
|
66
|
-
const saved =
|
|
67
|
-
messageTokens(message) - messageTokens(withContent(message, content));
|
|
62
|
+
const saved = messageTokens(message) - messageTokens(withContent(message, content));
|
|
68
63
|
return saved > 0 ? { edit: { targetId, content }, saved } : undefined;
|
|
69
64
|
}
|
|
70
65
|
|
|
@@ -73,8 +68,7 @@ function shakeContent(
|
|
|
73
68
|
id: string,
|
|
74
69
|
options: ShakeOptions,
|
|
75
70
|
): ContextEditableContent | undefined {
|
|
76
|
-
const text = (value: string) =>
|
|
77
|
-
elideBlocks(value, id, options.blockMinTokens);
|
|
71
|
+
const text = (value: string) => elideBlocks(value, id, options.blockMinTokens);
|
|
78
72
|
|
|
79
73
|
if (typeof message.content === "string") {
|
|
80
74
|
const next = text(message.content);
|
|
@@ -95,7 +89,7 @@ function shakeContent(
|
|
|
95
89
|
return block;
|
|
96
90
|
});
|
|
97
91
|
// Blocks keep their kind, and images only occur where text is also allowed.
|
|
98
|
-
return changed ?
|
|
92
|
+
return changed ? next : undefined;
|
|
99
93
|
}
|
|
100
94
|
|
|
101
95
|
function elideBlocks(text: string, id: string, minTokens: number): string {
|
package/src/stale.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
3
4
|
import { type Edit, messageTokens, withContent } from "./context.ts";
|
|
4
5
|
import { newestToolResults, type ToolResult } from "./tool-calls.ts";
|
|
5
6
|
|
|
@@ -44,19 +45,12 @@ export function supersededReads(
|
|
|
44
45
|
recordRead(seen, result, key);
|
|
45
46
|
newer.set(key.path, seen);
|
|
46
47
|
}
|
|
47
|
-
return edits.
|
|
48
|
+
return edits.toReversed();
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
function isSuperseded(
|
|
51
|
-
{ message }: ToolResult,
|
|
52
|
-
key: ReadKey,
|
|
53
|
-
seen: NewerReads,
|
|
54
|
-
): boolean {
|
|
51
|
+
function isSuperseded({ message }: ToolResult, key: ReadKey, seen: NewerReads): boolean {
|
|
55
52
|
if (message.isError) return seen.anyRead;
|
|
56
|
-
return (
|
|
57
|
-
seen.fullSuccess ||
|
|
58
|
-
(key.range !== undefined && seen.successRanges.has(key.range))
|
|
59
|
-
);
|
|
53
|
+
return seen.fullSuccess || (key.range !== undefined && seen.successRanges.has(key.range));
|
|
60
54
|
}
|
|
61
55
|
|
|
62
56
|
function supersededEdit({ id, message }: ToolResult): Edit | undefined {
|
|
@@ -66,11 +60,7 @@ function supersededEdit({ id, message }: ToolResult): Edit | undefined {
|
|
|
66
60
|
: undefined;
|
|
67
61
|
}
|
|
68
62
|
|
|
69
|
-
function recordRead(
|
|
70
|
-
seen: NewerReads,
|
|
71
|
-
{ message }: ToolResult,
|
|
72
|
-
key: ReadKey,
|
|
73
|
-
): void {
|
|
63
|
+
function recordRead(seen: NewerReads, { message }: ToolResult, key: ReadKey): void {
|
|
74
64
|
seen.anyRead = true;
|
|
75
65
|
if (message.isError || isTruncatedRead(message.content)) return;
|
|
76
66
|
if (key.range === undefined) seen.fullSuccess = true;
|
|
@@ -81,21 +71,27 @@ function isTruncatedRead(content: ToolResultMessage["content"]): boolean {
|
|
|
81
71
|
return content.some(
|
|
82
72
|
(block) =>
|
|
83
73
|
block.type === "text" &&
|
|
84
|
-
/\[(?:Showing lines |\d+ more lines in file|Line \d+ is )/.test(
|
|
85
|
-
block.text,
|
|
86
|
-
),
|
|
74
|
+
/\[(?:Showing lines |\d+ more lines in file|Line \d+ is )/.test(block.text),
|
|
87
75
|
);
|
|
88
76
|
}
|
|
89
77
|
|
|
90
|
-
function
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
function isRangePart(value: unknown): value is number | string | undefined {
|
|
79
|
+
return value === undefined || typeof value === "number" || typeof value === "string";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function readKey(args: Record<string, unknown> | undefined): ReadKey | undefined {
|
|
93
83
|
const path = args?.path;
|
|
94
84
|
if (typeof path !== "string" || path.length === 0) return undefined;
|
|
95
85
|
const { offset, limit } = args ?? {};
|
|
96
|
-
|
|
97
|
-
return
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
// A range that is not a number or string has no comparable key, so the read is never superseded.
|
|
87
|
+
if (!isRangePart(offset) || !isRangePart(limit)) return undefined;
|
|
88
|
+
return { path, range: rangeKey(offset, limit) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function rangeKey(
|
|
92
|
+
offset: number | string | undefined,
|
|
93
|
+
limit: number | string | undefined,
|
|
94
|
+
): string | undefined {
|
|
95
|
+
if (offset === undefined && limit === undefined) return undefined;
|
|
96
|
+
return `${String(offset ?? 1)}:${String(limit ?? "")}`;
|
|
101
97
|
}
|
package/src/status.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface StatusInput {
|
|
|
8
8
|
/** Where compaction starts, or +Infinity when nothing compacts. */
|
|
9
9
|
limit: number;
|
|
10
10
|
contextWindow: number;
|
|
11
|
-
note?: keyof typeof NOTE_LABEL;
|
|
11
|
+
note?: keyof typeof NOTE_LABEL | undefined;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/** The footer text: context use and threshold as shares of the window, then the background note. */
|
|
@@ -22,11 +22,7 @@ export function statusText(input: StatusInput): string {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/** The notice after context edits, which Pi does not show by itself. */
|
|
25
|
-
export function freedNotice(
|
|
26
|
-
before: number,
|
|
27
|
-
after: number,
|
|
28
|
-
contextWindow: number,
|
|
29
|
-
): string {
|
|
25
|
+
export function freedNotice(before: number, after: number, contextWindow: number): string {
|
|
30
26
|
const freed = before - after;
|
|
31
27
|
return `pi-context-kit freed ${percent(freed, contextWindow)} of the context (${tokenCount(freed)} tokens). The agent can still look up what was removed.`;
|
|
32
28
|
}
|
package/src/tool-calls.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ToolCall } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
3
4
|
import { type EditableMessage, editableMessage } from "./context.ts";
|
|
4
5
|
|
|
5
6
|
export interface ToolResult {
|
|
@@ -9,9 +10,7 @@ export interface ToolResult {
|
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
/** Tool calls in the projected context, by tool call ID. */
|
|
12
|
-
function toolCallsById(
|
|
13
|
-
entries: readonly ProjectedSessionEntry[],
|
|
14
|
-
): Map<string, ToolCall> {
|
|
13
|
+
function toolCallsById(entries: readonly ProjectedSessionEntry[]): Map<string, ToolCall> {
|
|
15
14
|
const calls = new Map<string, ToolCall>();
|
|
16
15
|
for (const entry of entries) {
|
|
17
16
|
for (const message of entry.messages) {
|