Skip to content

Commit bbbb982

Browse files
wip
1 parent 6ab9bc7 commit bbbb982

File tree

10 files changed

+42
-38
lines changed

10 files changed

+42
-38
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function toVercelAITool<TName extends string, TShape extends z.ZodRawShap
99
return tool({
1010
description: def.description,
1111
inputSchema: def.inputSchema,
12-
execute: def.execute,
12+
execute: (input) => def.execute(input, { source: 'sourcebot-ask-agent' }),
1313
toModelOutput: ({ output }) => ({
1414
type: "content",
1515
value: [{ type: "text", text: output.output }],
@@ -30,7 +30,7 @@ export function registerMcpTool<TName extends string, TShape extends z.ZodRawSha
3030
async (input) => {
3131
try {
3232
const parsed = def.inputSchema.parse(input);
33-
const result = await def.execute(parsed);
33+
const result = await def.execute(parsed, { source: 'mcp' });
3434
return { content: [{ type: "text" as const, text: result.output }] };
3535
} catch (error) {
3636
const message = error instanceof Error ? error.message : String(error);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { findSearchBasedSymbolDefinitions } from "@/features/codeNav/api";
44
import { addLineNumbers } from "@/features/chat/utils";
5-
import { createLogger } from "@sourcebot/shared";
65
import { ToolDefinition } from "./types";
76
import { FindSymbolFile } from "./findSymbolReferences";
7+
import { logger } from "./logger";
88
import description from "./findSymbolDefinitions.txt";
99

10-
const logger = createLogger('tool-findSymbolDefinitions');
11-
1210
const findSymbolDefinitionsShape = {
1311
symbol: z.string().describe("The symbol to find definitions of"),
1412
language: z.string().describe("The programming language of the symbol"),
@@ -27,8 +25,8 @@ export const findSymbolDefinitionsDefinition: ToolDefinition<
2725
name: 'find_symbol_definitions',
2826
description,
2927
inputSchema: z.object(findSymbolDefinitionsShape),
30-
execute: async ({ symbol, language, repo }) => {
31-
logger.debug('findSymbolDefinitions', { symbol, language, repo });
28+
execute: async ({ symbol, language, repo }, _context) => {
29+
logger.debug('find_symbol_definitions', { symbol, language, repo });
3230
const revision = "HEAD";
3331

3432
const response = await findSearchBasedSymbolDefinitions({

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { findSearchBasedSymbolReferences } from "@/features/codeNav/api";
44
import { addLineNumbers } from "@/features/chat/utils";
5-
import { createLogger } from "@sourcebot/shared";
65
import { ToolDefinition } from "./types";
6+
import { logger } from "./logger";
77
import description from "./findSymbolReferences.txt";
88

9-
const logger = createLogger('tool-findSymbolReferences');
10-
119
const findSymbolReferencesShape = {
1210
symbol: z.string().describe("The symbol to find references to"),
1311
language: z.string().describe("The programming language of the symbol"),
@@ -34,8 +32,8 @@ export const findSymbolReferencesDefinition: ToolDefinition<
3432
name: 'find_symbol_references',
3533
description,
3634
inputSchema: z.object(findSymbolReferencesShape),
37-
execute: async ({ symbol, language, repo }) => {
38-
logger.debug('findSymbolReferences', { symbol, language, repo });
35+
execute: async ({ symbol, language, repo }, _context) => {
36+
logger.debug('find_symbol_references', { symbol, language, repo });
3937
const revision = "HEAD";
4038

4139
const response = await findSearchBasedSymbolReferences({

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { listCommits, SearchCommitsResult } from "@/features/git";
4-
import { createLogger } from "@sourcebot/shared";
54
import { ToolDefinition } from "./types";
5+
import { logger } from "./logger";
66
import description from "./listCommits.txt";
77

8-
const logger = createLogger('tool-listCommits');
9-
108
const listCommitsShape = {
119
repo: z.string().describe("The repository to list commits from"),
1210
query: z.string().describe("Search query to filter commits by message (case-insensitive)").optional(),
@@ -24,7 +22,7 @@ export const listCommitsDefinition: ToolDefinition<"list_commits", typeof listCo
2422
name: "list_commits",
2523
description,
2624
inputSchema: z.object(listCommitsShape),
27-
execute: async (params) => {
25+
execute: async (params, _context) => {
2826
logger.debug('list_commits', params);
2927

3028
const { repo, query, since, until, author, ref, page, perPage } = params;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { listRepos } from "@/app/api/(server)/repos/listReposApi";
44
import { ToolDefinition } from "./types";
5+
import { logger } from "./logger";
56
import description from './listRepos.txt';
67

78
const listReposShape = {
@@ -34,8 +35,16 @@ export const listReposDefinition: ToolDefinition<
3435
name: 'list_repos',
3536
description,
3637
inputSchema: z.object(listReposShape),
37-
execute: async ({ page, perPage, sort, direction, query }) => {
38-
const reposResponse = await listRepos({ page, perPage, sort, direction, query });
38+
execute: async ({ page, perPage, sort, direction, query }, context) => {
39+
logger.debug('list_repos', { page, perPage, sort, direction, query });
40+
const reposResponse = await listRepos({
41+
page,
42+
perPage,
43+
sort,
44+
direction,
45+
query,
46+
source: context.source,
47+
});
3948

4049
if (isServiceError(reposResponse)) {
4150
throw new Error(reposResponse.message);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isServiceError } from "@/lib/utils";
33
import { getTree } from "@/features/git";
44
import { buildTreeNodeIndex, joinTreePath, normalizeTreePath, sortTreeEntries } from "@/features/mcp/utils";
55
import { ToolDefinition } from "./types";
6+
import { logger } from "./logger";
67
import description from "./listTree.txt";
78

89
const DEFAULT_TREE_DEPTH = 1;
@@ -41,7 +42,8 @@ export const listTreeDefinition: ToolDefinition<'list_tree', typeof listTreeShap
4142
name: 'list_tree',
4243
description,
4344
inputSchema: z.object(listTreeShape),
44-
execute: async ({ repo, path = '', ref = 'HEAD', depth = DEFAULT_TREE_DEPTH, includeFiles = true, includeDirectories = true, maxEntries = DEFAULT_MAX_TREE_ENTRIES }) => {
45+
execute: async ({ repo, path = '', ref = 'HEAD', depth = DEFAULT_TREE_DEPTH, includeFiles = true, includeDirectories = true, maxEntries = DEFAULT_MAX_TREE_ENTRIES }, context) => {
46+
logger.debug('list_tree', { repo, path, ref, depth, includeFiles, includeDirectories, maxEntries });
4547
const normalizedPath = normalizeTreePath(path);
4648
const normalizedDepth = Math.min(depth, MAX_TREE_DEPTH);
4749
const normalizedMaxEntries = Math.min(maxEntries, MAX_MAX_TREE_ENTRIES);
@@ -74,7 +76,7 @@ export const listTreeDefinition: ToolDefinition<'list_tree', typeof listTreeShap
7476
repoName: repo,
7577
revisionName: ref,
7678
paths: currentLevelPaths.filter(Boolean),
77-
});
79+
}, { source: context.source });
7880

7981
if (isServiceError(treeResult)) {
8082
throw new Error(treeResult.message);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createLogger } from "@sourcebot/shared";
2+
3+
export const logger = createLogger('tool');

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { getFileSource } from "@/features/git";
4-
import { createLogger } from "@sourcebot/shared";
54
import { ToolDefinition } from "./types";
5+
import { logger } from "./logger";
66
import description from "./readFile.txt";
77

8-
const logger = createLogger('tool-readFile');
9-
108
// NOTE: if you change these values, update readFile.txt to match.
119
const READ_FILES_MAX_LINES = 500;
1210
const MAX_LINE_LENGTH = 2000;
@@ -39,16 +37,16 @@ export const readFileDefinition: ToolDefinition<"read_file", typeof readFileShap
3937
name: "read_file",
4038
description,
4139
inputSchema: z.object(readFileShape),
42-
execute: async ({ path, repo, offset, limit }) => {
43-
logger.debug('readFile', { path, repo, offset, limit });
40+
execute: async ({ path, repo, offset, limit }, context) => {
41+
logger.debug('read_file', { path, repo, offset, limit });
4442
// @todo: make revision configurable.
4543
const revision = "HEAD";
4644

4745
const fileSource = await getFileSource({
4846
path,
4947
repo,
5048
ref: revision,
51-
});
49+
}, { source: context.source });
5250

5351
if (isServiceError(fileSource)) {
5452
throw new Error(fileSource.message);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import { z } from "zod";
22
import { isServiceError } from "@/lib/utils";
33
import { search } from "@/features/search";
44
import { addLineNumbers } from "@/features/chat/utils";
5-
import { createLogger } from "@sourcebot/shared";
65
import escapeStringRegexp from "escape-string-regexp";
76
import { ToolDefinition } from "./types";
7+
import { logger } from "./logger";
88
import description from "./searchCode.txt";
99

10-
const logger = createLogger('tool-searchCode');
11-
1210
const DEFAULT_SEARCH_LIMIT = 100;
1311

1412
const searchCodeShape = {
@@ -77,8 +75,8 @@ export const searchCodeDefinition: ToolDefinition<'search_code', typeof searchCo
7775
caseSensitive = false,
7876
ref,
7977
limit = DEFAULT_SEARCH_LIMIT,
80-
}) => {
81-
logger.debug('searchCode', { query, useRegex, repos, languages, filepaths, caseSensitive, ref, limit });
78+
}, context) => {
79+
logger.debug('search_code', { query, useRegex, repos, languages, filepaths, caseSensitive, ref, limit });
8280

8381
if (repos.length > 0) {
8482
query += ` (repo:${repos.map(id => escapeStringRegexp(id)).join(' or repo:')})`;
@@ -104,7 +102,8 @@ export const searchCodeDefinition: ToolDefinition<'search_code', typeof searchCo
104102
contextLines: 3,
105103
isCaseSensitivityEnabled: caseSensitive,
106104
isRegexEnabled: useRegex,
107-
}
105+
},
106+
source: context.source,
108107
});
109108

110109
if (isServiceError(response)) {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { z } from "zod";
22

3-
// TShape is constrained to ZodRawShape (i.e. Record<string, ZodType>) so that
4-
// both adapters receive a statically-known object schema. Tool inputs are
5-
// always key-value objects, so this constraint is semantically correct and also
6-
// lets the MCP adapter pass `.shape` (a ZodRawShapeCompat) to registerTool,
7-
// which avoids an unresolvable conditional type in BaseToolCallback.
3+
export interface ToolContext {
4+
source?: string;
5+
}
6+
87
export interface ToolDefinition<
98
TName extends string,
109
TShape extends z.ZodRawShape,
@@ -13,7 +12,7 @@ export interface ToolDefinition<
1312
name: TName;
1413
description: string;
1514
inputSchema: z.ZodObject<TShape>;
16-
execute: (input: z.infer<z.ZodObject<TShape>>) => Promise<ToolResult<TMetadata>>;
15+
execute: (input: z.infer<z.ZodObject<TShape>>, context: ToolContext) => Promise<ToolResult<TMetadata>>;
1716
}
1817

1918
export interface ToolResult<TMetadata = Record<string, unknown>> {

0 commit comments

Comments
 (0)