@@ -5,6 +5,7 @@ import { Logger } from "./lib/logger"
55import { Janitor , type SessionStats } from "./lib/janitor"
66import { checkForUpdates } from "./lib/version-checker"
77import { loadPrompt } from "./lib/prompt"
8+ import { injectSynthInstruction } from "./lib/synth-instruction"
89
910async 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 }
0 commit comments