Skip to content

Commit e9c4b3d

Browse files
migrate listRepos & listCommits
1 parent 7927a84 commit e9c4b3d

File tree

12 files changed

+141
-94
lines changed

12 files changed

+141
-94
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import {
1515
import { randomUUID } from "crypto";
1616
import _dedent from "dedent";
1717
import { ANSWER_TAG, FILE_REFERENCE_PREFIX, toolNames } from "./constants";
18-
import { findSymbolDefinitionsTool, findSymbolReferencesTool, listCommitsTool, listReposTool, searchCodeTool } from "./tools";
18+
import { findSymbolDefinitionsTool, findSymbolReferencesTool, searchCodeTool } from "./tools";
1919
import { toVercelAITool } from "@/features/tools/adapters";
2020
import { readFileDefinition } from "@/features/tools/readFile";
21+
import { listCommitsDefinition } from "@/features/tools/listCommits";
22+
import { listReposDefinition } from "@/features/tools/listRepos";
2123
import { Source } from "./types";
2224
import { addLineNumbers, fileReferenceToString } from "./utils";
2325

@@ -205,8 +207,8 @@ const createAgentStream = async ({
205207
[toolNames.readFile]: toVercelAITool(readFileDefinition),
206208
[toolNames.findSymbolReferences]: findSymbolReferencesTool,
207209
[toolNames.findSymbolDefinitions]: findSymbolDefinitionsTool,
208-
[toolNames.listRepos]: listReposTool,
209-
[toolNames.listCommits]: listCommitsTool,
210+
[toolNames.listRepos]: toVercelAITool(listReposDefinition),
211+
[toolNames.listCommits]: toVercelAITool(listCommitsDefinition),
210212
},
211213
temperature: env.SOURCEBOT_CHAT_MODEL_TEMPERATURE,
212214
stopWhen: [

packages/web/src/features/chat/components/chatThread/tools/listCommitsToolComponent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { ListCommitsToolUIPart } from "@/features/chat/tools";
3+
import { ListCommitsToolUIPart } from "@/features/tools/registry";
44
import { isServiceError } from "@/lib/utils";
55
import { useMemo, useState } from "react";
66
import { ToolHeader, TreeList } from "./shared";
@@ -41,14 +41,14 @@ export const ListCommitsToolComponent = ({ part }: { part: ListCommitsToolUIPart
4141
</TreeList>
4242
) : (
4343
<>
44-
{part.output.commits.length === 0 ? (
44+
{part.output.metadata.commits.length === 0 ? (
4545
<span className="text-sm text-muted-foreground ml-[25px]">No commits found</span>
4646
) : (
4747
<TreeList>
4848
<div className="text-sm text-muted-foreground mb-2">
49-
Found {part.output.commits.length} of {part.output.totalCount} total commits:
49+
Found {part.output.metadata.commits.length} of {part.output.metadata.totalCount} total commits:
5050
</div>
51-
{part.output.commits.map((commit) => (
51+
{part.output.metadata.commits.map((commit) => (
5252
<div key={commit.hash} className="mb-3 last:mb-0">
5353
<div className="flex items-start gap-2 text-sm">
5454
<GitCommitVerticalIcon className="h-4 w-4 text-muted-foreground mt-0.5 flex-shrink-0" />

packages/web/src/features/chat/components/chatThread/tools/listReposToolComponent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { ListReposToolUIPart } from "@/features/chat/tools";
3+
import { ListReposToolUIPart } from "@/features/tools/registry";
44
import { isServiceError } from "@/lib/utils";
55
import { useMemo, useState } from "react";
66
import { ToolHeader, TreeList } from "./shared";
@@ -41,14 +41,14 @@ export const ListReposToolComponent = ({ part }: { part: ListReposToolUIPart })
4141
</TreeList>
4242
) : (
4343
<>
44-
{part.output.length === 0 ? (
44+
{part.output.metadata.repos.length === 0 ? (
4545
<span className="text-sm text-muted-foreground ml-[25px]">No repositories found</span>
4646
) : (
4747
<TreeList>
4848
<div className="text-sm text-muted-foreground mb-2">
49-
Found {part.output.length} repositories:
49+
Found {part.output.metadata.repos.length} repositories:
5050
</div>
51-
{part.output.map((repoName, index) => (
51+
{part.output.metadata.repos.map((repoName, index) => (
5252
<div key={index} className="flex items-center gap-2 text-sm">
5353
<FolderOpenIcon className="h-4 w-4 text-muted-foreground" />
5454
<span className="truncate">{repoName}</span>

packages/web/src/features/chat/tools/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
export * from "./findSymbolReferences";
1212
export * from "./findSymbolDefinitions";
1313
export * from "./searchCode";
14-
export * from "./listRepos";
15-
export * from "./listCommits";

packages/web/src/features/chat/tools/listCommits.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/web/src/features/chat/tools/listRepos.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BaseEditor, Descendant } from "slate";
33
import { HistoryEditor } from "slate-history";
44
import { ReactEditor, RenderElementProps } from "slate-react";
55
import { z } from "zod";
6-
import { FindSymbolDefinitionsTool, FindSymbolReferencesTool, SearchCodeTool, ListReposTool, ListCommitsTool } from "./tools";
6+
import { FindSymbolDefinitionsTool, FindSymbolReferencesTool, SearchCodeTool } from "./tools";
77
import { toolNames } from "./constants";
88
import { ToolTypes } from "@/features/tools/registry";
99
import { LanguageModel } from "@sourcebot/schemas/v3/index.type";
@@ -84,8 +84,8 @@ export type SBChatMessageToolTypes = {
8484
[toolNames.readFile]: ToolTypes['readFile'],
8585
[toolNames.findSymbolReferences]: FindSymbolReferencesTool,
8686
[toolNames.findSymbolDefinitions]: FindSymbolDefinitionsTool,
87-
[toolNames.listRepos]: ListReposTool,
88-
[toolNames.listCommits]: ListCommitsTool,
87+
[toolNames.listRepos]: ToolTypes['listRepos'],
88+
[toolNames.listCommits]: ToolTypes['listCommits'],
8989
}
9090

9191
export type SBChatMessageDataParts = {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { z } from "zod";
2+
import { isServiceError } from "@/lib/utils";
3+
import { listCommits } from "@/features/git";
4+
import { createLogger } from "@sourcebot/shared";
5+
import { ToolDefinition } from "./types";
6+
import description from "./listCommits.txt";
7+
8+
const logger = createLogger('tool-listCommits');
9+
10+
const listCommitsShape = {
11+
repository: z.string().describe("The repository to list commits from"),
12+
query: z.string().describe("Search query to filter commits by message (case-insensitive)").optional(),
13+
since: z.string().describe("Start date for commit range (e.g., '30 days ago', '2024-01-01', 'last week')").optional(),
14+
until: z.string().describe("End date for commit range (e.g., 'yesterday', '2024-12-31', 'today')").optional(),
15+
author: z.string().describe("Filter commits by author name or email (case-insensitive)").optional(),
16+
maxCount: z.number().describe("Maximum number of commits to return (default: 50)").optional(),
17+
};
18+
19+
export type Commit = {
20+
hash: string;
21+
date: string;
22+
message: string;
23+
author: string;
24+
refs: string;
25+
};
26+
27+
export type ListCommitsMetadata = {
28+
commits: Commit[];
29+
totalCount: number;
30+
};
31+
32+
export const listCommitsDefinition: ToolDefinition<"listCommits", typeof listCommitsShape, ListCommitsMetadata> = {
33+
name: "listCommits",
34+
description,
35+
inputSchema: z.object(listCommitsShape),
36+
execute: async ({ repository, query, since, until, author, maxCount }) => {
37+
logger.debug('listCommits', { repository, query, since, until, author, maxCount });
38+
const response = await listCommits({
39+
repo: repository,
40+
query,
41+
since,
42+
until,
43+
author,
44+
maxCount,
45+
});
46+
47+
if (isServiceError(response)) {
48+
throw new Error(response.message);
49+
}
50+
51+
const commits: Commit[] = response.commits.map((commit) => ({
52+
hash: commit.hash,
53+
date: commit.date,
54+
message: commit.message,
55+
author: `${commit.author_name} <${commit.author_email}>`,
56+
refs: commit.refs,
57+
}));
58+
59+
const metadata: ListCommitsMetadata = {
60+
commits,
61+
totalCount: response.totalCount,
62+
};
63+
64+
return {
65+
output: JSON.stringify(metadata),
66+
metadata,
67+
};
68+
},
69+
};
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { z } from "zod";
2+
import { isServiceError } from "@/lib/utils";
3+
import { listRepos } from "@/app/api/(server)/repos/listReposApi";
4+
import { ToolDefinition } from "./types";
5+
import description from './listRepos.txt';
6+
7+
const listReposShape = {
8+
page: z.coerce.number().int().positive().default(1).describe("Page number for pagination"),
9+
perPage: z.coerce.number().int().positive().max(100).default(30).describe("Number of repositories per page (max 100)"),
10+
sort: z.enum(['name', 'pushed']).default('name').describe("Sort repositories by name or last pushed date"),
11+
direction: z.enum(['asc', 'desc']).default('asc').describe("Sort direction"),
12+
query: z.string().optional().describe("Filter repositories by name"),
13+
};
14+
15+
type ListReposMetadata = {
16+
repos: string[];
17+
};
18+
19+
export const listReposDefinition: ToolDefinition<
20+
'listRepos',
21+
typeof listReposShape,
22+
ListReposMetadata
23+
> = {
24+
name: 'listRepos',
25+
description,
26+
inputSchema: z.object(listReposShape),
27+
execute: async ({ page, perPage, sort, direction, query }) => {
28+
const reposResponse = await listRepos({
29+
page,
30+
perPage,
31+
sort,
32+
direction,
33+
query,
34+
});
35+
36+
if (isServiceError(reposResponse)) {
37+
throw new Error(reposResponse.message);
38+
}
39+
40+
const repos = reposResponse.data.map((repo) => repo.repoName);
41+
const metadata: ListReposMetadata = { repos };
42+
43+
return {
44+
output: JSON.stringify(metadata),
45+
metadata,
46+
};
47+
},
48+
};

0 commit comments

Comments
 (0)