Skip to content

Commit f8895f2

Browse files
committed
refactor: consolidate trackNewToolResults to OpenCode API
- Move tool result tracking from format-specific parsing to syncToolParametersFromOpenCode() - Remove trackNewToolResults from all 4 format descriptors (~100 lines) - Remove getToolName callback from ToolTracker interface - Remove trackNewToolResults from FormatDescriptor interface
1 parent c14ca14 commit f8895f2

File tree

7 files changed

+4
-128
lines changed

7 files changed

+4
-128
lines changed

index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ const plugin: Plugin = (async (ctx) => {
4343
// Create tool tracker and load prompts for synthetic instruction injection
4444
const toolTracker = createToolTracker()
4545

46-
// Wire up tool name lookup from the cached tool parameters
47-
toolTracker.getToolName = (callId: string) => {
48-
const entry = state.toolParameters.get(callId.toLowerCase())
49-
return entry?.tool
50-
}
51-
5246
const prompts = {
5347
synthInstruction: loadPrompt("synthetic"),
5448
nudgeInstruction: loadPrompt("nudge")

lib/fetch-wrapper/formats/bedrock.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FormatDescriptor, ToolOutput, ToolTracker } from "../types"
1+
import type { FormatDescriptor, ToolOutput } from "../types"
22
import type { PluginState } from "../../state"
33

44
function isNudgeMessage(msg: any, nudgeText: string): boolean {
@@ -30,36 +30,6 @@ function injectSynth(messages: any[], instruction: string, nudgeText: string): b
3030
return false
3131
}
3232

33-
function trackNewToolResults(messages: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
34-
let newCount = 0
35-
for (const m of messages) {
36-
if (m.role === 'tool' && m.tool_call_id) {
37-
if (!tracker.seenToolResultIds.has(m.tool_call_id)) {
38-
tracker.seenToolResultIds.add(m.tool_call_id)
39-
const toolName = tracker.getToolName?.(m.tool_call_id)
40-
if (!toolName || !protectedTools.has(toolName)) {
41-
tracker.toolResultCount++
42-
newCount++
43-
}
44-
}
45-
} else if (m.role === 'user' && Array.isArray(m.content)) {
46-
for (const part of m.content) {
47-
if (part.type === 'tool_result' && part.tool_use_id) {
48-
if (!tracker.seenToolResultIds.has(part.tool_use_id)) {
49-
tracker.seenToolResultIds.add(part.tool_use_id)
50-
const toolName = tracker.getToolName?.(part.tool_use_id)
51-
if (!toolName || !protectedTools.has(toolName)) {
52-
tracker.toolResultCount++
53-
newCount++
54-
}
55-
}
56-
}
57-
}
58-
}
59-
}
60-
return newCount
61-
}
62-
6333
function injectPrunableList(messages: any[], injection: string): boolean {
6434
if (!injection) return false
6535
messages.push({ role: 'user', content: injection })
@@ -90,10 +60,6 @@ export const bedrockFormat: FormatDescriptor = {
9060
return injectSynth(data, instruction, nudgeText)
9161
},
9262

93-
trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
94-
return trackNewToolResults(data, tracker, protectedTools)
95-
},
96-
9763
injectPrunableList(data: any[], injection: string): boolean {
9864
return injectPrunableList(data, injection)
9965
},

lib/fetch-wrapper/formats/gemini.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FormatDescriptor, ToolOutput, ToolTracker } from "../types"
1+
import type { FormatDescriptor, ToolOutput } from "../types"
22
import type { PluginState } from "../../state"
33

44
function isNudgeContent(content: any, nudgeText: string): boolean {
@@ -26,29 +26,6 @@ function injectSynth(contents: any[], instruction: string, nudgeText: string): b
2626
return false
2727
}
2828

29-
function trackNewToolResults(contents: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
30-
let newCount = 0
31-
let positionCounter = 0
32-
for (const content of contents) {
33-
if (!Array.isArray(content.parts)) continue
34-
for (const part of content.parts) {
35-
if (part.functionResponse) {
36-
const positionId = `gemini_pos_${positionCounter}`
37-
positionCounter++
38-
if (!tracker.seenToolResultIds.has(positionId)) {
39-
tracker.seenToolResultIds.add(positionId)
40-
const toolName = part.functionResponse.name
41-
if (!toolName || !protectedTools.has(toolName)) {
42-
tracker.toolResultCount++
43-
newCount++
44-
}
45-
}
46-
}
47-
}
48-
}
49-
return newCount
50-
}
51-
5229
function injectPrunableList(contents: any[], injection: string): boolean {
5330
if (!injection) return false
5431
contents.push({ role: 'user', parts: [{ text: injection }] })
@@ -75,10 +52,6 @@ export const geminiFormat: FormatDescriptor = {
7552
return injectSynth(data, instruction, nudgeText)
7653
},
7754

78-
trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
79-
return trackNewToolResults(data, tracker, protectedTools)
80-
},
81-
8255
injectPrunableList(data: any[], injection: string): boolean {
8356
return injectPrunableList(data, injection)
8457
},

lib/fetch-wrapper/formats/openai-chat.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FormatDescriptor, ToolOutput, ToolTracker } from "../types"
1+
import type { FormatDescriptor, ToolOutput } from "../types"
22
import type { PluginState } from "../../state"
33

