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
15 changes: 15 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ const plugin: Plugin = (async (ctx) => {
logger,
config,
),
"chat.message": async (
input: {
sessionID: string
agent?: string
model?: { providerID: string; modelID: string }
messageID?: string
variant?: string
},
_output: any,
) => {
// Cache variant from real user messages (not synthetic)
// This avoids scanning all messages to find variant
state.variant = input.variant
logger.debug("Cached variant from chat.message hook", { variant: input.variant })
},
tool: {
...(config.tools.discard.enabled && {
discard: createDiscardTool({
Expand Down
4 changes: 3 additions & 1 deletion lib/messages/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SessionState, WithParts } from "../state"
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 { getLastUserMessage } from "../shared-utils"
Expand Down Expand Up @@ -125,5 +126,6 @@ export const insertPruneToolContext = (
if (!lastUserMessage) {
return
}
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent))
const variant = state.variant ?? (lastUserMessage.info as UserMessage).variant
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent, variant))
}
9 changes: 6 additions & 3 deletions lib/messages/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Logger } from "../logger"
import { isMessageCompacted } from "../shared-utils"
import type { SessionState, WithParts } from "../state"
import type { UserMessage } from "@opencode-ai/sdk"
import type { UserMessage } from "@opencode-ai/sdk/v2"

const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"

export const createSyntheticUserMessage = (baseMessage: WithParts, content: string): WithParts => {
export const createSyntheticUserMessage = (
baseMessage: WithParts,
content: string,
variant?: string,
): WithParts => {
const userInfo = baseMessage.info as UserMessage
const variant = (userInfo as any).variant
return {
info: {
id: SYNTHETIC_MESSAGE_ID,
Expand Down
2 changes: 2 additions & 0 deletions lib/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function createSessionState(): SessionState {
lastToolPrune: false,
lastCompaction: 0,
currentTurn: 0,
variant: undefined,
}
}

Expand All @@ -75,6 +76,7 @@ export function resetSessionState(state: SessionState): void {
state.lastToolPrune = false
state.lastCompaction = 0
state.currentTurn = 0
state.variant = undefined
}

export async function ensureSessionInitialized(
Expand Down
3 changes: 2 additions & 1 deletion lib/state/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, Part } from "@opencode-ai/sdk"
import { Message, Part } from "@opencode-ai/sdk/v2"

export interface WithParts {
info: Message
Expand Down Expand Up @@ -35,4 +35,5 @@ export interface SessionState {
lastToolPrune: boolean
lastCompaction: number
currentTurn: number
variant: string | undefined
}
2 changes: 1 addition & 1 deletion lib/strategies/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function executePruneOperation(

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

const currentParams = getCurrentParams(messages, logger)
const currentParams = getCurrentParams(state, messages, logger)
const toolIdList: string[] = buildToolIdList(state, messages, logger)

// Validate that all numeric IDs are within bounds
Expand Down
21 changes: 15 additions & 6 deletions lib/strategies/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { SessionState, WithParts } from "../state"
import { UserMessage } from "@opencode-ai/sdk"
import { UserMessage } from "@opencode-ai/sdk/v2"
import { Logger } from "../logger"
import { encode } from "gpt-tokenizer"
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"

export function getCurrentParams(
state: SessionState,
messages: WithParts[],
logger: Logger,
): {
providerId: string | undefined
modelId: string | undefined
agent: string | undefined
variant: string | undefined
} {
const userMsg = getLastUserMessage(messages)
if (!userMsg) {
logger.debug("No user message found when determining current params")
return { providerId: undefined, modelId: undefined, agent: undefined }
return {
providerId: undefined,
modelId: undefined,
agent: undefined,
variant: state.variant,
}
}
const agent: string = (userMsg.info as UserMessage).agent
const providerId: string | undefined = (userMsg.info as UserMessage).model.providerID
const modelId: string | undefined = (userMsg.info as UserMessage).model.modelID
const userInfo = userMsg.info as UserMessage
const agent: string = userInfo.agent
const providerId: string | undefined = userInfo.model.providerID
const modelId: string | undefined = userInfo.model.modelID
const variant: string | undefined = state.variant ?? userInfo.variant

return { providerId, modelId, agent }
return { providerId, modelId, agent, variant }
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function sendIgnoredMessage(
logger: Logger,
): Promise<void> {
const agent = params.agent || undefined
const variant = params.variant || undefined
const model =
params.providerId && params.modelId
? {
Expand All @@ -116,6 +117,7 @@ export async function sendIgnoredMessage(
noReply: true,
agent: agent,
model: model,
variant: variant,
parts: [
{
type: "text",
Expand Down
15 changes: 11 additions & 4 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
Expand Up @@ -40,7 +40,7 @@
"@opencode-ai/plugin": ">=0.13.7"
},
"dependencies": {
"@opencode-ai/sdk": "latest",
"@opencode-ai/sdk": "^1.1.3",
"gpt-tokenizer": "^3.4.0",
"jsonc-parser": "^3.3.1",
"zod": "^4.1.13"
Expand Down