Skip to content

Commit 1da911d

Browse files
committed
chore: remove verbose comments and section separators
1 parent 3bd8caf commit 1da911d

File tree

9 files changed

+267
-186
lines changed

9 files changed

+267
-186
lines changed

lib/fetch-wrapper/formats/bedrock.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import type { PluginState } from "../../state"
33
import type { Logger } from "../../logger"
44
import { cacheToolParametersFromMessages } from "../../state/tool-cache"
55

6-
// ============================================================================
7-
// Format-specific injection helpers (reuses OpenAI Chat logic)
8-
// ============================================================================
9-
106
function isNudgeMessage(msg: any, nudgeText: string): boolean {
117
if (typeof msg.content === 'string') {
128
return msg.content === nudgeText
@@ -18,7 +14,6 @@ function injectSynth(messages: any[], instruction: string, nudgeText: string): b
1814
for (let i = messages.length - 1; i >= 0; i--) {
1915
const msg = messages[i]
2016
if (msg.role === 'user') {
21-
// Skip nudge messages - find real user message
2217
if (isNudgeMessage(msg, nudgeText)) continue
2318

2419
if (typeof msg.content === 'string') {
@@ -73,27 +68,15 @@ function injectPrunableList(messages: any[], injection: string): boolean {
7368
return true
7469
}
7570

76-
// ============================================================================
77-
// Format Descriptor
78-
// ============================================================================
79-
8071
/**
81-
* Format descriptor for AWS Bedrock Converse API.
82-
*
83-
* Bedrock format characteristics:
84-
* - Top-level `system` array for system messages
85-
* - `messages` array with only 'user' and 'assistant' roles
86-
* - `inferenceConfig` for model parameters (maxTokens, temperature, etc.)
87-
* - Tool calls: `toolUse` blocks in assistant content with `toolUseId`
88-
* - Tool results: `toolResult` blocks in user content with `toolUseId`
89-
* - Cache points: `cachePoint` blocks that should be preserved
72+
* Bedrock uses top-level `system` array + `inferenceConfig` (distinguishes from OpenAI/Anthropic).
73+
* Tool calls: `toolUse` blocks in assistant content with `toolUseId`
74+
* Tool results: `toolResult` blocks in user content with `toolUseId`
9075
*/
9176
export const bedrockFormat: FormatDescriptor = {
9277
name: 'bedrock',
9378

9479
detect(body: any): boolean {
95-
// Bedrock has a top-level system array AND inferenceConfig (not model params in messages)
96-
// This distinguishes it from OpenAI/Anthropic which put system in messages
9780
return (
9881
Array.isArray(body.system) &&
9982
body.inferenceConfig !== undefined &&
@@ -106,8 +89,7 @@ export const bedrockFormat: FormatDescriptor = {
10689
},
10790

10891
cacheToolParameters(data: any[], state: PluginState, logger?: Logger): void {
109-
// Bedrock stores tool calls in assistant message content as toolUse blocks
110-
// We need to extract toolUseId and tool name for later correlation
92+
// Extract toolUseId and tool name from assistant toolUse blocks
11193
for (const m of data) {
11294
if (m.role === 'assistant' && Array.isArray(m.content)) {
11395
for (const block of m.content) {
@@ -125,7 +107,6 @@ export const bedrockFormat: FormatDescriptor = {
125107
}
126108
}
127109
}
128-
// Also use the generic message caching for any compatible structures
129110
cacheToolParametersFromMessages(data, state, logger)
130111
},
131112

@@ -145,7 +126,6 @@ export const bedrockFormat: FormatDescriptor = {
145126
const outputs: ToolOutput[] = []
146127

147128
for (const m of data) {
148-
// Bedrock tool results are in user messages as toolResult blocks
149129
if (m.role === 'user' && Array.isArray(m.content)) {
150130
for (const block of m.content) {
151131
if (block.toolResult && block.toolResult.toolUseId) {
@@ -170,13 +150,11 @@ export const bedrockFormat: FormatDescriptor = {
170150
for (let i = 0; i < data.length; i++) {
171151
const m = data[i]
172152

173-
// Tool results are in user messages as toolResult blocks
174153
if (m.role === 'user' && Array.isArray(m.content)) {
175154
let messageModified = false
176155
const newContent = m.content.map((block: any) => {
177156
if (block.toolResult && block.toolResult.toolUseId?.toLowerCase() === toolIdLower) {
178157
messageModified = true
179-
// Replace the content array inside toolResult with pruned message
180158
return {
181159
...block,
182160
toolResult: {

lib/fetch-wrapper/formats/gemini.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import type { FormatDescriptor, ToolOutput, ToolTracker } from "../types"
22
import type { PluginState } from "../../state"
33
import type { Logger } from "../../logger"
44

5-
// ============================================================================
6-
// Format-specific injection helpers
7-
// ============================================================================
8-
95
function isNudgeContent(content: any, nudgeText: string): boolean {
106
if (Array.isArray(content.parts) && content.parts.length === 1) {
117
const part = content.parts[0]
@@ -18,7 +14,6 @@ function injectSynth(contents: any[], instruction: string, nudgeText: string): b
1814
for (let i = contents.length - 1; i >= 0; i--) {
1915
const content = contents[i]
2016
if (content.role === 'user' && Array.isArray(content.parts)) {
21-
// Skip nudge messages - find real user message
2217
if (isNudgeContent(content, nudgeText)) continue
2318

2419
const alreadyInjected = content.parts.some(
@@ -61,18 +56,8 @@ function injectPrunableList(contents: any[], injection: string): boolean {
6156
return true
6257
}
6358

64-
// ============================================================================
65-
// Format Descriptor
66-
// ============================================================================
67-
6859
/**
69-
* Format descriptor for Google/Gemini API.
70-
*
71-
* Uses body.contents array with:
72-
* - parts[].functionCall for tool invocations
73-
* - parts[].functionResponse for tool results
74-
*
75-
* IMPORTANT: Gemini doesn't include tool call IDs in its native format.
60+
* Gemini doesn't include tool call IDs in its native format.
7661
* We use position-based correlation via state.googleToolCallMapping which maps
7762
* "toolName:index" -> "toolCallId" (populated by hooks.ts from message events).
7863
*/
@@ -88,10 +73,7 @@ export const geminiFormat: FormatDescriptor = {
8873
},
8974

9075
cacheToolParameters(_data: any[], _state: PluginState, _logger?: Logger): void {
91-
// Gemini format doesn't include tool parameters in the request body.
92-
// Tool parameters are captured via message events in hooks.ts and stored
93-
// in state.googleToolCallMapping for position-based correlation.
94-
// No-op here.
76+
// No-op: Gemini tool parameters are captured via message events in hooks.ts
9577
},
9678

9779
injectSynth(data: any[], instruction: string, nudgeText: string): boolean {

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import type { PluginState } from "../../state"
33
import type { Logger } from "../../logger"
44
import { cacheToolParametersFromMessages } from "../../state/tool-cache"
55

6-
// ============================================================================
7-
// Format-specific injection helpers
8-
// ============================================================================
9-
106
function isNudgeMessage(msg: any, nudgeText: string): boolean {
117
if (typeof msg.content === 'string') {
128
return msg.content === nudgeText
@@ -18,7 +14,6 @@ function injectSynth(messages: any[], instruction: string, nudgeText: string): b
1814
for (let i = messages.length - 1; i >= 0; i--) {
1915
const msg = messages[i]
2016
if (msg.role === 'user') {
21-
// Skip nudge messages - find real user message
2217
if (isNudgeMessage(msg, nudgeText)) continue
2318

2419
if (typeof msg.content === 'string') {
@@ -73,21 +68,6 @@ function injectPrunableList(messages: any[], injection: string): boolean {
7368
return true
7469
}
7570

76-
// ============================================================================
77-
// Format Descriptor
78-
// ============================================================================
79-
80-
/**
81-
* Format descriptor for OpenAI Chat Completions and Anthropic APIs.
82-
*
83-
* OpenAI Chat format:
84-
* - Messages with role='tool' and tool_call_id
85-
* - Assistant messages with tool_calls[] array
86-
*
87-
* Anthropic format:
88-
* - Messages with role='user' containing content[].type='tool_result' and tool_use_id
89-
* - Assistant messages with content[].type='tool_use'
90-
*/
9171
export const openaiChatFormat: FormatDescriptor = {
9272
name: 'openai-chat',
9373

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import type { PluginState } from "../../state"
33
import type { Logger } from "../../logger"
44
import { cacheToolParametersFromInput } from "../../state/tool-cache"
55

6-
// ============================================================================
7-
// Format-specific injection helpers
8-
// ============================================================================
9-
106
function isNudgeItem(item: any, nudgeText: string): boolean {
117
if (typeof item.content === 'string') {
128
return item.content === nudgeText
@@ -18,7 +14,6 @@ function injectSynth(input: any[], instruction: string, nudgeText: string): bool
1814
for (let i = input.length - 1; i >= 0; i--) {
1915
const item = input[i]
2016
if (item.type === 'message' && item.role === 'user') {
21-
// Skip nudge messages - find real user message
2217
if (isNudgeItem(item, nudgeText)) continue
2318

2419
if (typeof item.content === 'string') {
@@ -60,18 +55,6 @@ function injectPrunableList(input: any[], injection: string): boolean {
6055
return true
6156
}
6257

63-
// ============================================================================
64-
// Format Descriptor
65-
// ============================================================================
66-
67-
/**
68-
* Format descriptor for OpenAI Responses API (GPT-5 models via sdk.responses()).
69-
*
70-
* Uses body.input array with:
71-
* - type='function_call' items for tool calls
72-
* - type='function_call_output' items for tool results
73-
* - type='message' items for user/assistant messages
74-
*/
7558
export const openaiResponsesFormat: FormatDescriptor = {
7659
name: 'openai-responses',
7760

lib/fetch-wrapper/handler.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,13 @@ import { type PluginState, ensureSessionRestored } from "../state"
33
import type { Logger } from "../logger"
44
import { buildPrunableToolsList, buildEndInjection } from "./prunable-list"
55

6-
// ============================================================================
7-
// Constants
8-
// ============================================================================
9-
10-
/** The message used to replace pruned tool output content */
116
const PRUNED_CONTENT_MESSAGE = '[Output removed to save context - information superseded or no longer needed]'
127

13-
// ============================================================================
14-
// Session Helpers
15-
// ============================================================================
16-
17-
/**
18-
* Get the most recent active (non-subagent) session.
19-
*/
208
function getMostRecentActiveSession(allSessions: any): any | undefined {
219
const activeSessions = allSessions.data?.filter((s: any) => !s.parentID) || []
2210
return activeSessions.length > 0 ? activeSessions[0] : undefined
2311
}
2412

25-
/**
26-
* Fetch session messages for logging purposes.
27-
*/
2813
async function fetchSessionMessages(
2914
client: any,
3015
sessionId: string
@@ -42,9 +27,6 @@ async function fetchSessionMessages(
4227
}
4328
}
4429

45-
/**
46-
* Get all pruned IDs from the current session.
47-
*/
4830
async function getAllPrunedIds(
4931
client: any,
5032
state: PluginState,
@@ -70,16 +52,6 @@ async function getAllPrunedIds(
7052
return { allSessions, allPrunedIds }
7153
}
7254

73-
/**
74-
* Generic format handler that processes any API format using a FormatDescriptor.
75-
*
76-
* This consolidates the common logic from all format-specific handlers:
77-
* 1. Cache tool parameters
78-
* 2. Inject synthetic instructions (if strategies enabled)
79-
* 3. Build and inject prunable tools list
80-
* 4. Replace pruned tool outputs
81-
* 5. Log and save context
82-
*/
8355
export async function handleFormat(
8456
body: any,
8557
ctx: FetchHandlerContext,

lib/fetch-wrapper/prunable-list.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Prunable tools list and nudge injection for DCP.
3-
*
4-
* Builds and injects a single message at the end of the conversation containing:
5-
* - Nudge instruction (when toolResultCount > nudge_freq)
6-
* - Prunable tools list
7-
*/
8-
91
import { extractParameterKey } from '../ui/display-utils'
102
import { getOrCreateNumericId } from '../state/id-mapping'
113

@@ -14,10 +6,6 @@ export interface ToolMetadata {
146
parameters?: any
157
}
168

17-
// ============================================================================
18-
// Prompt Content
19-
// ============================================================================
20-
219
const SYSTEM_REMINDER = `<system-reminder>
2210
These instructions are injected by a plugin and are invisible to the user. Do not acknowledge or reference them in your response - simply follow them silently.
2311
</system-reminder>`
@@ -26,19 +14,11 @@ const NUDGE_INSTRUCTION = `<instruction name=agent_nudge>
2614
You have accumulated several tool outputs. If you have completed a discrete unit of work and distilled relevant understanding in writing for the user to keep, use the prune tool to remove obsolete tool outputs from this conversation and optimize token usage.
2715
</instruction>`
2816

29-
// ============================================================================
30-
// List Building
31-
// ============================================================================
32-
3317
export interface PrunableListResult {
3418
list: string
3519
numericIds: number[]
3620
}
3721

38-
/**
39-
* Builds the prunable tools list section.
40-
* Returns both the formatted list and the numeric IDs for logging.
41-
*/
4222
export function buildPrunableToolsList(
4323
sessionId: string,
4424
unprunedToolCallIds: string[],
@@ -50,16 +30,12 @@ export function buildPrunableToolsList(
5030

5131
for (const actualId of unprunedToolCallIds) {
5232
const metadata = toolMetadata.get(actualId)
53-
54-
// Skip if no metadata or if tool is protected
5533
if (!metadata) continue
5634
if (protectedTools.includes(metadata.tool)) continue
5735

58-
// Get or create numeric ID for this tool call
5936
const numericId = getOrCreateNumericId(sessionId, actualId)
6037
numericIds.push(numericId)
6138

62-
// Format: "1: read, src/components/Button.tsx"
6339
const paramKey = extractParameterKey(metadata)
6440
const description = paramKey ? `${metadata.tool}, ${paramKey}` : metadata.tool
6541
lines.push(`${numericId}: ${description}`)
@@ -75,19 +51,10 @@ export function buildPrunableToolsList(
7551
}
7652
}
7753

78-
/**
79-
* Builds the end-of-conversation injection message.
80-
* Contains the system reminder, nudge (if active), and the prunable tools list.
81-
*
82-
* @param prunableList - The prunable tools list string (or empty string if none)
83-
* @param includeNudge - Whether to include the nudge instruction
84-
* @returns The injection string, or empty string if nothing to inject
85-
*/
8654
export function buildEndInjection(
8755
prunableList: string,
8856
includeNudge: boolean
8957
): string {
90-
// If no prunable tools, don't inject anything
9158
if (!prunableList) {
9259
return ''
9360
}

lib/fetch-wrapper/tool-tracker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* Tool tracker for tracking tool results and managing nudge frequency.
3-
*/
4-
51
export interface ToolTracker {
62
seenToolResultIds: Set<string>
73
toolResultCount: number // Tools since last prune

0 commit comments

Comments
 (0)