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
21 changes: 16 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ const plugin: Plugin = (async (ctx) => {
"Summarize what was done in this conversation",
]
if (internalAgentSignatures.some((sig) => systemText.includes(sig))) {
logger.info("Skipping DCP injection for internal agent")
state.isInternalAgent = true
logger.info("Skipping DCP system prompt injection for internal agent")
return
}

// Reset flag for normal sessions
state.isInternalAgent = false

const discardEnabled = config.tools.discard.enabled
const extractEnabled = config.tools.extract.enabled

Expand All @@ -68,6 +64,21 @@ const plugin: Plugin = (async (ctx) => {
logger,
config,
),
"chat.message": async (
input: {
sessionID: string
agent?: string
model?: { providerID: string; modelID: string }
messageID?: string
variant?: string
},
_output: any,
) => {
// Cache variant from real user messages (not synthetic)
// This avoids scanning all messages to find variant
state.variant = input.variant
logger.debug("Cached variant from chat.message hook", { variant: input.variant })
},
tool: {
...(config.tools.discard.enabled && {
discard: createDiscardTool({
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createChatMessageTransformHandler(
return async (input: {}, output: { messages: WithParts[] }) => {
await checkSession(client, state, logger, output.messages)

if (state.isSubAgent || state.isInternalAgent) {
if (state.isSubAgent) {
return
}

Expand Down
4 changes: 3 additions & 1 deletion lib/messages/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SessionState, WithParts } from "../state"
import type { Logger } from "../logger"
import type { PluginConfig } from "../config"
import type { UserMessage } from "@opencode-ai/sdk/v2"
import { loadPrompt } from "../prompts"
import { extractParameterKey, buildToolIdList, createSyntheticUserMessage } from "./utils"
import { getLastUserMessage } from "../shared-utils"
Expand Down Expand Up @@ -125,5 +126,6 @@ export const insertPruneToolContext = (
if (!lastUserMessage) {
return
}
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent))
const variant = state.variant ?? (lastUserMessage.info as UserMessage).variant
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
}
9 changes: 7 additions & 2 deletions lib/messages/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Logger } from "../logger"
import { isMessageCompacted } from "../shared-utils"
import type { SessionState, WithParts } from "../state"
import type { UserMessage } from "@opencode-ai/sdk"
import type { UserMessage } from "@opencode-ai/sdk/v2"

const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"

export const createSyntheticUserMessage = (baseMessage: WithParts, content: string): WithParts => {
export const createSyntheticUserMessage = (
baseMessage: WithParts,
content: string,
variant?: string,
): WithParts => {
const userInfo = baseMessage.info as UserMessage
return {
info: {
Expand All @@ -19,6 +23,7 @@ export const createSyntheticUserMessage = (baseMessage: WithParts, content: stri
providerID: userInfo.model.providerID,
modelID: userInfo.model.modelID,
},
...(variant !== undefined && { variant }),
},
parts: [
{
Expand Down
4 changes: 2 additions & 2 deletions lib/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export function createSessionState(): SessionState {
return {
sessionId: null,
isSubAgent: false,
isInternalAgent: false,
prune: {
toolIds: [],
},
Expand All @@ -56,13 +55,13 @@ export function createSessionState(): SessionState {
lastToolPrune: false,
lastCompaction: 0,
currentTurn: 0,
variant: undefined,
}
}

export function resetSessionState(state: SessionState): void {
state.sessionId = null
state.isSubAgent = false
state.isInternalAgent = false
state.prune = {
toolIds: [],
}
Expand All @@ -75,6 +74,7 @@ export function resetSessionState(state: SessionState): void {
state.lastToolPrune = false
state.lastCompaction = 0
state.currentTurn = 0
state.variant = undefined
}

export async function ensureSessionInitialized(
Expand Down
4 changes: 2 additions & 2 deletions lib/state/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, Part } from "@opencode-ai/sdk"
import { Message, Part } from "@opencode-ai/sdk/v2"

export interface WithParts {
info: Message
Expand Down Expand Up @@ -27,12 +27,12 @@ export interface Prune {
export interface SessionState {
sessionId: string | null
isSubAgent: boolean
isInternalAgent: boolean
prune: Prune
stats: SessionStats
toolParameters: Map<string, ToolParameterEntry>
nudgeCounter: number
lastToolPrune: boolean
lastCompaction: number
currentTurn: number
variant: string | undefined
}
2 changes: 1 addition & 1 deletion lib/strategies/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function executePruneOperation(

await ensureSessionInitialized(ctx.client, state, sessionId, logger, messages)

const currentParams = getCurrentParams(messages, logger)
const currentParams = getCurrentParams(state, messages, logger)
const toolIdList: string[] = buildToolIdList(state, messages, logger)

// Validate that all numeric IDs are within bounds
Expand Down
21 changes: 15 additions & 6 deletions lib/strategies/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { SessionState, WithParts } from "../state"
import { UserMessage } from "@opencode-ai/sdk"
import { UserMessage } from "@opencode-ai/sdk/v2"
import { Logger } from "../logger"
import { encode } from "gpt-tokenizer"
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"

export function getCurrentParams(
state: SessionState,
messages: WithParts[],
logger: Logger,
): {
providerId: string | undefined
modelId: string | undefined
agent: string | undefined
variant: string | undefined
} {
const userMsg = getLastUserMessage(messages)
if (!userMsg) {
logger.debug("No user message found when determining current params")
return { providerId: undefined, modelId: undefined, agent: undefined }
return {
providerId: undefined,
modelId: undefined,
agent: undefined,
variant: state.variant,
}
}
const agent: string = (userMsg.info as UserMessage).agent
const providerId: string | undefined = (userMsg.info as UserMessage).model.providerID
const modelId: string | undefined = (userMsg.info as UserMessage).model.modelID
const userInfo = userMsg.info as UserMessage
const agent: string = userInfo.agent
const providerId: string | undefined = userInfo.model.providerID
const modelId: string | undefined = userInfo.model.modelID
const variant: string | undefined = state.variant ?? userInfo.variant

return { providerId, modelId, agent }
return { providerId, modelId, agent, variant }
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function sendIgnoredMessage(
logger: Logger,
): Promise<void> {
const agent = params.agent || undefined
const variant = params.variant || undefined
const model =
params.providerId && params.modelId
? {
Expand All @@ -116,6 +117,7 @@ export async function sendIgnoredMessage(
noReply: true,
agent: agent,
model: model,
variant: variant,
parts: [
{
type: "text",
Expand Down
19 changes: 13 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@tarquinen/opencode-dcp",
"version": "1.1.3",
"version": "1.1.4",
"type": "module",
"description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context",
"main": "./dist/index.js",
Expand Down Expand Up @@ -40,7 +40,7 @@
"@opencode-ai/plugin": ">=0.13.7"
},
"dependencies": {
"@opencode-ai/sdk": "latest",
"@opencode-ai/sdk": "^1.1.3",
"gpt-tokenizer": "^3.4.0",
"jsonc-parser": "^3.3.1",
"zod": "^4.1.13"
Expand Down