Skip to content

Commit 2e67a0d

Browse files
improve listTree output
1 parent dbd69a1 commit 2e67a0d

File tree

5 files changed

+66
-13
lines changed

5 files changed

+66
-13
lines changed

packages/web/src/features/chat/agent.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { grepDefinition } from "@/features/tools/grep";
2222
import { Source } from "./types";
2323
import { addLineNumbers, fileReferenceToString } from "./utils";
2424
import { tools } from "./tools";
25+
import { listTreeDefinition } from "../tools";
2526

2627
const dedent = _dedent.withOptions({ alignValues: true });
2728

@@ -223,27 +224,43 @@ const createAgentStream = async ({
223224
if (toolName === readFileDefinition.name) {
224225
onWriteSource({
225226
type: 'file',
226-
language: output.metadata.language,
227227
repo: output.metadata.repo,
228228
path: output.metadata.path,
229229
revision: output.metadata.revision,
230230
name: output.metadata.path.split('/').pop() ?? output.metadata.path,
231231
});
232232
} else if (toolName === grepDefinition.name) {
233233
output.metadata.files.forEach((file) => {
234-
onWriteSource(file);
234+
onWriteSource({
235+
type: 'file',
236+
repo: file.repo,
237+
path: file.path,
238+
revision: file.revision,
239+
name: file.path.split('/').pop() ?? file.path,
240+
});
235241
});
236242
} else if (toolName === findSymbolDefinitionsDefinition.name || toolName === findSymbolReferencesDefinition.name) {
237243
output.metadata.files.forEach((file) => {
238244
onWriteSource({
239245
type: 'file',
240-
language: file.language,
241246
repo: file.repo,
242247
path: file.fileName,
243248
revision: file.revision,
244249
name: file.fileName.split('/').pop() ?? file.fileName,
245250
});
246251
});
252+
} else if (toolName === listTreeDefinition.name) {
253+
output.metadata.entries
254+
.filter((entry) => entry.type === 'blob')
255+
.forEach((entry) => {
256+
onWriteSource({
257+
type: 'file',
258+
repo: output.metadata.repo,
259+
path: entry.path,
260+
revision: output.metadata.ref,
261+
name: entry.name,
262+
});
263+
});
247264
}
248265
});
249266
},

packages/web/src/features/chat/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const fileSourceSchema = z.object({
1111
repo: z.string(),
1212
path: z.string(),
1313
name: z.string(),
14-
language: z.string(),
1514
revision: z.string(),
1615
});
1716
export type FileSource = z.infer<typeof fileSourceSchema>;

packages/web/src/features/chat/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ export const createUIMessage = (text: string, mentions: MentionData[], selectedS
187187
path: mention.path,
188188
repo: mention.repo,
189189
name: mention.name,
190-
language: mention.language,
191190
revision: mention.revision,
192191
}
193192
return fileSource;

packages/web/src/features/tools/grep.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import escapeStringRegexp from "escape-string-regexp";
66
import { ToolDefinition } from "./types";
77
import { logger } from "./logger";
88
import description from "./grep.txt";
9-
import { FileSource } from "../chat/types";
109

1110
const DEFAULT_SEARCH_LIMIT = 100;
1211
const MAX_LINE_LENGTH = 2000;
@@ -44,8 +43,15 @@ const grepShape = {
4443
.optional(),
4544
};
4645

46+
export type GrepFile = {
47+
path: string;
48+
name: string;
49+
repo: string;
50+
revision: string;
51+
};
52+
4753
export type GrepMetadata = {
48-
files: FileSource[];
54+
files: GrepFile[];
4955
query: string;
5056
};
5157

@@ -102,13 +108,11 @@ export const grepDefinition: ToolDefinition<'grep', typeof grepShape, GrepMetada
102108

103109
const metadata: GrepMetadata = {
104110
files: response.files.map((file) => ({
105-
type: 'file',
106111
path: file.fileName.text,
107112
name: file.fileName.text.split('/').pop() ?? file.fileName.text,
108-
language: file.language,
109113
repo: file.repository,
110114
revision: ref ?? 'HEAD',
111-
})),
115+
} satisfies GrepFile)),
112116
query,
113117
};
114118

packages/web/src/features/tools/listTree.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ export const listTreeDefinition: ToolDefinition<'list_tree', typeof listTreeShap
5252

5353
if (!includeFiles && !includeDirectories) {
5454
const metadata: ListTreeMetadata = {
55-
repo, ref, path: normalizedPath,
55+
repo,
56+
ref,
57+
path: normalizedPath,
5658
entries: [],
5759
totalReturned: 0,
5860
truncated: false,
5961
};
60-
return { output: JSON.stringify(metadata), metadata };
62+
return { output: 'No entries found', metadata };
6163
}
6264

6365
const queue: Array<{ path: string; depth: number }> = [{ path: normalizedPath, depth: 0 }];
@@ -135,6 +137,38 @@ export const listTreeDefinition: ToolDefinition<'list_tree', typeof listTreeShap
135137
truncated,
136138
};
137139

138-
return { output: JSON.stringify(metadata), metadata };
140+
const outputLines = [normalizedPath || '/'];
141+
142+
const childrenByPath = new Map<string, ListTreeEntry[]>();
143+
for (const entry of sortedEntries) {
144+
const siblings = childrenByPath.get(entry.parentPath) ?? [];
145+
siblings.push(entry);
146+
childrenByPath.set(entry.parentPath, siblings);
147+
}
148+
149+
function renderEntries(parentPath: string) {
150+
const children = childrenByPath.get(parentPath) ?? [];
151+
for (const entry of children) {
152+
const indent = ' '.repeat(entry.depth);
153+
const label = entry.type === 'tree' ? `${entry.name}/` : entry.name;
154+
outputLines.push(`${indent}${label}`);
155+
if (entry.type === 'tree') {
156+
renderEntries(entry.path);
157+
}
158+
}
159+
}
160+
161+
renderEntries(normalizedPath);
162+
163+
if (sortedEntries.length === 0) {
164+
outputLines.push(' (no entries found)');
165+
}
166+
167+
if (truncated) {
168+
outputLines.push('');
169+
outputLines.push(`(truncated — showing first ${normalizedMaxEntries} entries)`);
170+
}
171+
172+
return { output: outputLines.join('\n'), metadata };
139173
},
140174
};

0 commit comments

Comments
 (0)