Skip to content

Commit baf6e1d

Browse files
committed
simplify compress mode handling
1 parent b75aad8 commit baf6e1d

6 files changed

Lines changed: 30 additions & 39 deletions

File tree

index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Plugin } from "@opencode-ai/plugin"
22
import { getConfig } from "./lib/config"
3+
import { createCompressMessageTool, createCompressRangeTool } from "./lib/tools"
34
import {
45
compressDisabledByOpencode,
56
hasExplicitToolPermission,
67
type HostPermissionSnapshot,
78
} from "./lib/host-permissions"
89
import { Logger } from "./lib/logger"
910
import { createSessionState } from "./lib/state"
10-
import { createCompressTool } from "./lib/tools"
1111
import { PromptStore } from "./lib/prompts/store"
1212
import {
1313
createChatMessageTransformHandler,
@@ -41,6 +41,15 @@ const plugin: Plugin = (async (ctx) => {
4141
strategies: config.strategies,
4242
})
4343

44+
const compressToolContext = {
45+
client: ctx.client,
46+
state,
47+
logger,
48+
config,
49+
workingDirectory: ctx.directory,
50+
prompts,
51+
}
52+
4453
return {
4554
"experimental.chat.system.transform": createSystemPromptHandler(
4655
state,
@@ -81,14 +90,10 @@ const plugin: Plugin = (async (ctx) => {
8190
),
8291
tool: {
8392
...(config.compress.permission !== "deny" && {
84-
compress: createCompressTool({
85-
client: ctx.client,
86-
state,
87-
logger,
88-
config,
89-
workingDirectory: ctx.directory,
90-
prompts,
91-
}),
93+
compress:
94+
config.compress.mode === "message"
95+
? createCompressMessageTool(compressToolContext)
96+
: createCompressRangeTool(compressToolContext),
9297
}),
9398
},
9499
config: async (opencodeConfig) => {

lib/commands/manual.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function getTriggerPrompt(
3333
userFocus?: string,
3434
): string {
3535
const base = COMPRESS_TRIGGER_PROMPT
36-
const compressedBlockGuidance = buildCompressedBlockGuidance(state, config)
36+
const compressedBlockGuidance =
37+
config.compress.mode === "message" ? "" : buildCompressedBlockGuidance(state)
3738

3839
const sections = [base, compressedBlockGuidance]
3940
if (userFocus && userFocus.trim().length > 0) {

lib/messages/inject/utils.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,7 @@ export function addAnchor(
169169
return anchorMessageIds.size !== previousSize
170170
}
171171

172-
export function buildCompressedBlockGuidance(state: SessionState, config: PluginConfig): string {
173-
if (config.compress.mode === "message") {
174-
return [
175-
"Compressed message context:",
176-
"- Message mode is active. Compress individual raw messages using `mNNNN` IDs only.",
177-
"- Do not use block placeholders or `bN` references in message mode.",
178-
].join("\n")
179-
}
180-
172+
export function buildCompressedBlockGuidance(state: SessionState): string {
181173
const refs = Array.from(state.prune.messages.activeBlockIds)
182174
.filter((id) => Number.isInteger(id) && id > 0)
183175
.sort((a, b) => a - b)
@@ -193,6 +185,10 @@ export function buildCompressedBlockGuidance(state: SessionState, config: Plugin
193185
}
194186

195187
function appendGuidanceToDcpTag(hintText: string, guidance: string): string {
188+
if (!guidance.trim()) {
189+
return hintText
190+
}
191+
196192
const closeTag = "</dcp-system-reminder>"
197193
const closeTagIndex = hintText.lastIndexOf(closeTag)
198194

@@ -246,7 +242,8 @@ export function applyAnchoredNudges(
246242
messages: WithParts[],
247243
prompts: RuntimePrompts,
248244
): void {
249-
const compressedBlockGuidance = buildCompressedBlockGuidance(state, config)
245+
const compressedBlockGuidance =
246+
config.compress.mode === "message" ? "" : buildCompressedBlockGuidance(state)
250247

251248
const contextLimitNudge = appendGuidanceToDcpTag(
252249
prompts.contextLimitNudge,

lib/tools/compress.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/tools/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { ToolContext } from "./types"
2-
export { createCompressTool } from "./compress"
32
export { createCompressMessageTool } from "./compress-message"
43
export { createCompressRangeTool } from "./compress-range"

tests/compress-message.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import test from "node:test"
33
import { join } from "node:path"
44
import { tmpdir } from "node:os"
55
import { mkdirSync } from "node:fs"
6-
import { createCompressTool } from "../lib/tools/compress"
6+
import { createCompressMessageTool } from "../lib/tools/compress-message"
77
import { createSessionState, type WithParts } from "../lib/state"
88
import type { PluginConfig } from "../lib/config"
99
import { Logger } from "../lib/logger"
@@ -149,7 +149,7 @@ test("compress message mode batches individual message summaries", async () => {
149149
const rawMessages = buildMessages(sessionID)
150150
const state = createSessionState()
151151
const logger = new Logger(false)
152-
const tool = createCompressTool({
152+
const tool = createCompressMessageTool({
153153
client: {
154154
session: {
155155
messages: async () => ({ data: rawMessages }),
@@ -216,7 +216,7 @@ test("compress message mode rejects compressed block ids", async () => {
216216
const rawMessages = buildMessages(sessionID)
217217
const state = createSessionState()
218218
const logger = new Logger(false)
219-
const tool = createCompressTool({
219+
const tool = createCompressMessageTool({
220220
client: {
221221
session: {
222222
messages: async () => ({ data: rawMessages }),
@@ -287,7 +287,7 @@ test("compress message mode allows messages containing compress tool parts", asy
287287

288288
const state = createSessionState()
289289
const logger = new Logger(false)
290-
const tool = createCompressTool({
290+
const tool = createCompressMessageTool({
291291
client: {
292292
session: {
293293
messages: async () => ({ data: rawMessages }),
@@ -340,7 +340,7 @@ test("compress message mode sends one aggregated notification for batched messag
340340
config.pruneNotificationType = "toast"
341341

342342
const toastCalls: string[] = []
343-
const tool = createCompressTool({
343+
const tool = createCompressMessageTool({
344344
client: {
345345
session: {
346346
messages: async () => ({ data: rawMessages }),
@@ -398,7 +398,7 @@ test("compress message mode skips invalid batch entries and reports issues", asy
398398
const rawMessages = buildMessages(sessionID)
399399
const state = createSessionState()
400400
const logger = new Logger(false)
401-
const tool = createCompressTool({
401+
const tool = createCompressMessageTool({
402402
client: {
403403
session: {
404404
messages: async () => ({ data: rawMessages }),
@@ -463,7 +463,7 @@ test("compress message mode reports issues when every batch entry is skipped", a
463463
const rawMessages = buildMessages(sessionID)
464464
const state = createSessionState()
465465
const logger = new Logger(false)
466-
const tool = createCompressTool({
466+
const tool = createCompressMessageTool({
467467
client: {
468468
session: {
469469
messages: async () => ({ data: rawMessages }),

0 commit comments

Comments
 (0)