Skip to content

Commit b7f46bb

Browse files
committed
refactor: deduplicate findCurrentAgent into display-utils
1 parent 373a00a commit b7f46bb

3 files changed

Lines changed: 18 additions & 23 deletions

File tree

lib/core/janitor.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
sendUnifiedNotification,
1212
type NotificationContext
1313
} from "../ui/notification"
14+
import { findCurrentAgent } from "../ui/display-utils"
1415

1516
export interface SessionStats {
1617
totalToolsPruned: number
@@ -437,16 +438,7 @@ export function parseMessages(
437438
return { toolCallIds, toolOutputs, toolMetadata }
438439
}
439440

440-
function findCurrentAgent(messages: any[]): string | undefined {
441-
for (let i = messages.length - 1; i >= 0; i--) {
442-
const msg = messages[i]
443-
const info = msg.info
444-
if (info?.role === 'user') {
445-
return info.agent || 'build'
446-
}
447-
}
448-
return undefined
449-
}
441+
450442

451443
// ============================================================================
452444
// Helpers

lib/pruning-tool.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { resetToolTrackerCount } from "./fetch-wrapper/tool-tracker"
66
import { isSubagentSession } from "./hooks"
77
import { getActualId } from "./state/id-mapping"
88
import { formatPruningResultForTool, sendUnifiedNotification, type NotificationContext } from "./ui/notification"
9+
import { findCurrentAgent } from "./ui/display-utils"
910
import { ensureSessionRestored } from "./state"
1011
import { saveSessionState } from "./state/persistence"
1112
import type { Logger } from "./logger"
@@ -136,19 +137,6 @@ export function createPruningTool(
136137
})
137138
}
138139

139-
/**
140-
* Finds the current agent from messages (same logic as janitor.ts).
141-
*/
142-
function findCurrentAgent(messages: any[]): string | undefined {
143-
for (let i = messages.length - 1; i >= 0; i--) {
144-
const msg = messages[i]
145-
const info = msg.info
146-
if (info?.role === 'user') {
147-
return info.agent || 'build'
148-
}
149-
}
150-
return undefined
151-
}
152140

153141
/**
154142
* Calculates approximate tokens saved by pruning the given tool call IDs.

lib/ui/display-utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,18 @@ export function extractParameterKey(metadata: { tool: string, parameters?: any }
7171
}
7272
return paramStr.substring(0, 50)
7373
}
74+
75+
/**
76+
* Finds the current agent from messages by looking for the most recent user message.
77+
* Used by janitor and pruning-tool to determine notification context.
78+
*/
79+
export function findCurrentAgent(messages: any[]): string | undefined {
80+
for (let i = messages.length - 1; i >= 0; i--) {
81+
const msg = messages[i]
82+
const info = msg.info
83+
if (info?.role === 'user') {
84+
return info.agent || 'build'
85+
}
86+
}
87+
return undefined
88+
}

0 commit comments

Comments
 (0)