Skip to content

Commit 60bb054

Browse files
authored
Merge pull request #227 from Opencode-DCP/dev
merge dev into master
2 parents 6a3ffbe + c3a7fdc commit 60bb054

File tree

11 files changed

+64
-28
lines changed

11 files changed

+64
-28
lines changed

index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ const plugin: Plugin = (async (ctx) => {
3737
"Summarize what was done in this conversation",
3838
]
3939
if (internalAgentSignatures.some((sig) => systemText.includes(sig))) {
40-
logger.info("Skipping DCP injection for internal agent")
41-
state.isInternalAgent = true
40+
logger.info("Skipping DCP system prompt injection for internal agent")
4241
return
4342
}
4443

45-
// Reset flag for normal sessions
46-
state.isInternalAgent = false
47-
4844
const discardEnabled = config.tools.discard.enabled
4945
const extractEnabled = config.tools.extract.enabled
5046

@@ -68,6 +64,21 @@ const plugin: Plugin = (async (ctx) => {
6864
logger,
6965
config,
7066
),
67+
"chat.message": async (
68+
input: {
69+
sessionID: string
70+
agent?: string
71+
model?: { providerID: string; modelID: string }
72+
messageID?: string
73+
variant?: string
74+
},
75+
_output: any,
76+
) => {
77+
// Cache variant from real user messages (not synthetic)
78+
// This avoids scanning all messages to find variant
79+
state.variant = input.variant
80+
logger.debug("Cached variant from chat.message hook", { variant: input.variant })
81+
},
7182
tool: {
7283
...(config.tools.discard.enabled && {
7384
discard: createDiscardTool({

lib/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function createChatMessageTransformHandler(
1515
return async (input: {}, output: { messages: WithParts[] }) => {
1616
await checkSession(client, state, logger, output.messages)
1717

18-
if (state.isSubAgent || state.isInternalAgent) {
18+
if (state.isSubAgent) {
1919
return
2020
}
2121

lib/messages/inject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SessionState, WithParts } from "../state"
22
import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
4+
import type { UserMessage } from "@opencode-ai/sdk/v2"
45
import { loadPrompt } from "../prompts"
56
import { extractParameterKey, buildToolIdList, createSyntheticUserMessage } from "./utils"
67
import { getLastUserMessage } from "../shared-utils"
@@ -125,5 +126,6 @@ export const insertPruneToolContext = (
125126
if (!lastUserMessage) {
126127
return
127128
}
128-
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent))
129+
const variant = state.variant ?? (lastUserMessage.info as UserMessage).variant
130+
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
129131
}

lib/messages/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Logger } from "../logger"
22
import { isMessageCompacted } from "../shared-utils"
33
import type { SessionState, WithParts } from "../state"
4-
import type { UserMessage } from "@opencode-ai/sdk"
4+
import type { UserMessage } from "@opencode-ai/sdk/v2"
55

66
const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
77
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
88

9-
export const createSyntheticUserMessage = (baseMessage: WithParts, content: string): WithParts => {
9+
export const createSyntheticUserMessage = (
10+
baseMessage: WithParts,
11+
content: string,
12+
variant?: string,
13+
): WithParts => {
1014
const userInfo = baseMessage.info as UserMessage
1115
return {
1216
info: {
@@ -19,6 +23,7 @@ export const createSyntheticUserMessage = (baseMessage: WithParts, content: stri
1923
providerID: userInfo.model.providerID,
2024
modelID: userInfo.model.modelID,
2125
},
26+
...(variant !== undefined && { variant }),
2227
},
2328
parts: [
2429
{

lib/state/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function createSessionState(): SessionState {
4343
return {
4444
sessionId: null,
4545
isSubAgent: false,
46-
isInternalAgent: false,
4746
prune: {
4847
toolIds: [],
4948
},
@@ -56,13 +55,13 @@ export function createSessionState(): SessionState {
5655
lastToolPrune: false,
5756
lastCompaction: 0,
5857
currentTurn: 0,
58+
variant: undefined,
5959
}
6060
}
6161

6262
export function resetSessionState(state: SessionState): void {
6363
state.sessionId = null
6464
state.isSubAgent = false
65-
state.isInternalAgent = false
6665
state.prune = {
6766
toolIds: [],
6867
}
@@ -75,6 +74,7 @@ export function resetSessionState(state: SessionState): void {
7574
state.lastToolPrune = false
7675
state.lastCompaction = 0
7776
state.currentTurn = 0
77+
state.variant = undefined
7878
}
7979

8080
export async function ensureSessionInitialized(

lib/state/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message, Part } from "@opencode-ai/sdk"
1+
import { Message, Part } from "@opencode-ai/sdk/v2"
22

33
export interface WithParts {
44
info: Message
@@ -27,12 +27,12 @@ export interface Prune {
2727
export interface SessionState {
2828
sessionId: string | null
2929
isSubAgent: boolean
30-
isInternalAgent: boolean
3130
prune: Prune
3231
stats: SessionStats
3332
toolParameters: Map<string, ToolParameterEntry>
3433
nudgeCounter: number
3534
lastToolPrune: boolean
3635
lastCompaction: number
3736
currentTurn: number
37+
variant: string | undefined
3838
}

lib/strategies/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async function executePruneOperation(
5858

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

61-
const currentParams = getCurrentParams(messages, logger)
61+
const currentParams = getCurrentParams(state, messages, logger)
6262
const toolIdList: string[] = buildToolIdList(state, messages, logger)
6363

6464
// Validate that all numeric IDs are within bounds

lib/strategies/utils.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import { SessionState, WithParts } from "../state"
2-
import { UserMessage } from "@opencode-ai/sdk"
2+
import { UserMessage } from "@opencode-ai/sdk/v2"
33
import { Logger } from "../logger"
44
import { encode } from "gpt-tokenizer"
55
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"
66

77
export function getCurrentParams(
8+
state: SessionState,
89
messages: WithParts[],
910
logger: Logger,
1011
): {
1112
providerId: string | undefined
1213
modelId: string | undefined
1314
agent: string | undefined
15+
variant: string | undefined
1416
} {
1517
const userMsg = getLastUserMessage(messages)
1618
if (!userMsg) {
1719
logger.debug("No user message found when determining current params")
18-
return { providerId: undefined, modelId: undefined, agent: undefined }
20+
return {
21+
providerId: undefined,
22+
modelId: undefined,
23+
agent: undefined,
24+
variant: state.variant,
25+
}
1926
}
20-
const agent: string = (userMsg.info as UserMessage).agent
21-
const providerId: string | undefined = (userMsg.info as UserMessage).model.providerID
22-
const modelId: string | undefined = (userMsg.info as UserMessage).model.modelID
27+
const userInfo = userMsg.info as UserMessage
28+
const agent: string = userInfo.agent
29+
const providerId: string | undefined = userInfo.model.providerID
30+
const modelId: string | undefined = userInfo.model.modelID
31+
const variant: string | undefined = state.variant ?? userInfo.variant
2332

24-
return { providerId, modelId, agent }
33+
return { providerId, modelId, agent, variant }
2534
}
2635

2736
/**

lib/ui/notification.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export async function sendIgnoredMessage(
9999
logger: Logger,
100100
): Promise<void> {
101101
const agent = params.agent || undefined
102+
const variant = params.variant || undefined
102103
const model =
103104
params.providerId && params.modelId
104105
? {
@@ -116,6 +117,7 @@ export async function sendIgnoredMessage(
116117
noReply: true,
117118
agent: agent,
118119
model: model,
120+
variant: variant,
119121
parts: [
120122
{
121123
type: "text",

package-lock.json

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)