Skip to content

Commit 31cd1d9

Browse files
committed
refactor: consolidate message utilities and eliminate duplicate iteration
1 parent 84cc8e3 commit 31cd1d9

File tree

6 files changed

+46
-49
lines changed

6 files changed

+46
-49
lines changed

lib/messages/prune.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { SessionState, WithParts } from "../state"
22
import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
4-
import { buildToolIdList } from "../utils"
5-
import { getLastUserMessage, extractParameterKey } from "./utils"
4+
import { getLastUserMessage, extractParameterKey, buildToolIdList } from "./utils"
65
import { loadPrompt } from "../prompt"
76

87
const PRUNED_TOOL_OUTPUT_REPLACEMENT = '[Output removed to save context - information superseded or no longer needed]'

lib/messages/utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,42 @@ export const getLastUserMessage = (
8383
}
8484
return null
8585
}
86+
87+
/**
88+
* Finds the current agent from messages by scanning backward for user messages.
89+
*/
90+
export function findCurrentAgent(messages: WithParts[]): string | undefined {
91+
const userMsg = getLastUserMessage(messages)
92+
if (!userMsg) return undefined
93+
return (userMsg.info as any).agent || 'build'
94+
}
95+
96+
/**
97+
* Builds a list of tool call IDs from messages.
98+
*/
99+
export function buildToolIdList(messages: WithParts[]): string[] {
100+
const toolIds: string[] = []
101+
for (const msg of messages) {
102+
if (msg.parts) {
103+
for (const part of msg.parts) {
104+
if (part.type === 'tool' && part.callID && part.tool) {
105+
toolIds.push(part.callID)
106+
}
107+
}
108+
}
109+
}
110+
return toolIds
111+
}
112+
113+
/**
114+
* Prunes numeric tool IDs to valid tool call IDs based on the provided tool ID list.
115+
*/
116+
export function getPruneToolIds(numericToolIds: number[], toolIdList: string[]): string[] {
117+
const pruneToolIds: string[] = []
118+
for (const index of numericToolIds) {
119+
if (!isNaN(index) && index >= 0 && index < toolIdList.length) {
120+
pruneToolIds.push(toolIdList[index])
121+
}
122+
}
123+
return pruneToolIds
124+
}

lib/strategies/deduplication.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { PluginConfig } from "../config"
22
import { Logger } from "../logger"
33
import type { SessionState, WithParts } from "../state"
4-
import { buildToolIdList, calculateTokensSaved } from "../utils"
4+
import { calculateTokensSaved } from "../utils"
5+
import { buildToolIdList } from "../messages/utils"
56

67
/**
78
* Deduplication strategy - prunes older tool calls that have identical

lib/strategies/on-idle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { Logger } from "../logger"
44
import type { PluginConfig } from "../config"
55
import { buildAnalysisPrompt } from "../prompt"
66
import { selectModel, extractModelFromSession, ModelInfo } from "../model-selector"
7-
import { calculateTokensSaved, findCurrentAgent } from "../utils"
7+
import { calculateTokensSaved } from "../utils"
8+
import { findCurrentAgent } from "../messages/utils"
89
import { saveSessionState } from "../state/persistence"
910
import { sendUnifiedNotification } from "../ui/notification"
1011

lib/strategies/prune-tool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { tool } from "@opencode-ai/plugin"
22
import type { SessionState, ToolParameterEntry, WithParts } from "../state"
33
import type { PluginConfig } from "../config"
4-
import { findCurrentAgent, buildToolIdList, getPruneToolIds, calculateTokensSaved } from "../utils"
4+
import { findCurrentAgent, buildToolIdList, getPruneToolIds } from "../messages/utils"
5+
import { calculateTokensSaved } from "../utils"
56
import { PruneReason, sendUnifiedNotification } from "../ui/notification"
67
import { formatPruningResultForTool } from "../ui/display-utils"
78
import { ensureSessionInitialized } from "../state"

lib/utils.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -68,47 +68,3 @@ export async function isSubAgentSession(client: any, sessionID: string): Promise
6868
return false
6969
}
7070
}
71-
72-
/**
73-
* Finds the current agent from messages by scanning backward for user messages.
74-
*/
75-
export function findCurrentAgent(messages: any[]): string | undefined {
76-
for (let i = messages.length - 1; i >= 0; i--) {
77-
const msg = messages[i]
78-
const info = msg.info
79-
if (info?.role === 'user') {
80-
return info.agent || 'build'
81-
}
82-
}
83-
return undefined
84-
}
85-
86-
/**
87-
* Builds a list of tool call IDs from messages.
88-
*/
89-
export function buildToolIdList(messages: WithParts[]): string[] {
90-
const toolIds: string[] = []
91-
for (const msg of messages) {
92-
if (msg.parts) {
93-
for (const part of msg.parts) {
94-
if (part.type === 'tool' && part.callID && part.tool) {
95-
toolIds.push(part.callID)
96-
}
97-
}
98-
}
99-
}
100-
return toolIds
101-
}
102-
103-
/**
104-
* Prunes numeric tool IDs to valid tool call IDs based on the provided tool ID list.
105-
*/
106-
export function getPruneToolIds(numericToolIds: number[], toolIdList: string[]): string[] {
107-
const pruneToolIds: string[] = []
108-
for (const index of numericToolIds) {
109-
if (!isNaN(index) && index >= 0 && index < toolIdList.length) {
110-
pruneToolIds.push(toolIdList[index])
111-
}
112-
}
113-
return pruneToolIds
114-
}

0 commit comments

Comments
 (0)