Skip to content

Commit f29b49a

Browse files
authored
Merge pull request #243 from spoons-and-mirrors/synthetic-msg-copilot-fix
Synthetic msg copilot fix
2 parents ef665bf + 426094d commit f29b49a

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

lib/messages/inject.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
44
import type { UserMessage } from "@opencode-ai/sdk/v2"
55
import { loadPrompt } from "../prompts"
6-
import { extractParameterKey, buildToolIdList, createSyntheticUserMessage } from "./utils"
6+
import {
7+
extractParameterKey,
8+
buildToolIdList,
9+
createSyntheticAssistantMessageWithToolPart,
10+
createSyntheticUserMessage,
11+
} from "./utils"
712
import { getFilePathFromParameters, isProtectedFilePath } from "../protected-file-patterns"
813
import { getLastUserMessage } from "../shared-utils"
914

@@ -132,6 +137,28 @@ export const insertPruneToolContext = (
132137
if (!lastUserMessage) {
133138
return
134139
}
140+
141+
const userInfo = lastUserMessage.info as UserMessage
142+
const providerID = userInfo.model.providerID
143+
const isGitHubCopilot =
144+
providerID === "github-copilot" || providerID === "github-copilot-enterprise"
145+
146+
logger.info("Injecting prunable-tools list", {
147+
providerID,
148+
isGitHubCopilot,
149+
injectionType: isGitHubCopilot ? "assistant-with-tool-part" : "user-message",
150+
})
151+
135152
const variant = state.variant ?? (lastUserMessage.info as UserMessage).variant
136-
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
153+
if (isGitHubCopilot) {
154+
messages.push(
155+
createSyntheticAssistantMessageWithToolPart(
156+
lastUserMessage,
157+
prunableToolsContent,
158+
variant,
159+
),
160+
)
161+
} else {
162+
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
163+
}
137164
}

lib/messages/utils.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { UserMessage } from "@opencode-ai/sdk/v2"
55

66
const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
77
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
8+
const SYNTHETIC_CALL_ID = "call_01234567890123456789012345"
89

910
export const createSyntheticUserMessage = (
1011
baseMessage: WithParts,
@@ -37,6 +38,53 @@ export const createSyntheticUserMessage = (
3738
}
3839
}
3940

41+
export const createSyntheticAssistantMessageWithToolPart = (
42+
baseMessage: WithParts,
43+
content: string,
44+
variant?: string,
45+
): WithParts => {
46+
const userInfo = baseMessage.info as UserMessage
47+
const now = Date.now()
48+
return {
49+
info: {
50+
id: SYNTHETIC_MESSAGE_ID,
51+
sessionID: userInfo.sessionID,
52+
role: "assistant",
53+
agent: userInfo.agent || "code",
54+
parentID: userInfo.id,
55+
modelID: userInfo.model.modelID,
56+
providerID: userInfo.model.providerID,
57+
mode: "default",
58+
path: {
59+
cwd: "/",
60+
root: "/",
61+
},
62+
time: { created: now, completed: now },
63+
cost: 0,
64+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
65+
...(variant !== undefined && { variant }),
66+
},
67+
parts: [
68+
{
69+
id: SYNTHETIC_PART_ID,
70+
sessionID: userInfo.sessionID,
71+
messageID: SYNTHETIC_MESSAGE_ID,
72+
type: "tool",
73+
callID: SYNTHETIC_CALL_ID,
74+
tool: "context_info",
75+
state: {
76+
status: "completed",
77+
input: {},
78+
output: content,
79+
title: "Context Info",
80+
metadata: {},
81+
time: { start: now, end: now },
82+
},
83+
},
84+
],
85+
}
86+
}
87+
4088
/**
4189
* Extracts a human-readable key from tool metadata for display purposes.
4290
*/

0 commit comments

Comments
 (0)