@pi-vault/pi-dcp 0.1.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 +16 -0
- package/LICENSE +21 -0
- package/README.md +86 -0
- package/package.json +62 -0
- package/src/commands/context.ts +25 -0
- package/src/commands/decompress.ts +21 -0
- package/src/commands/help.ts +14 -0
- package/src/commands/lifetime.ts +13 -0
- package/src/commands/manual.ts +21 -0
- package/src/commands/recompress.ts +22 -0
- package/src/commands/register.ts +79 -0
- package/src/commands/stats.ts +11 -0
- package/src/commands/sweep.ts +8 -0
- package/src/compress/handler.ts +148 -0
- package/src/compress/search.ts +62 -0
- package/src/compress/state.ts +140 -0
- package/src/config.ts +253 -0
- package/src/index.ts +217 -0
- package/src/logger.ts +49 -0
- package/src/messages/inject.ts +149 -0
- package/src/messages/priority.ts +75 -0
- package/src/messages/prune.ts +103 -0
- package/src/messages/strip.ts +23 -0
- package/src/messages/sync.ts +55 -0
- package/src/pipeline.ts +64 -0
- package/src/prompts/compress-message.ts +19 -0
- package/src/prompts/nudges.ts +44 -0
- package/src/prompts/system.ts +37 -0
- package/src/state/persistence.ts +111 -0
- package/src/state/state.ts +102 -0
- package/src/state/tool-cache.ts +87 -0
- package/src/state/types.ts +138 -0
- package/src/strategies/deduplication.ts +30 -0
- package/src/strategies/protected-patterns.ts +77 -0
- package/src/strategies/purge-errors.ts +18 -0
- package/src/strategies/runner.ts +149 -0
- package/src/utils/message-content.ts +85 -0
- package/src/utils/message-ids.ts +45 -0
- package/src/utils/tokens.ts +54 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export function formatMessageRef(index: number): string {
|
|
2
|
+
return `m${String(index).padStart(4, "0")}`;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function formatBlockRef(blockId: number): string {
|
|
6
|
+
return `b${blockId}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseMessageRef(ref: string): number | undefined {
|
|
10
|
+
const match = /^m(\d{4})$/.exec(ref);
|
|
11
|
+
if (!match) return undefined;
|
|
12
|
+
return parseInt(match[1], 10);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseBlockRef(ref: string): number | undefined {
|
|
16
|
+
const match = /^b(\d+)$/.exec(ref);
|
|
17
|
+
if (!match) return undefined;
|
|
18
|
+
const n = parseInt(match[1], 10);
|
|
19
|
+
if (n <= 0) return undefined;
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ParsedBoundaryId =
|
|
24
|
+
| { type: "message"; index: number }
|
|
25
|
+
| { type: "block"; blockId: number };
|
|
26
|
+
|
|
27
|
+
export function parseBoundaryId(id: string): ParsedBoundaryId | undefined {
|
|
28
|
+
const msgIndex = parseMessageRef(id);
|
|
29
|
+
if (msgIndex !== undefined) return { type: "message", index: msgIndex };
|
|
30
|
+
|
|
31
|
+
const blockId = parseBlockRef(id);
|
|
32
|
+
if (blockId !== undefined) return { type: "block", blockId };
|
|
33
|
+
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function formatMessageIdTag(
|
|
38
|
+
ref: string,
|
|
39
|
+
attrs?: { priority?: number },
|
|
40
|
+
): string {
|
|
41
|
+
if (attrs?.priority !== undefined) {
|
|
42
|
+
return `<dcp-message-id priority="${attrs.priority}">${ref}</dcp-message-id>`;
|
|
43
|
+
}
|
|
44
|
+
return `<dcp-message-id>${ref}</dcp-message-id>`;
|
|
45
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token counting using character-based estimation.
|
|
3
|
+
*
|
|
4
|
+
* Uses length/4 as a rough approximation. Pi's built-in ctx.getContextUsage()
|
|
5
|
+
* provides accurate context-level token counts for threshold decisions. These
|
|
6
|
+
* per-message estimates are for relative comparisons (compression savings,
|
|
7
|
+
* priority ranking).
|
|
8
|
+
*/
|
|
9
|
+
export function countTokens(text: string): number {
|
|
10
|
+
if (text.length === 0) return 0;
|
|
11
|
+
return Math.max(1, Math.round(text.length / 4));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function countTokensBatch(texts: string[]): number {
|
|
15
|
+
if (texts.length === 0) return 0;
|
|
16
|
+
return countTokens(texts.join(" "));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Extract all text content from a message-shaped object for token counting.
|
|
21
|
+
* Handles UserMessage (string | TextContent[]), AssistantMessage (TextContent +
|
|
22
|
+
* ToolCallContent), and ToolResultMessage.
|
|
23
|
+
*/
|
|
24
|
+
export function extractMessageText(message: {
|
|
25
|
+
role: string;
|
|
26
|
+
content?: unknown;
|
|
27
|
+
}): string {
|
|
28
|
+
const content = message.content;
|
|
29
|
+
if (!content) return "";
|
|
30
|
+
if (typeof content === "string") return content;
|
|
31
|
+
if (!Array.isArray(content)) return "";
|
|
32
|
+
|
|
33
|
+
const parts: string[] = [];
|
|
34
|
+
for (const part of content) {
|
|
35
|
+
if (typeof part !== "object" || part === null) continue;
|
|
36
|
+
const p = part as Record<string, unknown>;
|
|
37
|
+
if (p.type === "text" && typeof p.text === "string") {
|
|
38
|
+
parts.push(p.text);
|
|
39
|
+
} else if (p.type === "toolCall") {
|
|
40
|
+
if (typeof p.name === "string") parts.push(p.name);
|
|
41
|
+
if (p.arguments && typeof p.arguments === "object") {
|
|
42
|
+
parts.push(JSON.stringify(p.arguments));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return parts.join(" ");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function countMessageTokens(message: {
|
|
50
|
+
role: string;
|
|
51
|
+
content?: unknown;
|
|
52
|
+
}): number {
|
|
53
|
+
return countTokens(extractMessageText(message));
|
|
54
|
+
}
|