Skip to content

Commit c528e38

Browse files
fix: don't add synth instruction after DCP "ignored" summary messages - extract synth instruction to its own file
1 parent 141ee74 commit c528e38

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

index.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Logger } from "./lib/logger"
55
import { Janitor, type SessionStats } from "./lib/janitor"
66
import { checkForUpdates } from "./lib/version-checker"
77
import { loadPrompt } from "./lib/prompt"
8+
import { injectSynthInstruction } from "./lib/synth-instruction"
89

910
async function isSubagentSession(client: any, sessionID: string): Promise<boolean> {
1011
try {
@@ -61,37 +62,6 @@ const plugin: Plugin = (async (ctx) => {
6162
}
6263
}
6364

64-
// Inject synthInstruction into the last user message
65-
const injectSynthInstruction = (messages: any[]): boolean => {
66-
// Find the last user message
67-
for (let i = messages.length - 1; i >= 0; i--) {
68-
const msg = messages[i]
69-
if (msg.role === 'user') {
70-
const instruction = TOOL_SYNTH_INSTRUCTION
71-
72-
// Avoid double-injecting the same instruction
73-
if (typeof msg.content === 'string') {
74-
if (msg.content.includes(instruction)) {
75-
return false
76-
}
77-
msg.content = msg.content + '\n\n' + instruction
78-
} else if (Array.isArray(msg.content)) {
79-
const alreadyInjected = msg.content.some(
80-
(part: any) => part?.type === 'text' && typeof part.text === 'string' && part.text.includes(instruction)
81-
)
82-
if (alreadyInjected) {
83-
return false
84-
}
85-
msg.content.push({
86-
type: 'text',
87-
text: instruction
88-
})
89-
}
90-
return true
91-
}
92-
}
93-
return false
94-
}
9565

9666
// Global fetch wrapper - caches tool parameters, injects instructions, and performs pruning
9767
const originalGlobalFetch = globalThis.fetch
@@ -106,7 +76,7 @@ const plugin: Plugin = (async (ctx) => {
10676

10777
// Inject synthInstruction for the context_pruning tool
10878
if (config.strategies.onTool.length > 0) {
109-
if (injectSynthInstruction(body.messages)) {
79+
if (injectSynthInstruction(body.messages, TOOL_SYNTH_INSTRUCTION)) {
11080
logger.debug("fetch", "Injected synthInstruction")
11181
modified = true
11282
}

lib/synth-instruction.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export function isIgnoredUserMessage(msg: any): boolean {
2+
if (!msg || msg.role !== 'user') {
3+
return false
4+
}
5+
6+
if (msg.ignored || msg.info?.ignored) {
7+
return true
8+
}
9+
10+
if (Array.isArray(msg.content) && msg.content.length > 0) {
11+
const allPartsIgnored = msg.content.every((part: any) => part?.ignored)
12+
if (allPartsIgnored) {
13+
return true
14+
}
15+
}
16+
17+
return false
18+
}
19+
20+
export function injectSynthInstruction(messages: any[], instruction: string): boolean {
21+
// Find the last user message that is not ignored
22+
for (let i = messages.length - 1; i >= 0; i--) {
23+
const msg = messages[i]
24+
if (msg.role === 'user' && !isIgnoredUserMessage(msg)) {
25+
// Avoid double-injecting the same instruction
26+
if (typeof msg.content === 'string') {
27+
if (msg.content.includes(instruction)) {
28+
return false
29+
}
30+
msg.content = msg.content + '\n\n' + instruction
31+
} else if (Array.isArray(msg.content)) {
32+
const alreadyInjected = msg.content.some(
33+
(part: any) => part?.type === 'text' && typeof part.text === 'string' && part.text.includes(instruction)
34+
)
35+
if (alreadyInjected) {
36+
return false
37+
}
38+
msg.content.push({
39+
type: 'text',
40+
text: instruction
41+
})
42+
}
43+
return true
44+
}
45+
}
46+
return false
47+
}

0 commit comments

Comments
 (0)