Skip to content

Commit 0462d2f

Browse files
committed
refactor: extract system prompt handler to hooks.ts
1 parent 4a18692 commit 0462d2f

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

index.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { Plugin } from "@opencode-ai/plugin"
22
import { getConfig } from "./lib/config"
33
import { Logger } from "./lib/logger"
4-
import { loadPrompt } from "./lib/prompts"
54
import { createSessionState } from "./lib/state"
65
import { createDiscardTool, createExtractTool } from "./lib/strategies"
7-
import { createChatMessageTransformHandler } from "./lib/hooks"
6+
import { createChatMessageTransformHandler, createSystemPromptHandler } from "./lib/hooks"
87

98
const plugin: Plugin = (async (ctx) => {
109
const config = getConfig(ctx)
@@ -26,42 +25,7 @@ const plugin: Plugin = (async (ctx) => {
2625
})
2726

2827
return {
29-
"experimental.chat.system.transform": async (
30-
_input: unknown,
31-
output: { system: string[] },
32-
) => {
33-
if (state.isSubAgent) {
34-
return
35-
}
36-
37-
const systemText = output.system.join("\n")
38-
const internalAgentSignatures = [
39-
"You are a title generator",
40-
"You are a helpful AI assistant tasked with summarizing conversations",
41-
"Summarize what was done in this conversation",
42-
]
43-
if (internalAgentSignatures.some((sig) => systemText.includes(sig))) {
44-
logger.info("Skipping DCP system prompt injection for internal agent")
45-
return
46-
}
47-
48-
const discardEnabled = config.tools.discard.enabled
49-
const extractEnabled = config.tools.extract.enabled
50-
51-
let promptName: string
52-
if (discardEnabled && extractEnabled) {
53-
promptName = "user/system/system-prompt-both"
54-
} else if (discardEnabled) {
55-
promptName = "user/system/system-prompt-discard"
56-
} else if (extractEnabled) {
57-
promptName = "user/system/system-prompt-extract"
58-
} else {
59-
return
60-
}
61-
62-
const syntheticPrompt = loadPrompt(promptName)
63-
output.system.push(syntheticPrompt)
64-
},
28+
"experimental.chat.system.transform": createSystemPromptHandler(state, logger, config),
6529
"experimental.chat.messages.transform": createChatMessageTransformHandler(
6630
ctx.client,
6731
state,

lib/hooks.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,48 @@ import { syncToolCache } from "./state/tool-cache"
55
import { deduplicate, supersedeWrites, purgeErrors } from "./strategies"
66
import { prune, insertPruneToolContext } from "./messages"
77
import { checkSession } from "./state"
8+
import { loadPrompt } from "./prompts"
9+
10+
const INTERNAL_AGENT_SIGNATURES = [
11+
"You are a title generator",
12+
"You are a helpful AI assistant tasked with summarizing conversations",
13+
"Summarize what was done in this conversation",
14+
]
15+
16+
export function createSystemPromptHandler(
17+
state: SessionState,
18+
logger: Logger,
19+
config: PluginConfig,
20+
) {
21+
return async (_input: unknown, output: { system: string[] }) => {
22+
if (state.isSubAgent) {
23+
return
24+
}
25+
26+
const systemText = output.system.join("\n")
27+
if (INTERNAL_AGENT_SIGNATURES.some((sig) => systemText.includes(sig))) {
28+
logger.info("Skipping DCP system prompt injection for internal agent")
29+
return
30+
}
31+
32+
const discardEnabled = config.tools.discard.enabled
33+
const extractEnabled = config.tools.extract.enabled
34+
35+
let promptName: string
36+
if (discardEnabled && extractEnabled) {
37+
promptName = "user/system/system-prompt-both"
38+
} else if (discardEnabled) {
39+
promptName = "user/system/system-prompt-discard"
40+
} else if (extractEnabled) {
41+
promptName = "user/system/system-prompt-extract"
42+
} else {
43+
return
44+
}
45+
46+
const syntheticPrompt = loadPrompt(promptName)
47+
output.system.push(syntheticPrompt)
48+
}
49+
}
850

951
export function createChatMessageTransformHandler(
1052
client: any,

0 commit comments

Comments
 (0)