|
| 1 | +export interface ToolTracker { |
| 2 | + seenToolResultIds: Set<string> |
| 3 | + toolResultCount: number |
| 4 | +} |
| 5 | + |
| 6 | +export function createToolTracker(): ToolTracker { |
| 7 | + return { |
| 8 | + seenToolResultIds: new Set(), |
| 9 | + toolResultCount: 0 |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function countToolResults(messages: any[], tracker: ToolTracker): number { |
| 14 | + let newCount = 0 |
| 15 | + |
| 16 | + for (const m of messages) { |
| 17 | + if (m.role === 'tool' && m.tool_call_id) { |
| 18 | + const id = String(m.tool_call_id).toLowerCase() |
| 19 | + if (!tracker.seenToolResultIds.has(id)) { |
| 20 | + tracker.seenToolResultIds.add(id) |
| 21 | + newCount++ |
| 22 | + } |
| 23 | + } else if (m.role === 'user' && Array.isArray(m.content)) { |
| 24 | + for (const part of m.content) { |
| 25 | + if (part.type === 'tool_result' && part.tool_use_id) { |
| 26 | + const id = String(part.tool_use_id).toLowerCase() |
| 27 | + if (!tracker.seenToolResultIds.has(id)) { |
| 28 | + tracker.seenToolResultIds.add(id) |
| 29 | + newCount++ |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + tracker.toolResultCount += newCount |
| 37 | + return newCount |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Counts new tool results and injects nudge instruction every N tool results. |
| 42 | + * Returns true if injection happened. |
| 43 | + */ |
| 44 | +export function injectNudge( |
| 45 | + messages: any[], |
| 46 | + tracker: ToolTracker, |
| 47 | + nudgeText: string, |
| 48 | + nudgeFreq: number = 5 |
| 49 | +): boolean { |
| 50 | + const prevCount = tracker.toolResultCount |
| 51 | + const newCount = countToolResults(messages, tracker) |
| 52 | + |
| 53 | + if (newCount > 0) { |
| 54 | + // Check if we crossed a multiple of nudgeFreq |
| 55 | + const prevBucket = Math.floor(prevCount / nudgeFreq) |
| 56 | + const newBucket = Math.floor(tracker.toolResultCount / nudgeFreq) |
| 57 | + if (newBucket > prevBucket) { |
| 58 | + // Inject at the END of messages so it's in immediate context |
| 59 | + return appendNudge(messages, nudgeText) |
| 60 | + } |
| 61 | + } |
| 62 | + return false |
| 63 | +} |
| 64 | + |
| 65 | +export function isIgnoredUserMessage(msg: any): boolean { |
| 66 | + if (!msg || msg.role !== 'user') { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + // Skip ignored or synthetic messages |
| 71 | + if (msg.ignored || msg.info?.ignored || msg.synthetic) { |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + if (Array.isArray(msg.content) && msg.content.length > 0) { |
| 76 | + const allPartsIgnored = msg.content.every((part: any) => part?.ignored) |
| 77 | + if (allPartsIgnored) { |
| 78 | + return true |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return false |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Appends a nudge message at the END of the messages array as a new user message. |
| 87 | + * This ensures it's in the model's immediate context, not buried in old messages. |
| 88 | + */ |
| 89 | +function appendNudge(messages: any[], nudgeText: string): boolean { |
| 90 | + messages.push({ |
| 91 | + role: 'user', |
| 92 | + content: nudgeText, |
| 93 | + synthetic: true |
| 94 | + }) |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +export function injectSynth(messages: any[], instruction: string): boolean { |
| 99 | + // Find the last user message that is not ignored |
| 100 | + for (let i = messages.length - 1; i >= 0; i--) { |
| 101 | + const msg = messages[i] |
| 102 | + if (msg.role === 'user' && !isIgnoredUserMessage(msg)) { |
| 103 | + // Avoid double-injecting the same instruction |
| 104 | + if (typeof msg.content === 'string') { |
| 105 | + if (msg.content.includes(instruction)) { |
| 106 | + return false |
| 107 | + } |
| 108 | + msg.content = msg.content + '\n\n' + instruction |
| 109 | + } else if (Array.isArray(msg.content)) { |
| 110 | + const alreadyInjected = msg.content.some( |
| 111 | + (part: any) => part?.type === 'text' && typeof part.text === 'string' && part.text.includes(instruction) |
| 112 | + ) |
| 113 | + if (alreadyInjected) { |
| 114 | + return false |
| 115 | + } |
| 116 | + msg.content.push({ |
| 117 | + type: 'text', |
| 118 | + text: instruction |
| 119 | + }) |
| 120 | + } |
| 121 | + return true |
| 122 | + } |
| 123 | + } |
| 124 | + return false |
| 125 | +} |
0 commit comments