Skip to content

Commit 9830acc

Browse files
authored
perf(agents): prepare only consumed tool and skill diagnostics (#133512)
1 parent 796f893 commit 9830acc

2 files changed

Lines changed: 41 additions & 53 deletions

File tree

‎src/agents/agent-tools.before-tool-call.diagnostics.ts‎

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
resolveSkillTelemetrySource,
3636
resolveSkillTelemetrySourceValue,
3737
} from "../skills/loading/source.js";
38-
import type { SkillSnapshot, SkillTelemetrySource, SkillUsagePath } from "../skills/types.js";
38+
import type { SkillSnapshot, SkillTelemetrySource } from "../skills/types.js";
3939
import { isPlainObject, truncateUtf16Safe } from "../utils.js";
4040
import { buildAdjustedParamsKey } from "./agent-tools.before-tool-call.state.js";
4141
import type {
@@ -334,51 +334,31 @@ function resolveRelativeToolPath(candidate: string, ctx?: HookContext): string |
334334
return base ? path.resolve(base, trimmed) : undefined;
335335
}
336336

337-
function readToolPathCandidates(params: unknown, ctx?: HookContext): string[] {
338-
if (!isPlainObject(params)) {
339-
return [];
340-
}
341-
const candidates = typeof params.path === "string" ? [params.path] : [];
342-
return candidates
343-
.map((candidate) => resolveRelativeToolPath(normalizeFileToolPathParam(candidate), ctx))
344-
.filter((candidate): candidate is string => Boolean(candidate));
337+
function readToolPathCandidate(params: unknown, ctx?: HookContext): string | undefined {
338+
return isPlainObject(params) && typeof params.path === "string"
339+
? resolveRelativeToolPath(normalizeFileToolPathParam(params.path), ctx)
340+
: undefined;
345341
}
346342

347-
function skillInstructionPaths(snapshot: SkillSnapshot | undefined): Map<string, SkillUsageMatch> {
348-
const matches = new Map<string, SkillUsageMatch>();
349-
for (const skill of snapshot?.resolvedSkills ?? []) {
350-
const skillName = typeof skill.name === "string" ? skill.name.trim() : "";
351-
if (!skillName) {
352-
continue;
353-
}
354-
const match = resolvedSkillUsageMatch({ activation: "read", skill });
355-
const filePath = typeof skill.filePath === "string" ? skill.filePath.trim() : "";
356-
if (filePath) {
357-
if (filePath.startsWith("node://")) {
358-
matches.set(filePath, match);
359-
} else if (path.isAbsolute(filePath)) {
360-
matches.set(path.resolve(filePath), match);
361-
}
343+
function findSkillInstructionMatch(
344+
snapshot: SkillSnapshot,
345+
candidate: string,
346+
): SkillUsageMatch | undefined {
347+
const skill = snapshot.resolvedSkills?.findLast((entry) => {
348+
if (typeof entry.name !== "string" || !entry.name.trim()) {
349+
return false;
362350
}
363-
const baseDir = typeof skill.baseDir === "string" ? skill.baseDir.trim() : "";
364-
if (baseDir && path.isAbsolute(baseDir)) {
365-
matches.set(path.resolve(baseDir, "SKILL.md"), match);
366-
}
367-
}
368-
return matches;
369-
}
370-
371-
function materializedSkillInstructionPaths(paths: SkillUsagePath[] | undefined) {
372-
const matches = new Map<string, SkillUsageMatch>();
373-
for (const entry of paths ?? []) {
374-
matches.set(path.resolve(entry.readPath), {
375-
skillFile: entry.skillFile,
376-
skillName: entry.skillName,
377-
skillSource: entry.skillSource,
378-
activation: "read",
379-
});
380-
}
381-
return matches;
351+
const filePath = typeof entry.filePath === "string" ? entry.filePath.trim() : "";
352+
const baseDir = typeof entry.baseDir === "string" ? entry.baseDir.trim() : "";
353+
return (
354+
(filePath &&
355+
(filePath.startsWith("node://")
356+
? filePath === candidate
357+
: path.isAbsolute(filePath) && path.resolve(filePath) === candidate)) ||
358+
(baseDir && path.isAbsolute(baseDir) && path.resolve(baseDir, "SKILL.md") === candidate)
359+
);
360+
});
361+
return skill ? resolvedSkillUsageMatch({ activation: "read", skill }) : undefined;
382362
}
383363

384364
export function findSkillUsageMatch(params: {
@@ -410,16 +390,24 @@ export function findSkillUsageMatch(params: {
410390
if (params.toolName !== "read") {
411391
return undefined;
412392
}
413-
const skillPaths = params.ctx?.skillsSnapshot?.resolvedSkills?.length
414-
? skillInstructionPaths(params.ctx.skillsSnapshot)
415-
: materializedSkillInstructionPaths(params.ctx?.skillUsagePaths);
416-
for (const candidate of readToolPathCandidates(params.toolParams, params.ctx)) {
417-
const match = skillPaths.get(candidate);
418-
if (match) {
419-
return match;
420-
}
393+
const candidate = readToolPathCandidate(params.toolParams, params.ctx);
394+
if (!candidate) {
395+
return undefined;
421396
}
422-
return undefined;
397+
if (params.ctx?.skillsSnapshot?.resolvedSkills?.length) {
398+
return findSkillInstructionMatch(params.ctx.skillsSnapshot, candidate);
399+
}
400+
const match = params.ctx?.skillUsagePaths?.findLast(
401+
(entry) => path.resolve(entry.readPath) === candidate,
402+
);
403+
return match
404+
? {
405+
skillFile: match.skillFile,
406+
skillName: match.skillName,
407+
skillSource: match.skillSource,
408+
activation: "read",
409+
}
410+
: undefined;
423411
}
424412

425413
export function emitSkillUsedDiagnostic(params: {

‎src/agents/embedded-agent-subscribe.handlers.tools.results.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,6 @@ export async function emitToolResultOutput(params: {
676676
return;
677677
}
678678

679-
const outputText = extractToolResultText(sanitizedResult);
680679
const mediaReply = isToolError ? undefined : extractToolResultMediaArtifact(result);
681680
const mediaUrls = mediaReply
682681
? filterToolResultMediaUrls(
@@ -695,6 +694,7 @@ export async function emitToolResultOutput(params: {
695694
builtinToolNames: ctx.builtinToolNames,
696695
}) && ctx.shouldEmitToolOutput();
697696
if (shouldEmitOutput) {
697+
const outputText = extractToolResultText(sanitizedResult);
698698
if (outputText) {
699699
ctx.emitToolOutput(rawToolName, meta, outputText, hasStructuredMedia ? undefined : result);
700700
}

0 commit comments

Comments
 (0)