44
function isNudgeMessage(msg: any, nudgeText: string): boolean {
@@ -30,36 +30,6 @@ function injectSynth(messages: any[], instruction: string, nudgeText: string): b
3030
return false
3131
}
3232

33-
function trackNewToolResults(messages: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
34-
let newCount = 0
35-
for (const m of messages) {
36-
if (m.role === 'tool' && m.tool_call_id) {
37-
if (!tracker.seenToolResultIds.has(m.tool_call_id)) {
38-
tracker.seenToolResultIds.add(m.tool_call_id)
39-
const toolName = tracker.getToolName?.(m.tool_call_id)
40-
if (!toolName || !protectedTools.has(toolName)) {
41-
tracker.toolResultCount++
42-
newCount++
43-
}
44-
}
45-
} else if (m.role === 'user' && Array.isArray(m.content)) {
46-
for (const part of m.content) {
47-
if (part.type === 'tool_result' && part.tool_use_id) {
48-
if (!tracker.seenToolResultIds.has(part.tool_use_id)) {
49-
tracker.seenToolResultIds.add(part.tool_use_id)
50-
const toolName = tracker.getToolName?.(part.tool_use_id)
51-
if (!toolName || !protectedTools.has(toolName)) {
52-
tracker.toolResultCount++
53-
newCount++
54-
}
55-
}
56-
}
57-
}
58-
}
59-
}
60-
return newCount
61-
}
62-
6333
function injectPrunableList(messages: any[], injection: string): boolean {
6434
if (!injection) return false
6535
messages.push({ role: 'user', content: injection })
@@ -81,10 +51,6 @@ export const openaiChatFormat: FormatDescriptor = {
8151
return injectSynth(data, instruction, nudgeText)
8252
},
8353

84-
trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
85-
return trackNewToolResults(data, tracker, protectedTools)
86-
},
87-
8854
injectPrunableList(data: any[], injection: string): boolean {
8955
return injectPrunableList(data, injection)
9056
},

lib/fetch-wrapper/formats/openai-responses.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FormatDescriptor, ToolOutput, ToolTracker } from "../types"
1+
import type { FormatDescriptor, ToolOutput } from "../types"
22
import type { PluginState } from "../../state"
33

44
function isNudgeItem(item: any, nudgeText: string): boolean {
@@ -30,23 +30,6 @@ function injectSynth(input: any[], instruction: string, nudgeText: string): bool
3030
return false
3131
}
3232

33-
function trackNewToolResults(input: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
34-
let newCount = 0
35-
for (const item of input) {
36-
if (item.type === 'function_call_output' && item.call_id) {
37-
if (!tracker.seenToolResultIds.has(item.call_id)) {
38-
tracker.seenToolResultIds.add(item.call_id)
39-
const toolName = tracker.getToolName?.(item.call_id)
40-
if (!toolName || !protectedTools.has(toolName)) {
41-
tracker.toolResultCount++
42-
newCount++
43-
}
44-
}
45-
}
46-
}
47-
return newCount
48-
}
49-
5033
function injectPrunableList(input: any[], injection: string): boolean {
5134
if (!injection) return false
5235
input.push({ type: 'message', role: 'user', content: injection })
@@ -68,10 +51,6 @@ export const openaiResponsesFormat: FormatDescriptor = {
6851
return injectSynth(data, instruction, nudgeText)
6952
},
7053

71-
trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number {
72-
return trackNewToolResults(data, tracker, protectedTools)
73-
},
74-
7554
injectPrunableList(data: any[], injection: string): boolean {
7655
return injectPrunableList(data, injection)
7756
},

lib/fetch-wrapper/tool-tracker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export interface ToolTracker {
22
seenToolResultIds: Set<string>
33
toolResultCount: number // Tools since last prune
44
skipNextIdle: boolean
5-
getToolName?: (callId: string) => string | undefined
65
}
76

87
export function createToolTracker(): ToolTracker {

lib/fetch-wrapper/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface FormatDescriptor {
1414
detect(body: any): boolean
1515
getDataArray(body: any): any[] | undefined
1616
injectSynth(data: any[], instruction: string, nudgeText: string): boolean
17-
trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number
1817
injectPrunableList(data: any[], injection: string): boolean
1918
extractToolOutputs(data: any[], state: PluginState): ToolOutput[]
2019
replaceToolOutput(data: any[], toolId: string, prunedMessage: string, state: PluginState): boolean

0 commit comments

Comments
 (0)