@serkanalgur/opencodev2-slim 2.0.1 → 2.0.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/README.md +34 -2
- package/package.json +1 -1
- package/src/index.ts +88 -102
- package/src/lib/compress.ts +17 -11
- package/src/lib/types.ts +2 -2
package/README.md
CHANGED
|
@@ -5,8 +5,6 @@
|
|
|
5
5
|
|
|
6
6
|
Smart context management plugin for OpenCode v2. Optimizes token usage through semantic compression, cost-aware pruning, and adaptive thresholds.
|
|
7
7
|
|
|
8
|
-
> **Note:** This plugin requires OpenCode v2 (1.18.29+). For v1 compatibility, use [@serkanalgur/opencode-slim](https://github.com/serkanalgur/opencode-slim).
|
|
9
|
-
|
|
10
8
|
## Features
|
|
11
9
|
|
|
12
10
|
- **TUI Panel** - Rich context usage visualization with status indicators
|
|
@@ -141,6 +139,40 @@ State is saved to disk, so compression history and learning persist across resta
|
|
|
141
139
|
|
|
142
140
|
Note: Compression is performed by the AI assistant using the `compress` tool. The slash command provides guidance on usage.
|
|
143
141
|
|
|
142
|
+
## Changelog
|
|
143
|
+
|
|
144
|
+
### 2.0.3
|
|
145
|
+
|
|
146
|
+
- Fix v2 API compatibility issues
|
|
147
|
+
- Remove namespace from tool registration
|
|
148
|
+
- Handle both v1 and v2 message part types
|
|
149
|
+
- Make context hooks synchronous
|
|
150
|
+
|
|
151
|
+
### 2.0.2
|
|
152
|
+
|
|
153
|
+
- Update README documentation
|
|
154
|
+
- Add GitHub Actions workflow for automated npm publish
|
|
155
|
+
|
|
156
|
+
### 2.0.1
|
|
157
|
+
|
|
158
|
+
- Fix npm publish conflict
|
|
159
|
+
|
|
160
|
+
### 2.0.0
|
|
161
|
+
|
|
162
|
+
**Breaking Changes:** Migrated to OpenCode v2 plugin API.
|
|
163
|
+
|
|
164
|
+
- Migrate from `@opencode-ai/plugin` to `@opencode/plugin`
|
|
165
|
+
- Use `Plugin.define()` pattern instead of server function
|
|
166
|
+
- Register tools via `ctx.tool.transform()` with JSON Schema input
|
|
167
|
+
- Replace experimental hooks with `ctx.session.hook()`
|
|
168
|
+
- Replace event callback with `ctx.event.subscribe()`
|
|
169
|
+
- Update cleanup to use setup return function
|
|
170
|
+
- Minimum OpenCode version: 2.0.0
|
|
171
|
+
|
|
172
|
+
### 1.0.1
|
|
173
|
+
|
|
174
|
+
- Initial release
|
|
175
|
+
|
|
144
176
|
## License
|
|
145
177
|
|
|
146
178
|
MIT
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -43,17 +43,21 @@ function buildCompressionSummary(messages: MessageWithParts[], focus: string): s
|
|
|
43
43
|
|
|
44
44
|
for (const msg of messages) {
|
|
45
45
|
for (const part of msg.parts) {
|
|
46
|
-
if (part.type === "tool") {
|
|
46
|
+
if (part.type === "tool-call") {
|
|
47
47
|
const toolPart = part as any
|
|
48
48
|
toolCalls.push(
|
|
49
|
-
`${toolPart.
|
|
49
|
+
`${toolPart.name}: ${JSON.stringify(toolPart.input || {}).slice(0, 100)}`,
|
|
50
50
|
)
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
}
|
|
52
|
+
if (part.type === "tool-result") {
|
|
53
|
+
const toolPart = part as any
|
|
54
|
+
if (toolPart.result?.type === "error") {
|
|
55
|
+
errors.push(String(toolPart.result.value).slice(0, 200) || "Unknown error")
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
if (part.type === "text") {
|
|
56
|
-
const
|
|
59
|
+
const textPart = part as any
|
|
60
|
+
const text = textPart.text || ""
|
|
57
61
|
if (
|
|
58
62
|
text.includes("decided") ||
|
|
59
63
|
text.includes("chose") ||
|
|
@@ -86,22 +90,23 @@ function buildCompressionSummary(messages: MessageWithParts[], focus: string): s
|
|
|
86
90
|
return lines.join("\n")
|
|
87
91
|
}
|
|
88
92
|
|
|
93
|
+
function wrapAsMessageWithParts(msg: any): MessageWithParts {
|
|
94
|
+
return {
|
|
95
|
+
info: msg.info || { id: msg.id || "", role: msg.role, sessionID: "", time: { created: Date.now() } },
|
|
96
|
+
parts: msg.parts || msg.content || [],
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
89
100
|
// ─── Plugin Entry ───────────────────────────────────────────────────────────
|
|
90
101
|
|
|
91
102
|
export default Plugin.define({
|
|
92
103
|
id: "opencodev2-slim",
|
|
93
104
|
async setup(ctx) {
|
|
94
|
-
// Load and create default config if needed
|
|
95
105
|
createDefaultConfig()
|
|
96
106
|
const globalConfig = loadConfig()
|
|
97
107
|
|
|
98
108
|
// ─── Register Compress Tool ───────────────────────────────────────
|
|
99
109
|
await ctx.tool.transform((editor) => {
|
|
100
|
-
editor.namespace({
|
|
101
|
-
name: "slim",
|
|
102
|
-
description: "Smart context management tools",
|
|
103
|
-
})
|
|
104
|
-
|
|
105
110
|
editor.add({
|
|
106
111
|
name: "compress",
|
|
107
112
|
description: getCompressToolDescription(),
|
|
@@ -150,9 +155,7 @@ export default Plugin.define({
|
|
|
150
155
|
}
|
|
151
156
|
const mode = args.mode || "auto"
|
|
152
157
|
const keepRecent = args.keepRecent || 5
|
|
153
|
-
|
|
154
|
-
// Get session ID from context or fallback
|
|
155
|
-
const sessionId = (context as any).sessionID || ""
|
|
158
|
+
const sessionId = context.sessionID
|
|
156
159
|
const config = getConfig(sessionId)
|
|
157
160
|
const state = getState(sessionId, config)
|
|
158
161
|
|
|
@@ -163,12 +166,10 @@ export default Plugin.define({
|
|
|
163
166
|
return { content: "No messages found in session" }
|
|
164
167
|
}
|
|
165
168
|
|
|
166
|
-
const messageWithParts: MessageWithParts[] = messages.map(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}))
|
|
169
|
+
const messageWithParts: MessageWithParts[] = messages.map(
|
|
170
|
+
(m: any) => wrapAsMessageWithParts(m),
|
|
171
|
+
)
|
|
170
172
|
|
|
171
|
-
// Determine what to compress
|
|
172
173
|
let targetIndices: number[] = []
|
|
173
174
|
let inputTokens = 0
|
|
174
175
|
|
|
@@ -193,7 +194,6 @@ export default Plugin.define({
|
|
|
193
194
|
}
|
|
194
195
|
}
|
|
195
196
|
} else {
|
|
196
|
-
// Auto mode
|
|
197
197
|
for (let i = 0; i < messageWithParts.length - keepRecent; i++) {
|
|
198
198
|
const msg = messageWithParts[i]
|
|
199
199
|
const text = getMessageText(msg) + getToolResultContent(msg)
|
|
@@ -210,7 +210,6 @@ export default Plugin.define({
|
|
|
210
210
|
|
|
211
211
|
const targetMessages = targetIndices.map((i) => messageWithParts[i])
|
|
212
212
|
const summary = buildCompressionSummary(targetMessages, args.focus)
|
|
213
|
-
|
|
214
213
|
const outputTokens = await countTokens(summary)
|
|
215
214
|
const ratio = inputTokens > 0 ? 1 - outputTokens / inputTokens : 0
|
|
216
215
|
|
|
@@ -256,7 +255,7 @@ export default Plugin.define({
|
|
|
256
255
|
additionalProperties: false,
|
|
257
256
|
},
|
|
258
257
|
execute: async (_input, context) => {
|
|
259
|
-
const sessionId =
|
|
258
|
+
const sessionId = context.sessionID
|
|
260
259
|
const config = getConfig(sessionId)
|
|
261
260
|
const state = getState(sessionId, config)
|
|
262
261
|
|
|
@@ -267,23 +266,18 @@ export default Plugin.define({
|
|
|
267
266
|
return { content: "No messages found in session" }
|
|
268
267
|
}
|
|
269
268
|
|
|
270
|
-
const messageWithParts: MessageWithParts[] = messages.map(
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}))
|
|
274
|
-
|
|
275
|
-
const modelId = (context as any).model?.id || "unknown"
|
|
269
|
+
const messageWithParts: MessageWithParts[] = messages.map(
|
|
270
|
+
(m: any) => wrapAsMessageWithParts(m),
|
|
271
|
+
)
|
|
276
272
|
|
|
277
273
|
const panelData = await buildPanelData(
|
|
278
274
|
sessionId,
|
|
279
275
|
messageWithParts,
|
|
280
276
|
state,
|
|
281
277
|
config,
|
|
282
|
-
modelId,
|
|
283
278
|
)
|
|
284
279
|
|
|
285
280
|
const panel = renderPanel(panelData)
|
|
286
|
-
|
|
287
281
|
return { content: panel }
|
|
288
282
|
} catch (error) {
|
|
289
283
|
return {
|
|
@@ -294,92 +288,85 @@ export default Plugin.define({
|
|
|
294
288
|
})
|
|
295
289
|
})
|
|
296
290
|
|
|
297
|
-
// ─── System Prompt Hook
|
|
291
|
+
// ─── System Prompt Hook (sync) ───────────────────────────────────
|
|
298
292
|
await ctx.session.hook("context", (event) => {
|
|
299
|
-
const sessionId =
|
|
293
|
+
const sessionId = event.sessionID
|
|
300
294
|
const config = getConfig(sessionId)
|
|
301
|
-
if (!config.enabled || !config.compress.enabled)
|
|
302
|
-
return
|
|
303
|
-
}
|
|
295
|
+
if (!config.enabled || !config.compress.enabled) return
|
|
304
296
|
|
|
305
297
|
const state = getState(sessionId, config)
|
|
298
|
+
state.modelContextLimit = 200000 // default; updated by tool calls
|
|
306
299
|
|
|
307
|
-
|
|
308
|
-
if ((event as any).model?.limit?.context) {
|
|
309
|
-
state.modelContextLimit = (event as any).model.limit.context
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Add system prompt
|
|
313
|
-
const systemPrompt = getSystemPrompt()
|
|
314
|
-
event.system.push({ type: "text", text: systemPrompt })
|
|
300
|
+
event.system.push({ type: "text", text: getSystemPrompt() })
|
|
315
301
|
})
|
|
316
302
|
|
|
317
|
-
// ─── Messages Transform Hook
|
|
318
|
-
await ctx.session.hook("context",
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
303
|
+
// ─── Messages Transform Hook (sync) ──────────────────────────────
|
|
304
|
+
await ctx.session.hook("context", (event) => {
|
|
305
|
+
const sessionId = event.sessionID
|
|
306
|
+
const config = getConfig(sessionId)
|
|
307
|
+
if (!config.enabled) return
|
|
323
308
|
|
|
324
|
-
const sessionId = (event as any).sessionID || ""
|
|
325
309
|
const state = getState(sessionId, config)
|
|
326
310
|
|
|
327
|
-
// Apply pruning strategies
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
event.messages.push(
|
|
311
|
+
// Apply pruning strategies
|
|
312
|
+
const pruned = pruneMessages(
|
|
313
|
+
event.messages.map((m: any) => wrapAsMessageWithParts(m)),
|
|
314
|
+
config,
|
|
315
|
+
event.messages.length,
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
// Replace messages in-place
|
|
319
|
+
event.messages.length = 0
|
|
320
|
+
for (const msg of pruned) {
|
|
321
|
+
event.messages.push(msg as any)
|
|
322
|
+
}
|
|
338
323
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
324
|
+
// Quick token estimate (sync, ~4 chars per token)
|
|
325
|
+
let totalTokens = 0
|
|
326
|
+
for (const msg of event.messages) {
|
|
327
|
+
const content = (msg as any).content
|
|
328
|
+
if (Array.isArray(content)) {
|
|
329
|
+
for (const part of content) {
|
|
330
|
+
if (part.type === "text" && part.text) {
|
|
331
|
+
totalTokens += Math.ceil(part.text.length / 4)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
344
334
|
}
|
|
335
|
+
}
|
|
345
336
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
337
|
+
state.currentTokenCount = totalTokens
|
|
338
|
+
|
|
339
|
+
const maxTokens = resolveTokenLimit(
|
|
340
|
+
config.compress.maxContextLimit,
|
|
341
|
+
state.modelContextLimit,
|
|
342
|
+
)
|
|
343
|
+
const minTokens = resolveTokenLimit(
|
|
344
|
+
config.compress.minContextLimit,
|
|
345
|
+
state.modelContextLimit,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
const shouldComp = shouldCompress(
|
|
349
|
+
totalTokens,
|
|
350
|
+
maxTokens,
|
|
351
|
+
minTokens,
|
|
352
|
+
state.lastCompressionTime,
|
|
353
|
+
config.compress.nudgeFrequency,
|
|
354
|
+
event.messages.length,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
if (shouldComp.compress && !state.manualMode) {
|
|
358
|
+
const nudgeMessage = getNudgeMessage(
|
|
359
|
+
shouldComp.reason,
|
|
358
360
|
totalTokens,
|
|
359
361
|
maxTokens,
|
|
360
|
-
minTokens,
|
|
361
|
-
state.lastCompressionTime,
|
|
362
|
-
config.compress.nudgeFrequency,
|
|
363
|
-
event.messages.length,
|
|
364
362
|
)
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
totalTokens,
|
|
370
|
-
maxTokens,
|
|
371
|
-
)
|
|
372
|
-
event.messages.push({
|
|
373
|
-
info: {
|
|
374
|
-
role: "assistant",
|
|
375
|
-
sessionID: sessionId,
|
|
376
|
-
} as any,
|
|
377
|
-
parts: [{ type: "text", text: nudgeMessage }],
|
|
378
|
-
} as any)
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
saveSessionState(state, config.persistence.directory)
|
|
363
|
+
event.messages.push({
|
|
364
|
+
role: "assistant",
|
|
365
|
+
content: [{ type: "text", text: nudgeMessage }],
|
|
366
|
+
} as any)
|
|
382
367
|
}
|
|
368
|
+
|
|
369
|
+
saveSessionState(state, config.persistence.directory)
|
|
383
370
|
})
|
|
384
371
|
|
|
385
372
|
// ─── Event Subscription ──────────────────────────────────────────
|
|
@@ -387,8 +374,8 @@ export default Plugin.define({
|
|
|
387
374
|
void (async () => {
|
|
388
375
|
for await (const event of ctx.event.subscribe({ signal: eventController.signal })) {
|
|
389
376
|
if (event.type === "session.created") {
|
|
390
|
-
const
|
|
391
|
-
const sessionId =
|
|
377
|
+
const props = (event as any).properties || {}
|
|
378
|
+
const sessionId = props.sessionID || ""
|
|
392
379
|
const config = getConfig(sessionId)
|
|
393
380
|
sessionConfigs.set(sessionId, config)
|
|
394
381
|
getState(sessionId, config)
|
|
@@ -399,7 +386,6 @@ export default Plugin.define({
|
|
|
399
386
|
// ─── Cleanup ─────────────────────────────────────────────────────
|
|
400
387
|
return () => {
|
|
401
388
|
eventController.abort()
|
|
402
|
-
// Save all states on dispose
|
|
403
389
|
for (const [sessionId, state] of sessionStates.entries()) {
|
|
404
390
|
const config = getConfig(sessionId)
|
|
405
391
|
saveSessionState(state, config.persistence.directory)
|
package/src/lib/compress.ts
CHANGED
|
@@ -10,7 +10,6 @@ async function getTokenizer() {
|
|
|
10
10
|
const mod = await import("@anthropic-ai/tokenizer")
|
|
11
11
|
tokenizer = mod
|
|
12
12
|
} catch {
|
|
13
|
-
// Fallback: estimate ~4 chars per token
|
|
14
13
|
return null
|
|
15
14
|
}
|
|
16
15
|
}
|
|
@@ -40,9 +39,8 @@ export function getMessageText(msg: MessageWithParts): string {
|
|
|
40
39
|
|
|
41
40
|
for (const part of msg.parts) {
|
|
42
41
|
if (part.type === "text") {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
texts.push(textPart.text)
|
|
42
|
+
if (part.text) {
|
|
43
|
+
texts.push(part.text)
|
|
46
44
|
}
|
|
47
45
|
}
|
|
48
46
|
}
|
|
@@ -54,10 +52,15 @@ export function getToolResultContent(msg: MessageWithParts): string {
|
|
|
54
52
|
const results: string[] = []
|
|
55
53
|
|
|
56
54
|
for (const part of msg.parts) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
// v1 SDK format: type === "tool"
|
|
56
|
+
if (part.type === "tool" && part.state?.type === "result" && part.state?.output) {
|
|
57
|
+
results.push(String(part.state.output).slice(0, 500))
|
|
58
|
+
}
|
|
59
|
+
// v2 AI format: type === "tool-result"
|
|
60
|
+
if (part.type === "tool-result" && part.result) {
|
|
61
|
+
const val = part.result.value
|
|
62
|
+
if (val !== undefined && val !== null) {
|
|
63
|
+
results.push(String(val).slice(0, 500))
|
|
61
64
|
}
|
|
62
65
|
}
|
|
63
66
|
}
|
|
@@ -67,9 +70,13 @@ export function getToolResultContent(msg: MessageWithParts): string {
|
|
|
67
70
|
|
|
68
71
|
export function getToolName(msg: MessageWithParts): string | null {
|
|
69
72
|
for (const part of msg.parts) {
|
|
73
|
+
// v1 SDK format
|
|
70
74
|
if (part.type === "tool") {
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
return part.tool || null
|
|
76
|
+
}
|
|
77
|
+
// v2 AI format
|
|
78
|
+
if (part.type === "tool-call") {
|
|
79
|
+
return part.name || null
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
return null
|
|
@@ -96,7 +103,6 @@ export function shouldCompress(
|
|
|
96
103
|
|
|
97
104
|
// Soft limit: recommend compression
|
|
98
105
|
if (currentTokens >= minTokens) {
|
|
99
|
-
// Check if enough time has passed since last compression
|
|
100
106
|
if (minutesSinceLast >= nudgeFrequency) {
|
|
101
107
|
return {
|
|
102
108
|
compress: true,
|
package/src/lib/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Message
|
|
1
|
+
import type { Message } from "@opencode-ai/sdk/v2"
|
|
2
2
|
|
|
3
3
|
// ─── Config Types ───────────────────────────────────────────────────────────
|
|
4
4
|
|
|
@@ -90,7 +90,7 @@ export interface ToolCallInfo {
|
|
|
90
90
|
|
|
91
91
|
export interface MessageWithParts {
|
|
92
92
|
info: Message
|
|
93
|
-
parts:
|
|
93
|
+
parts: any[]
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
export interface CompressionRange {
|