-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathclient.ts
More file actions
130 lines (111 loc) · 4.45 KB
/
client.ts
File metadata and controls
130 lines (111 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { env } from './env.js';
import { listReposResponseSchema, searchResponseSchema, fileSourceResponseSchema, listCommitsResponseSchema, askCodebaseResponseSchema } from './schemas.js';
import { AskCodebaseRequest, AskCodebaseResponse, FileSourceRequest, ListReposQueryParams, SearchRequest, ListCommitsQueryParamsSchema } from './types.js';
import { isServiceError, ServiceErrorException } from './utils.js';
import { z } from 'zod';
const parseResponse = async <T extends z.ZodTypeAny>(
response: Response,
schema: T
): Promise<z.infer<T>> => {
const text = await response.text();
let json: unknown;
try {
json = JSON.parse(text);
} catch {
throw new Error(`Invalid JSON response: ${text}`);
}
// Check if the response is already a service error from the API
if (isServiceError(json)) {
throw new ServiceErrorException(json);
}
const parsed = schema.safeParse(json);
if (!parsed.success) {
throw new Error(`Failed to parse response: ${parsed.error.message}`);
}
return parsed.data;
};
export const search = async (request: SearchRequest) => {
const response = await fetch(`${env.SOURCEBOT_HOST}/api/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Sourcebot-Client-Source': 'mcp',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
body: JSON.stringify(request)
});
return parseResponse(response, searchResponseSchema);
}
export const listRepos = async (queryParams: ListReposQueryParams = {}) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/repos`);
for (const [key, value] of Object.entries(queryParams)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Sourcebot-Client-Source': 'mcp',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
const repos = await parseResponse(response, listReposResponseSchema);
const totalCount = parseInt(response.headers.get('X-Total-Count') ?? '0', 10);
return { repos, totalCount };
}
export const getFileSource = async (request: FileSourceRequest) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/source`);
for (const [key, value] of Object.entries(request)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
'X-Sourcebot-Client-Source': 'mcp',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
return parseResponse(response, fileSourceResponseSchema);
}
export const listCommits = async (queryParams: ListCommitsQueryParamsSchema) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/commits`);
for (const [key, value] of Object.entries(queryParams)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
'X-Org-Domain': '~',
'X-Sourcebot-Client-Source': 'mcp',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
const commits = await parseResponse(response, listCommitsResponseSchema);
const totalCount = parseInt(response.headers.get('X-Total-Count') ?? '0', 10);
return { commits, totalCount };
}
/**
* Asks a natural language question about the codebase using the Sourcebot AI agent.
* This is a blocking call that runs the full agent loop and returns when complete.
*
* @param request - The question and optional repo filters
* @returns The agent's answer, chat URL, sources, and metadata
*/
export const askCodebase = async (request: AskCodebaseRequest): Promise<AskCodebaseResponse> => {
const response = await fetch(`${env.SOURCEBOT_HOST}/api/chat/blocking`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Sourcebot-Client-Source': 'mcp',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
body: JSON.stringify(request),
});
return parseResponse(response, askCodebaseResponseSchema);
}