@tarquinen/opencode-dcp 3.2.4-beta0 → 3.2.6-beta0
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/dcp.schema.json +329 -0
- package/dist/lib/config.js +2 -2
- package/dist/lib/config.js.map +1 -1
- package/index.ts +141 -0
- package/lib/auth.ts +37 -0
- package/lib/commands/compression-targets.ts +137 -0
- package/lib/commands/context.ts +132 -0
- package/lib/commands/decompress.ts +275 -0
- package/lib/commands/help.ts +76 -0
- package/lib/commands/index.ts +11 -0
- package/lib/commands/manual.ts +125 -0
- package/lib/commands/recompress.ts +224 -0
- package/lib/commands/stats.ts +148 -0
- package/lib/commands/sweep.ts +268 -0
- package/lib/compress-permission.ts +25 -0
- package/lib/config.ts +2 -2
- package/lib/hooks.ts +378 -0
- package/lib/host-permissions.ts +101 -0
- package/lib/messages/index.ts +8 -0
- package/lib/messages/inject/inject.ts +215 -0
- package/lib/messages/inject/subagent-results.ts +82 -0
- package/lib/messages/inject/utils.ts +374 -0
- package/lib/messages/priority.ts +102 -0
- package/lib/messages/prune.ts +238 -0
- package/lib/messages/reasoning-strip.ts +40 -0
- package/lib/messages/sync.ts +124 -0
- package/lib/messages/utils.ts +187 -0
- package/lib/prompts/compress-message.ts +42 -0
- package/lib/prompts/compress-range.ts +60 -0
- package/lib/prompts/context-limit-nudge.ts +18 -0
- package/lib/prompts/extensions/nudge.ts +43 -0
- package/lib/prompts/extensions/system.ts +32 -0
- package/lib/prompts/extensions/tool.ts +35 -0
- package/lib/prompts/index.ts +29 -0
- package/lib/prompts/iteration-nudge.ts +6 -0
- package/lib/prompts/store.ts +467 -0
- package/lib/prompts/system.ts +33 -0
- package/lib/prompts/turn-nudge.ts +10 -0
- package/lib/protected-patterns.ts +128 -0
- package/lib/strategies/deduplication.ts +127 -0
- package/lib/strategies/index.ts +2 -0
- package/lib/strategies/purge-errors.ts +88 -0
- package/lib/subagents/subagent-results.ts +74 -0
- package/lib/ui/notification.ts +346 -0
- package/lib/ui/utils.ts +287 -0
- package/package.json +14 -19
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { CompressionBlock, PruneMessagesState } from "../state"
|
|
2
|
+
|
|
3
|
+
export interface CompressionTarget {
|
|
4
|
+
displayId: number
|
|
5
|
+
runId: number
|
|
6
|
+
topic: string
|
|
7
|
+
compressedTokens: number
|
|
8
|
+
durationMs: number
|
|
9
|
+
grouped: boolean
|
|
10
|
+
blocks: CompressionBlock[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function byBlockId(a: CompressionBlock, b: CompressionBlock): number {
|
|
14
|
+
return a.blockId - b.blockId
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function buildTarget(blocks: CompressionBlock[]): CompressionTarget {
|
|
18
|
+
const ordered = [...blocks].sort(byBlockId)
|
|
19
|
+
const first = ordered[0]
|
|
20
|
+
if (!first) {
|
|
21
|
+
throw new Error("Cannot build compression target from empty block list.")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const grouped = first.mode === "message"
|
|
25
|
+
return {
|
|
26
|
+
displayId: first.blockId,
|
|
27
|
+
runId: first.runId,
|
|
28
|
+
topic: grouped ? first.batchTopic || first.topic : first.topic,
|
|
29
|
+
compressedTokens: ordered.reduce((total, block) => total + block.compressedTokens, 0),
|
|
30
|
+
durationMs: ordered.reduce((total, block) => Math.max(total, block.durationMs), 0),
|
|
31
|
+
grouped,
|
|
32
|
+
blocks: ordered,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function groupMessageBlocks(blocks: CompressionBlock[]): CompressionTarget[] {
|
|
37
|
+
const grouped = new Map<number, CompressionBlock[]>()
|
|
38
|
+
|
|
39
|
+
for (const block of blocks) {
|
|
40
|
+
const existing = grouped.get(block.runId)
|
|
41
|
+
if (existing) {
|
|
42
|
+
existing.push(block)
|
|
43
|
+
continue
|
|
44
|
+
}
|
|
45
|
+
grouped.set(block.runId, [block])
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Array.from(grouped.values()).map(buildTarget)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function splitTargets(blocks: CompressionBlock[]): CompressionTarget[] {
|
|
52
|
+
const messageBlocks: CompressionBlock[] = []
|
|
53
|
+
const singleBlocks: CompressionBlock[] = []
|
|
54
|
+
|
|
55
|
+
for (const block of blocks) {
|
|
56
|
+
if (block.mode === "message") {
|
|
57
|
+
messageBlocks.push(block)
|
|
58
|
+
} else {
|
|
59
|
+
singleBlocks.push(block)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const targets = [
|
|
64
|
+
...singleBlocks.map((block) => buildTarget([block])),
|
|
65
|
+
...groupMessageBlocks(messageBlocks),
|
|
66
|
+
]
|
|
67
|
+
return targets.sort((a, b) => a.displayId - b.displayId)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function getActiveCompressionTargets(
|
|
71
|
+
messagesState: PruneMessagesState,
|
|
72
|
+
): CompressionTarget[] {
|
|
73
|
+
const activeBlocks = Array.from(messagesState.activeBlockIds)
|
|
74
|
+
.map((blockId) => messagesState.blocksById.get(blockId))
|
|
75
|
+
.filter((block): block is CompressionBlock => !!block && block.active)
|
|
76
|
+
|
|
77
|
+
return splitTargets(activeBlocks)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function getRecompressibleCompressionTargets(
|
|
81
|
+
messagesState: PruneMessagesState,
|
|
82
|
+
availableMessageIds: Set<string>,
|
|
83
|
+
): CompressionTarget[] {
|
|
84
|
+
const allBlocks = Array.from(messagesState.blocksById.values()).filter((block) => {
|
|
85
|
+
return availableMessageIds.has(block.compressMessageId)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const messageGroups = new Map<number, CompressionBlock[]>()
|
|
89
|
+
const singleTargets: CompressionTarget[] = []
|
|
90
|
+
|
|
91
|
+
for (const block of allBlocks) {
|
|
92
|
+
if (block.mode === "message") {
|
|
93
|
+
const existing = messageGroups.get(block.runId)
|
|
94
|
+
if (existing) {
|
|
95
|
+
existing.push(block)
|
|
96
|
+
} else {
|
|
97
|
+
messageGroups.set(block.runId, [block])
|
|
98
|
+
}
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (block.deactivatedByUser && !block.active) {
|
|
103
|
+
singleTargets.push(buildTarget([block]))
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const blocks of messageGroups.values()) {
|
|
108
|
+
if (blocks.some((block) => block.deactivatedByUser && !block.active)) {
|
|
109
|
+
singleTargets.push(buildTarget(blocks))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return singleTargets.sort((a, b) => a.displayId - b.displayId)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function resolveCompressionTarget(
|
|
117
|
+
messagesState: PruneMessagesState,
|
|
118
|
+
blockId: number,
|
|
119
|
+
): CompressionTarget | null {
|
|
120
|
+
const block = messagesState.blocksById.get(blockId)
|
|
121
|
+
if (!block) {
|
|
122
|
+
return null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (block.mode !== "message") {
|
|
126
|
+
return buildTarget([block])
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const blocks = Array.from(messagesState.blocksById.values()).filter(
|
|
130
|
+
(candidate) => candidate.mode === "message" && candidate.runId === block.runId,
|
|
131
|
+
)
|
|
132
|
+
if (blocks.length === 0) {
|
|
133
|
+
return null
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return buildTarget(blocks)
|
|
137
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DCP Context Command
|
|
3
|
+
* Shows a visual breakdown of token usage in the current session.
|
|
4
|
+
* Token calculation logic lives in ../analysis/tokens.ts
|
|
5
|
+
*
|
|
6
|
+
* TOKEN CALCULATION STRATEGY
|
|
7
|
+
* ==========================
|
|
8
|
+
* We minimize tokenizer estimation by leveraging API-reported values wherever possible.
|
|
9
|
+
*
|
|
10
|
+
* WHAT WE GET FROM THE API (exact):
|
|
11
|
+
* - tokens.input : Input tokens for each assistant response
|
|
12
|
+
* - tokens.output : Output tokens generated (includes text + tool calls)
|
|
13
|
+
* - tokens.reasoning: Reasoning tokens used
|
|
14
|
+
* - tokens.cache : Cache read/write tokens
|
|
15
|
+
*
|
|
16
|
+
* HOW WE CALCULATE EACH CATEGORY:
|
|
17
|
+
*
|
|
18
|
+
* SYSTEM = firstAssistant.input + cache.read + cache.write - tokenizer(firstUserMessage)
|
|
19
|
+
* The first response's total input (input + cache.read + cache.write)
|
|
20
|
+
* contains system + first user message. On the first request of a
|
|
21
|
+
* session, the system prompt appears in cache.write (cache creation),
|
|
22
|
+
* not cache.read.
|
|
23
|
+
*
|
|
24
|
+
* TOOLS = tokenizer(toolInputs + toolOutputs) - prunedTokens
|
|
25
|
+
* We must tokenize tools anyway for pruning decisions.
|
|
26
|
+
*
|
|
27
|
+
* USER = tokenizer(all user messages)
|
|
28
|
+
* User messages are typically small, so estimation is acceptable.
|
|
29
|
+
*
|
|
30
|
+
* ASSISTANT = total - system - user - tools
|
|
31
|
+
* Calculated as residual. This absorbs:
|
|
32
|
+
* - Assistant text output tokens
|
|
33
|
+
* - Reasoning tokens (if persisted by the model)
|
|
34
|
+
* - Any estimation errors
|
|
35
|
+
*
|
|
36
|
+
* TOTAL = input + output + reasoning + cache.read + cache.write
|
|
37
|
+
* Matches opencode's UI display.
|
|
38
|
+
*
|
|
39
|
+
* WHY ASSISTANT IS THE RESIDUAL:
|
|
40
|
+
* If reasoning tokens persist in context (model-dependent), they semantically
|
|
41
|
+
* belong with "Assistant" since reasoning IS assistant-generated content.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import type { Logger } from "../logger"
|
|
45
|
+
import type { SessionState, WithParts } from "../state"
|
|
46
|
+
import { sendIgnoredMessage } from "../ui/notification"
|
|
47
|
+
import { formatTokenCount } from "../ui/utils"
|
|
48
|
+
import { getCurrentParams } from "../token-utils"
|
|
49
|
+
import { analyzeTokens, type TokenBreakdown } from "../analysis/tokens"
|
|
50
|
+
|
|
51
|
+
export interface ContextCommandContext {
|
|
52
|
+
client: any
|
|
53
|
+
state: SessionState
|
|
54
|
+
logger: Logger
|
|
55
|
+
sessionId: string
|
|
56
|
+
messages: WithParts[]
|
|
57
|
+
}
|
|
58
|
+
function createBar(value: number, maxValue: number, width: number, char: string = "█"): string {
|
|
59
|
+
if (maxValue === 0) return ""
|
|
60
|
+
const filled = Math.round((value / maxValue) * width)
|
|
61
|
+
const bar = char.repeat(Math.max(0, filled))
|
|
62
|
+
return bar
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function formatContextMessage(breakdown: TokenBreakdown): string {
|
|
66
|
+
const lines: string[] = []
|
|
67
|
+
const barWidth = 30
|
|
68
|
+
|
|
69
|
+
const toolsLabel = `Tools (${breakdown.toolsInContextCount})`
|
|
70
|
+
|
|
71
|
+
const categories = [
|
|
72
|
+
{ label: "System", value: breakdown.system, char: "█" },
|
|
73
|
+
{ label: "User", value: breakdown.user, char: "▓" },
|
|
74
|
+
{ label: "Assistant", value: breakdown.assistant, char: "▒" },
|
|
75
|
+
{ label: toolsLabel, value: breakdown.tools, char: "░" },
|
|
76
|
+
] as const
|
|
77
|
+
|
|
78
|
+
const maxLabelLen = Math.max(...categories.map((c) => c.label.length))
|
|
79
|
+
|
|
80
|
+
lines.push("╭───────────────────────────────────────────────────────────╮")
|
|
81
|
+
lines.push("│ DCP Context Analysis │")
|
|
82
|
+
lines.push("╰───────────────────────────────────────────────────────────╯")
|
|
83
|
+
lines.push("")
|
|
84
|
+
lines.push("Session Context Breakdown:")
|
|
85
|
+
lines.push("─".repeat(60))
|
|
86
|
+
lines.push("")
|
|
87
|
+
|
|
88
|
+
for (const cat of categories) {
|
|
89
|
+
const bar = createBar(cat.value, breakdown.total, barWidth, cat.char)
|
|
90
|
+
const percentage =
|
|
91
|
+
breakdown.total > 0 ? ((cat.value / breakdown.total) * 100).toFixed(1) : "0.0"
|
|
92
|
+
const labelWithPct = `${cat.label.padEnd(maxLabelLen)} ${percentage.padStart(5)}% `
|
|
93
|
+
const valueStr = formatTokenCount(cat.value).padStart(13)
|
|
94
|
+
lines.push(`${labelWithPct}│${bar.padEnd(barWidth)}│${valueStr}`)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
lines.push("")
|
|
98
|
+
lines.push("─".repeat(60))
|
|
99
|
+
lines.push("")
|
|
100
|
+
|
|
101
|
+
lines.push("Summary:")
|
|
102
|
+
|
|
103
|
+
if (breakdown.prunedTokens > 0) {
|
|
104
|
+
const withoutPruning = breakdown.total + breakdown.prunedTokens
|
|
105
|
+
const pruned = []
|
|
106
|
+
if (breakdown.prunedToolCount > 0) pruned.push(`${breakdown.prunedToolCount} tools`)
|
|
107
|
+
if (breakdown.prunedMessageCount > 0)
|
|
108
|
+
pruned.push(`${breakdown.prunedMessageCount} messages`)
|
|
109
|
+
lines.push(
|
|
110
|
+
` Pruned: ${pruned.join(", ")} (~${formatTokenCount(breakdown.prunedTokens)})`,
|
|
111
|
+
)
|
|
112
|
+
lines.push(` Current context: ~${formatTokenCount(breakdown.total)}`)
|
|
113
|
+
lines.push(` Without DCP: ~${formatTokenCount(withoutPruning)}`)
|
|
114
|
+
} else {
|
|
115
|
+
lines.push(` Current context: ~${formatTokenCount(breakdown.total)}`)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
lines.push("")
|
|
119
|
+
|
|
120
|
+
return lines.join("\n")
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function handleContextCommand(ctx: ContextCommandContext): Promise<void> {
|
|
124
|
+
const { client, state, logger, sessionId, messages } = ctx
|
|
125
|
+
|
|
126
|
+
const { breakdown } = analyzeTokens(state, messages)
|
|
127
|
+
|
|
128
|
+
const message = formatContextMessage(breakdown)
|
|
129
|
+
|
|
130
|
+
const params = getCurrentParams(state, messages, logger)
|
|
131
|
+
await sendIgnoredMessage(client, sessionId, message, params, logger)
|
|
132
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import type { Logger } from "../logger"
|
|
2
|
+
import type { CompressionBlock, PruneMessagesState, SessionState, WithParts } from "../state"
|
|
3
|
+
import { syncCompressionBlocks } from "../messages"
|
|
4
|
+
import { parseBlockRef } from "../message-ids"
|
|
5
|
+
import { getCurrentParams } from "../token-utils"
|
|
6
|
+
import { saveSessionState } from "../state/persistence"
|
|
7
|
+
import { sendIgnoredMessage } from "../ui/notification"
|
|
8
|
+
import { formatTokenCount } from "../ui/utils"
|
|
9
|
+
import {
|
|
10
|
+
getActiveCompressionTargets,
|
|
11
|
+
resolveCompressionTarget,
|
|
12
|
+
type CompressionTarget,
|
|
13
|
+
} from "./compression-targets"
|
|
14
|
+
|
|
15
|
+
export interface DecompressCommandContext {
|
|
16
|
+
client: any
|
|
17
|
+
state: SessionState
|
|
18
|
+
logger: Logger
|
|
19
|
+
sessionId: string
|
|
20
|
+
messages: WithParts[]
|
|
21
|
+
args: string[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parseBlockIdArg(arg: string): number | null {
|
|
25
|
+
const normalized = arg.trim().toLowerCase()
|
|
26
|
+
const blockRef = parseBlockRef(normalized)
|
|
27
|
+
if (blockRef !== null) {
|
|
28
|
+
return blockRef
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!/^[1-9]\d*$/.test(normalized)) {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const parsed = Number.parseInt(normalized, 10)
|
|
36
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function findActiveParentBlockId(
|
|
40
|
+
messagesState: PruneMessagesState,
|
|
41
|
+
block: CompressionBlock,
|
|
42
|
+
): number | null {
|
|
43
|
+
const queue = [...block.parentBlockIds]
|
|
44
|
+
const visited = new Set<number>()
|
|
45
|
+
|
|
46
|
+
while (queue.length > 0) {
|
|
47
|
+
const parentBlockId = queue.shift()
|
|
48
|
+
if (parentBlockId === undefined || visited.has(parentBlockId)) {
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
visited.add(parentBlockId)
|
|
52
|
+
|
|
53
|
+
const parent = messagesState.blocksById.get(parentBlockId)
|
|
54
|
+
if (!parent) {
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (parent.active) {
|
|
59
|
+
return parent.blockId
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const ancestorId of parent.parentBlockIds) {
|
|
63
|
+
if (!visited.has(ancestorId)) {
|
|
64
|
+
queue.push(ancestorId)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function findActiveAncestorBlockId(
|
|
73
|
+
messagesState: PruneMessagesState,
|
|
74
|
+
target: CompressionTarget,
|
|
75
|
+
): number | null {
|
|
76
|
+
for (const block of target.blocks) {
|
|
77
|
+
const activeAncestorBlockId = findActiveParentBlockId(messagesState, block)
|
|
78
|
+
if (activeAncestorBlockId !== null) {
|
|
79
|
+
return activeAncestorBlockId
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function snapshotActiveMessages(messagesState: PruneMessagesState): Map<string, number> {
|
|
87
|
+
const activeMessages = new Map<string, number>()
|
|
88
|
+
for (const [messageId, entry] of messagesState.byMessageId) {
|
|
89
|
+
if (entry.activeBlockIds.length > 0) {
|
|
90
|
+
activeMessages.set(messageId, entry.tokenCount)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return activeMessages
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function formatDecompressMessage(
|
|
97
|
+
target: CompressionTarget,
|
|
98
|
+
restoredMessageCount: number,
|
|
99
|
+
restoredTokens: number,
|
|
100
|
+
reactivatedBlockIds: number[],
|
|
101
|
+
): string {
|
|
102
|
+
const lines: string[] = []
|
|
103
|
+
|
|
104
|
+
lines.push(`Restored compression ${target.displayId}.`)
|
|
105
|
+
if (target.runId !== target.displayId || target.grouped) {
|
|
106
|
+
lines.push(`Tool call label: Compression #${target.runId}.`)
|
|
107
|
+
}
|
|
108
|
+
if (reactivatedBlockIds.length > 0) {
|
|
109
|
+
const refs = reactivatedBlockIds.map((id) => String(id)).join(", ")
|
|
110
|
+
lines.push(`Also restored nested compression(s): ${refs}.`)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (restoredMessageCount > 0) {
|
|
114
|
+
lines.push(
|
|
115
|
+
`Restored ${restoredMessageCount} message(s) (~${formatTokenCount(restoredTokens)}).`,
|
|
116
|
+
)
|
|
117
|
+
} else {
|
|
118
|
+
lines.push("No messages were restored.")
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return lines.join("\n")
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function formatAvailableBlocksMessage(availableTargets: CompressionTarget[]): string {
|
|
125
|
+
const lines: string[] = []
|
|
126
|
+
|
|
127
|
+
lines.push("Usage: /dcp decompress <n>")
|
|
128
|
+
lines.push("")
|
|
129
|
+
|
|
130
|
+
if (availableTargets.length === 0) {
|
|
131
|
+
lines.push("No compressions are available to restore.")
|
|
132
|
+
return lines.join("\n")
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
lines.push("Available compressions:")
|
|
136
|
+
const entries = availableTargets.map((target) => {
|
|
137
|
+
const topic = target.topic.replace(/\s+/g, " ").trim() || "(no topic)"
|
|
138
|
+
const label = `${target.displayId} (${formatTokenCount(target.compressedTokens)})`
|
|
139
|
+
const details = target.grouped
|
|
140
|
+
? `Compression #${target.runId} - ${target.blocks.length} messages`
|
|
141
|
+
: `Compression #${target.runId}`
|
|
142
|
+
return { label, topic: `${details} - ${topic}` }
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
const labelWidth = Math.max(...entries.map((entry) => entry.label.length)) + 4
|
|
146
|
+
for (const entry of entries) {
|
|
147
|
+
lines.push(` ${entry.label.padEnd(labelWidth)}${entry.topic}`)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return lines.join("\n")
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function handleDecompressCommand(ctx: DecompressCommandContext): Promise<void> {
|
|
154
|
+
const { client, state, logger, sessionId, messages, args } = ctx
|
|
155
|
+
|
|
156
|
+
const params = getCurrentParams(state, messages, logger)
|
|
157
|
+
const targetArg = args[0]
|
|
158
|
+
|
|
159
|
+
if (args.length > 1) {
|
|
160
|
+
await sendIgnoredMessage(
|
|
161
|
+
client,
|
|
162
|
+
sessionId,
|
|
163
|
+
"Invalid arguments. Usage: /dcp decompress <n>",
|
|
164
|
+
params,
|
|
165
|
+
logger,
|
|
166
|
+
)
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
syncCompressionBlocks(state, logger, messages)
|
|
171
|
+
const messagesState = state.prune.messages
|
|
172
|
+
|
|
173
|
+
if (!targetArg) {
|
|
174
|
+
const availableTargets = getActiveCompressionTargets(messagesState)
|
|
175
|
+
const message = formatAvailableBlocksMessage(availableTargets)
|
|
176
|
+
await sendIgnoredMessage(client, sessionId, message, params, logger)
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const targetBlockId = parseBlockIdArg(targetArg)
|
|
181
|
+
if (targetBlockId === null) {
|
|
182
|
+
await sendIgnoredMessage(
|
|
183
|
+
client,
|
|
184
|
+
sessionId,
|
|
185
|
+
`Please enter a compression number. Example: /dcp decompress 2`,
|
|
186
|
+
params,
|
|
187
|
+
logger,
|
|
188
|
+
)
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const target = resolveCompressionTarget(messagesState, targetBlockId)
|
|
193
|
+
if (!target) {
|
|
194
|
+
await sendIgnoredMessage(
|
|
195
|
+
client,
|
|
196
|
+
sessionId,
|
|
197
|
+
`Compression ${targetBlockId} does not exist.`,
|
|
198
|
+
params,
|
|
199
|
+
logger,
|
|
200
|
+
)
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const activeBlocks = target.blocks.filter((block) => block.active)
|
|
205
|
+
if (activeBlocks.length === 0) {
|
|
206
|
+
const activeAncestorBlockId = findActiveAncestorBlockId(messagesState, target)
|
|
207
|
+
if (activeAncestorBlockId !== null) {
|
|
208
|
+
await sendIgnoredMessage(
|
|
209
|
+
client,
|
|
210
|
+
sessionId,
|
|
211
|
+
`Compression ${target.displayId} is inside compression ${activeAncestorBlockId}. Restore compression ${activeAncestorBlockId} first.`,
|
|
212
|
+
params,
|
|
213
|
+
logger,
|
|
214
|
+
)
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
await sendIgnoredMessage(
|
|
219
|
+
client,
|
|
220
|
+
sessionId,
|
|
221
|
+
`Compression ${target.displayId} is not active.`,
|
|
222
|
+
params,
|
|
223
|
+
logger,
|
|
224
|
+
)
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const activeMessagesBefore = snapshotActiveMessages(messagesState)
|
|
229
|
+
const activeBlockIdsBefore = new Set(messagesState.activeBlockIds)
|
|
230
|
+
const deactivatedAt = Date.now()
|
|
231
|
+
|
|
232
|
+
for (const block of target.blocks) {
|
|
233
|
+
block.active = false
|
|
234
|
+
block.deactivatedByUser = true
|
|
235
|
+
block.deactivatedAt = deactivatedAt
|
|
236
|
+
block.deactivatedByBlockId = undefined
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
syncCompressionBlocks(state, logger, messages)
|
|
240
|
+
|
|
241
|
+
let restoredMessageCount = 0
|
|
242
|
+
let restoredTokens = 0
|
|
243
|
+
for (const [messageId, tokenCount] of activeMessagesBefore) {
|
|
244
|
+
const entry = messagesState.byMessageId.get(messageId)
|
|
245
|
+
const isActiveNow = entry ? entry.activeBlockIds.length > 0 : false
|
|
246
|
+
if (!isActiveNow) {
|
|
247
|
+
restoredMessageCount++
|
|
248
|
+
restoredTokens += tokenCount
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
state.stats.totalPruneTokens = Math.max(0, state.stats.totalPruneTokens - restoredTokens)
|
|
253
|
+
|
|
254
|
+
const reactivatedBlockIds = Array.from(messagesState.activeBlockIds)
|
|
255
|
+
.filter((blockId) => !activeBlockIdsBefore.has(blockId))
|
|
256
|
+
.sort((a, b) => a - b)
|
|
257
|
+
|
|
258
|
+
await saveSessionState(state, logger)
|
|
259
|
+
|
|
260
|
+
const message = formatDecompressMessage(
|
|
261
|
+
target,
|
|
262
|
+
restoredMessageCount,
|
|
263
|
+
restoredTokens,
|
|
264
|
+
reactivatedBlockIds,
|
|
265
|
+
)
|
|
266
|
+
await sendIgnoredMessage(client, sessionId, message, params, logger)
|
|
267
|
+
|
|
268
|
+
logger.info("Decompress command completed", {
|
|
269
|
+
targetBlockId: target.displayId,
|
|
270
|
+
targetRunId: target.runId,
|
|
271
|
+
restoredMessageCount,
|
|
272
|
+
restoredTokens,
|
|
273
|
+
reactivatedBlockIds,
|
|
274
|
+
})
|
|
275
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DCP Help command handler.
|
|
3
|
+
* Shows available DCP commands and their descriptions.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Logger } from "../logger"
|
|
7
|
+
import type { PluginConfig } from "../config"
|
|
8
|
+
import type { SessionState, WithParts } from "../state"
|
|
9
|
+
import { compressPermission } from "../compress-permission"
|
|
10
|
+
import { sendIgnoredMessage } from "../ui/notification"
|
|
11
|
+
import { getCurrentParams } from "../token-utils"
|
|
12
|
+
|
|
13
|
+
export interface HelpCommandContext {
|
|
14
|
+
client: any
|
|
15
|
+
state: SessionState
|
|
16
|
+
config: PluginConfig
|
|
17
|
+
logger: Logger
|
|
18
|
+
sessionId: string
|
|
19
|
+
messages: WithParts[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const BASE_COMMANDS: [string, string][] = [
|
|
23
|
+
["/dcp context", "Show token usage breakdown for current session"],
|
|
24
|
+
["/dcp stats", "Show DCP pruning statistics"],
|
|
25
|
+
["/dcp sweep [n]", "Prune tools since last user message, or last n tools"],
|
|
26
|
+
["/dcp manual [on|off]", "Toggle manual mode or set explicit state"],
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
const TOOL_COMMANDS: Record<string, [string, string]> = {
|
|
30
|
+
compress: ["/dcp compress [focus]", "Trigger manual compress tool execution"],
|
|
31
|
+
decompress: ["/dcp decompress <n>", "Restore selected compression"],
|
|
32
|
+
recompress: ["/dcp recompress <n>", "Re-apply a user-decompressed compression"],
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function getVisibleCommands(state: SessionState, config: PluginConfig): [string, string][] {
|
|
36
|
+
const commands = [...BASE_COMMANDS]
|
|
37
|
+
|
|
38
|
+
if (compressPermission(state, config) !== "deny") {
|
|
39
|
+
commands.push(TOOL_COMMANDS.compress)
|
|
40
|
+
commands.push(TOOL_COMMANDS.decompress)
|
|
41
|
+
commands.push(TOOL_COMMANDS.recompress)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return commands
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function formatHelpMessage(state: SessionState, config: PluginConfig): string {
|
|
48
|
+
const commands = getVisibleCommands(state, config)
|
|
49
|
+
const colWidth = Math.max(...commands.map(([cmd]) => cmd.length)) + 4
|
|
50
|
+
const lines: string[] = []
|
|
51
|
+
|
|
52
|
+
lines.push("╭─────────────────────────────────────────────────────────────────────────╮")
|
|
53
|
+
lines.push("│ DCP Commands │")
|
|
54
|
+
lines.push("╰─────────────────────────────────────────────────────────────────────────╯")
|
|
55
|
+
lines.push("")
|
|
56
|
+
lines.push(` ${"Manual mode:".padEnd(colWidth)}${state.manualMode ? "ON" : "OFF"}`)
|
|
57
|
+
lines.push("")
|
|
58
|
+
for (const [cmd, desc] of commands) {
|
|
59
|
+
lines.push(` ${cmd.padEnd(colWidth)}${desc}`)
|
|
60
|
+
}
|
|
61
|
+
lines.push("")
|
|
62
|
+
|
|
63
|
+
return lines.join("\n")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function handleHelpCommand(ctx: HelpCommandContext): Promise<void> {
|
|
67
|
+
const { client, state, logger, sessionId, messages } = ctx
|
|
68
|
+
|
|
69
|
+
const { config } = ctx
|
|
70
|
+
const message = formatHelpMessage(state, config)
|
|
71
|
+
|
|
72
|
+
const params = getCurrentParams(state, messages, logger)
|
|
73
|
+
await sendIgnoredMessage(client, sessionId, message, params, logger)
|
|
74
|
+
|
|
75
|
+
logger.info("Help command executed")
|
|
76
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { handleContextCommand } from "./context"
|
|
2
|
+
export { handleDecompressCommand } from "./decompress"
|
|
3
|
+
export { handleHelpCommand } from "./help"
|
|
4
|
+
export {
|
|
5
|
+
applyPendingManualTrigger,
|
|
6
|
+
handleManualToggleCommand,
|
|
7
|
+
handleManualTriggerCommand,
|
|
8
|
+
} from "./manual"
|
|
9
|
+
export { handleRecompressCommand } from "./recompress"
|
|
10
|
+
export { handleStatsCommand } from "./stats"
|
|
11
|
+
export { handleSweepCommand } from "./sweep"
|