diff --git a/lib/messages/inject.ts b/lib/messages/inject.ts index 5d9b36f..132e784 100644 --- a/lib/messages/inject.ts +++ b/lib/messages/inject.ts @@ -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" @@ -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)) + } } diff --git a/lib/messages/utils.ts b/lib/messages/utils.ts index 0338d86..9a9a2d8 100644 --- a/lib/messages/utils.ts +++ b/lib/messages/utils.ts @@ -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, @@ -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. */ diff --git a/package-lock.json b/package-lock.json index 9cede6e..d06ba6d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tarquinen/opencode-dcp", - "version": "1.1.5", + "version": "1.1.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@tarquinen/opencode-dcp", - "version": "1.1.5", + "version": "1.1.6", "license": "MIT", "dependencies": { "@opencode-ai/sdk": "^1.1.3", diff --git a/package.json b/package.json index f6305ca..2c9e86a 100644 --- a/package.json +++ b/package.json @@ -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",