Skip to content

Commit 3267d9f

Browse files
authored
Merge pull request #225 from cau1k/fix/variant-support
fix: propagate variant to notification messages
2 parents bb7b26d + 5a4b901 commit 3267d9f

File tree

10 files changed

+58
-17
lines changed

10 files changed

+58
-17
lines changed

index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ const plugin: Plugin = (async (ctx) => {
6868
logger,
6969
config,
7070
),
71+
"chat.message": async (
72+
input: {
73+
sessionID: string
74+
agent?: string
75+
model?: { providerID: string; modelID: string }
76+
messageID?: string
77+
variant?: string
78+
},
79+
_output: any,
80+
) => {
81+
// Cache variant from real user messages (not synthetic)
82+
// This avoids scanning all messages to find variant
83+
state.variant = input.variant
84+
logger.debug("Cached variant from chat.message hook", { variant: input.variant })
85+
},
7186
tool: {
7287
...(config.tools.discard.enabled && {
7388
discard: createDiscardTool({

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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
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
11-
const variant = (userInfo as any).variant
1215
return {
1316
info: {
1417
id: SYNTHETIC_MESSAGE_ID,

lib/state/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function createSessionState(): SessionState {
5656
lastToolPrune: false,
5757
lastCompaction: 0,
5858
currentTurn: 0,
59+
variant: undefined,
5960
}
6061
}
6162

@@ -75,6 +76,7 @@ export function resetSessionState(state: SessionState): void {
7576
state.lastToolPrune = false
7677
state.lastCompaction = 0
7778
state.currentTurn = 0
79+
state.variant = undefined
7880
}
7981

8082
export async function ensureSessionInitialized(

lib/state/types.ts

Lines changed: 2 additions & 1 deletion
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
@@ -35,4 +35,5 @@ export interface SessionState {
3535
lastToolPrune: boolean
3636
lastCompaction: number
3737
currentTurn: number
38+
variant: string | undefined
3839
}

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: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@opencode-ai/plugin": ">=0.13.7"
4141
},
4242
"dependencies": {
43-
"@opencode-ai/sdk": "latest",
43+
"@opencode-ai/sdk": "^1.1.3",
4444
"gpt-tokenizer": "^3.4.0",
4545
"jsonc-parser": "^3.3.1",
4646
"zod": "^4.1.13"

0 commit comments

Comments
 (0)