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
31 changes: 29 additions & 2 deletions lib/messages/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ 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 {
extractParameterKey,
buildToolIdList,
createSyntheticAssistantMessageWithToolPart,
createSyntheticUserMessage,
} from "./utils"
import { getFilePathFromParameters, isProtectedFilePath } from "../protected-file-patterns"
import { getLastUserMessage } from "../shared-utils"

Expand Down Expand Up @@ -132,6 +137,28 @@ export const insertPruneToolContext = (
if (!lastUserMessage) {
return
}

const userInfo = lastUserMessage.info as UserMessage
const providerID = userInfo.model.providerID
const isGitHubCopilot =
providerID === "github-copilot" || providerID === "github-copilot-enterprise"

logger.info("Injecting prunable-tools list", {
providerID,
isGitHubCopilot,
injectionType: isGitHubCopilot ? "assistant-with-tool-part" : "user-message",
})

const variant = state.variant ?? (lastUserMessage.info as UserMessage).variant
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
if (isGitHubCopilot) {
messages.push(
createSyntheticAssistantMessageWithToolPart(
lastUserMessage,
prunableToolsContent,
variant,
),
)
} else {
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
}
}
48 changes: 48 additions & 0 deletions lib/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { UserMessage } from "@opencode-ai/sdk/v2"

const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
const SYNTHETIC_CALL_ID = "call_01234567890123456789012345"

export const createSyntheticUserMessage = (
baseMessage: WithParts,
Expand Down Expand Up @@ -37,6 +38,53 @@ export const createSyntheticUserMessage = (
}
}

export const createSyntheticAssistantMessageWithToolPart = (
baseMessage: WithParts,
content: string,
variant?: string,
): WithParts => {
const userInfo = baseMessage.info as UserMessage
const now = Date.now()
return {
info: {
id: SYNTHETIC_MESSAGE_ID,
sessionID: userInfo.sessionID,
role: "assistant",
agent: userInfo.agent || "code",
parentID: userInfo.id,
modelID: userInfo.model.modelID,
providerID: userInfo.model.providerID,
mode: "default",
path: {
cwd: "/",
root: "/",
},
time: { created: now, completed: now },
cost: 0,
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
...(variant !== undefined && { variant }),
},
parts: [
{
id: SYNTHETIC_PART_ID,
sessionID: userInfo.sessionID,
messageID: SYNTHETIC_MESSAGE_ID,
type: "tool",
callID: SYNTHETIC_CALL_ID,
tool: "context_info",
state: {
status: "completed",
input: {},
output: content,
title: "Context Info",
metadata: {},
time: { start: now, end: now },
},
},
],
}
}

/**
* Extracts a human-readable key from tool metadata for display purposes.
*/
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion 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.5",
"version": "1.1.6",
"type": "module",
"description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context",
"main": "./dist/index.js",
Expand Down