Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
} from "./lib/hooks"
import { configureClientAuth, isSecureMode } from "./lib/auth"

const plugin: Plugin = (async (ctx) => {
const id = "opencode-dynamic-context-pruning"

const server: Plugin = (async (ctx) => {
const config = getConfig(ctx)

if (!config.enabled) {
Expand Down Expand Up @@ -133,4 +135,7 @@ const plugin: Plugin = (async (ctx) => {
}
}) satisfies Plugin

export default plugin
export default {
id,
server,
}
4 changes: 2 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from "fs"
import { join, dirname } from "path"
import { homedir } from "os"
import * as jsoncParser from "jsonc-parser"
import { parse } from "jsonc-parser/lib/esm/main.js"
import type { PluginInput } from "@opencode-ai/plugin"

type Permission = "ask" | "allow" | "deny"
Expand Down Expand Up @@ -773,7 +773,7 @@ function loadConfigFile(configPath: string): ConfigLoadResult {
}

try {
const parsed = jsoncParser.parse(fileContent, undefined, { allowTrailingComma: true })
const parsed = parse(fileContent, undefined, { allowTrailingComma: true })
if (parsed === undefined || parsed === null) {
return { data: null, parseError: "Config file is empty or invalid" }
}
Expand Down
1 change: 1 addition & 0 deletions lib/prompts/compress-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ During general cleanup, compress all medium and high-priority messages that are
Optimize for reducing context footprint, not for grouping messages by topic.
Do not compress away still-active instructions, unresolved questions, or constraints that are likely to matter soon.
Prioritize the earliest messages in the context as they will be the least relevant to the active task.
General cleanup should be done periodically between other normal compression tool passes, not as the primary form of compression.
`
6 changes: 4 additions & 2 deletions lib/token-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { SessionState, WithParts } from "./state"
import { AssistantMessage, UserMessage } from "@opencode-ai/sdk/v2"
import { Logger } from "./logger"
import * as anthropicTokenizer from "@anthropic-ai/tokenizer"
import * as _anthropicTokenizer from "@anthropic-ai/tokenizer"
const anthropicCountTokens = (_anthropicTokenizer.countTokens ??
(_anthropicTokenizer as any).default?.countTokens) as typeof _anthropicTokenizer.countTokens
import { getLastUserMessage } from "./messages/query"

export function getCurrentTokenUsage(state: SessionState, messages: WithParts[]): number {
Expand Down Expand Up @@ -67,7 +69,7 @@ export function getCurrentParams(
export function countTokens(text: string): number {
if (!text) return 0
try {
return anthropicTokenizer.countTokens(text)
return anthropicCountTokens(text)
} catch {
return Math.round(text.length / 4)
}
Expand Down
32 changes: 1 addition & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./server": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"scripts": {
"clean": "rm -rf dist",
"build": "npm run clean && tsc",
Expand Down Expand Up @@ -39,12 +49,11 @@
"author": "tarquinen",
"license": "AGPL-3.0-or-later",
"peerDependencies": {
"@opencode-ai/plugin": ">=0.13.7"
"@opencode-ai/plugin": ">=1.2.0"
},
"dependencies": {
"@anthropic-ai/tokenizer": "^0.0.4",
"@opencode-ai/sdk": "^1.3.2",
"fuzzball": "^2.2.3",
"jsonc-parser": "^3.3.1",
"zod": "^4.3.6"
},
Expand Down
12 changes: 12 additions & 0 deletions scripts/verify-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ function assertPackageJsonShape() {
fail(`package.json main must remain ./dist/index.js, found ${pkg.main ?? "<missing>"}`)
}

if (pkg.exports?.["."]?.import !== "./dist/index.js") {
fail("expected package.json exports['.'].import to be './dist/index.js'")
}

if (pkg.exports?.["./server"]?.import !== "./dist/index.js") {
fail("expected package.json exports['./server'].import to be './dist/index.js'")
}

const files = Array.isArray(pkg.files) ? pkg.files : []
for (const entry of ["dist/", "README.md", "LICENSE"]) {
if (!files.includes(entry)) {
Expand Down Expand Up @@ -177,6 +185,10 @@ function validateRuntimeImportGraph() {
continue
}

if (entry.specifier === "jsonc-parser/lib/esm/main.js") {
continue
}

const packageName = getPackageName(entry.specifier)
if (builtinNames.has(packageName)) continue

Expand Down
Loading