-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathcommonTools.ts
More file actions
1028 lines (932 loc) · 29.1 KB
/
commonTools.ts
File metadata and controls
1028 lines (932 loc) · 29.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { RepoData } from "../../shared/repoData.js";
import {
constructGithubUrl,
fetchFileFromGitHub,
getRepoBranch,
searchGitHubRepo,
} from "../utils/github.js";
import { fetchFileWithRobotsTxtCheck } from "../utils/robotsTxt.js";
import htmlToMd from "html-to-md";
import { searchCode } from "../utils/githubClient.js";
import { fetchFileFromR2 } from "../utils/r2.js";
import { generateServerName } from "../../shared/nameUtils.js";
import {
getCachedFetchDocResult,
cacheFetchDocResult,
} from "../utils/cache.js";
// Define the return type for fetchDocumentation
export type FetchDocumentationResult = {
fileUsed: string;
content: { type: "text"; text: string }[];
};
// Add env parameter to access Cloudflare's bindings
export async function fetchDocumentation({
repoData,
env,
ctx,
}: {
repoData: RepoData;
env: CloudflareEnvironment;
ctx: any;
}): Promise<FetchDocumentationResult> {
const { owner, repo, urlType } = repoData;
const cacheTTL = 30 * 60; // 30 minutes in seconds
// Try fetching from cache first
if (owner && repo) {
const cachedResult = await getCachedFetchDocResult(owner, repo, env);
if (cachedResult) {
console.log(
`Returning cached fetchDocumentation result for ${owner}/${repo}`,
);
return cachedResult;
}
}
// Initialize fileUsed to prevent "used before assigned" error
let fileUsed = "unknown";
let content: string | null = null;
let docsPath: string = "";
let docsBranch: string = "";
let blockedByRobots = false;
// Check for subdomain pattern: {subdomain}.gitmcp.io/{path}
if (urlType === "subdomain") {
// Map to github.io
const githubIoDomain = `${owner}.github.io`;
const pathWithSlash = repo ? `/${repo}` : "";
const baseURL = `https://${githubIoDomain}${pathWithSlash}/`;
// Try to fetch llms.txt with robots.txt check
const llmsResult = await fetchFileWithRobotsTxtCheck(
baseURL + "llms.txt",
env,
);
if (llmsResult.blockedByRobots) {
blockedByRobots = true;
console.log(`Access to ${baseURL}llms.txt disallowed by robots.txt`);
} else if (llmsResult.content) {
content = llmsResult.content;
fileUsed = "llms.txt";
} else {
// If llms.txt is not found or disallowed, fall back to the landing page
console.warn(
`llms.txt not found or not allowed at ${baseURL}, trying base URL`,
);
const indexResult = await fetchFileWithRobotsTxtCheck(baseURL, env);
if (indexResult.blockedByRobots) {
blockedByRobots = true;
console.log(`Access to ${baseURL} disallowed by robots.txt`);
} else if (indexResult.content) {
try {
// Convert HTML to Markdown for proper processing
content = htmlToMd(indexResult.content);
fileUsed = "landing page (index.html, converted to Markdown)";
} catch (error) {
console.warn(
`Error converting HTML to Markdown for ${baseURL}: ${error}`,
);
}
}
// If index page was blocked or not available, try readme.md
if (!content && !blockedByRobots) {
const readmeResult = await fetchFileWithRobotsTxtCheck(
baseURL + "README.md",
env,
);
if (readmeResult.blockedByRobots) {
blockedByRobots = true;
console.log(`Access to ${baseURL}README.md disallowed by robots.txt`);
} else if (readmeResult.content) {
content = readmeResult.content;
fileUsed = "README.md";
}
}
}
// If any path was blocked by robots.txt, return appropriate message
if (blockedByRobots) {
content =
"Access to this GitHub Pages site is restricted by robots.txt. GitMCP respects robots.txt directives.";
fileUsed = "robots.txt restriction";
}
} else if (urlType === "github" && owner && repo) {
// Try static paths + search for llms.txt directly
docsBranch = await getRepoBranch(owner, repo, env); // Get branch once
console.log(`Checking static paths for llms.txt in ${owner}/${repo}`);
const possibleLocations = [
"docs/docs/llms.txt", // Current default
"llms.txt", // Root directory
"docs/llms.txt", // Common docs folder
];
// Create array of all location+branch combinations to try
const fetchPromises = possibleLocations.flatMap((location) => [
{
promise: fetchFileFromGitHub(
owner,
repo,
docsBranch,
location,
env,
false,
),
location,
branch: docsBranch,
},
]);
// Execute all fetch promises in parallel
const results = await Promise.all(
fetchPromises.map(async ({ promise, location, branch }) => {
const content = await promise;
return { content, location, branch };
}),
);
for (const location of possibleLocations) {
const mainResult = results.find(
(r) => r.location === location && r.content !== null,
);
if (mainResult) {
content = mainResult.content;
fileUsed = `llms.txt`;
docsPath = constructGithubUrl(
owner,
repo,
mainResult.branch,
mainResult.location,
);
break;
}
}
// Fallback to GitHub Search API if static paths don't work for llms.txt
if (!content) {
console.log(
`llms.txt not found in static paths, trying GitHub Search API`,
);
const result = await searchGitHubRepo(
owner,
repo,
"llms.txt",
docsBranch,
env,
ctx,
);
if (result) {
content = result.content;
docsPath = result.path;
fileUsed = "llms.txt";
}
}
// Try R2 fallback if llms.txt wasn't found via GitHub
if (!content) {
// Try to fetch pre-generated llms.txt
content = (await fetchFileFromR2(owner, repo, "llms.txt", env)) ?? null;
if (content) {
console.log(`Fetched pre-generated llms.txt for ${owner}/${repo}`);
fileUsed = "llms.txt (generated)";
} else {
console.error(`No pre-generated llms.txt found for ${owner}/${repo}`);
}
}
// Fallback to README if llms.txt not found in any location (GitHub or R2)
if (!content) {
console.log(
`llms.txt not found, trying README.* at root`,
owner,
repo,
docsBranch,
);
// Ensure docsBranch is available (should be fetched above)
if (!docsBranch) {
docsBranch = await getRepoBranch(owner, repo, env);
}
// Search for README.* files in the root directory
const readmeResult = await searchGitHubRepo(
owner,
repo,
"README+path:/", // Search for files like README.* in root
docsBranch, // Use the determined branch
env,
ctx,
);
if (readmeResult) {
content = readmeResult.content;
// Extract filename from the path for clarity, default to full path if extraction fails
const filename =
readmeResult.path.split("/").pop() || readmeResult.path;
fileUsed = filename; // e.g., "README.md", "README.asciidoc"
docsPath = constructGithubUrl(
owner,
repo,
docsBranch,
readmeResult.path,
); // Use the full path found
console.log(`Found README file via search: ${fileUsed}`);
} else {
console.log(`No README file found at root for ${owner}/${repo}`);
}
}
if (!content) {
console.error(`Failed to find documentation for ${owner}/${repo}`);
}
}
if (owner && repo) {
ctx.waitUntil(
enqueueDocumentationProcessing(
owner,
repo,
content,
fileUsed,
docsPath,
docsBranch,
env,
),
);
}
if (!content) {
content = "No documentation found.";
return {
fileUsed,
content: [
{
type: "text" as const,
text: content,
},
],
};
}
const result: FetchDocumentationResult = {
fileUsed,
content: [
{
type: "text" as const,
text: content,
},
],
};
if (owner && repo) {
ctx.waitUntil(
cacheFetchDocResult(owner, repo, result, cacheTTL, env).catch((error) => {
console.warn(`Failed to cache fetch documentation result: ${error}`);
}),
);
}
return result;
}
async function enqueueDocumentationProcessing(
owner: string,
repo: string,
content: string | null,
fileUsed: string,
docsPath: string,
docsBranch: string,
env: Env,
) {
try {
if (env.MY_QUEUE) {
console.log("Enqueuing documentation processing", owner, repo);
const repoUrl = `https://github.com/${owner}/${repo}`;
// Prepare and send message to queue
const message = {
owner,
repo,
repo_url: repoUrl,
file_url: docsPath,
content_length: content?.length,
file_used: fileUsed,
docs_branch: docsBranch,
};
await env.MY_QUEUE.send(JSON.stringify(message));
console.log(
`Queued documentation processing for ${owner}/${repo}`,
message,
);
} else {
console.error("Queue 'MY_QUEUE' not available in environment");
}
} catch (error) {
console.error(
`Failed to enqueue documentation request for ${owner}/${repo}`,
error,
);
}
}
export async function searchRepositoryDocumentation({
repoData,
query,
env,
ctx,
fallbackSearch = searchRepositoryDocumentationNaive,
}: {
repoData: RepoData;
query: string;
env: CloudflareEnvironment;
ctx: any;
fallbackSearch?: typeof searchRepositoryDocumentationNaive;
}): Promise<{
searchQuery: string;
content: { type: "text"; text: string }[];
}> {
if (!env.DOCS_BUCKET) {
throw new Error("DOCS_BUCKET is not available in environment");
}
const docsInR2 = !!(await env.DOCS_BUCKET.head(
`${repoData.owner}/${repoData.repo}/llms.txt`,
));
if (docsInR2) {
try {
const autoragResult = await searchRepositoryDocumentationAutoRag({
repoData,
query,
env,
ctx,
autoragPipeline: "docs-rag",
});
if (
autoragResult?.content[0]?.text?.startsWith("No results found") ===
false
) {
console.log("Found results in AutoRAG", autoragResult);
return autoragResult;
}
console.log("No results in AutoRAG", autoragResult);
} catch (error) {
console.error("Error in AutoRAG search", error);
}
}
return await fallbackSearch({
repoData,
query,
env,
ctx,
});
}
export async function searchRepositoryDocumentationAutoRag({
repoData,
query,
env,
ctx,
autoragPipeline = "docs-rag",
}: {
repoData: RepoData;
query: string;
env: CloudflareEnvironment;
ctx: any;
autoragPipeline: string;
}): Promise<{
searchQuery: string;
content: { type: "text"; text: string }[];
}> {
if (!repoData.owner || !repoData.repo) {
return {
searchQuery: query,
content: [{ type: "text", text: "No repository data provided" }],
};
}
const repoPrefix = `${repoData.owner}/${repoData.repo}/`;
const searchRequest = {
query: query,
rewrite_query: true,
max_num_results: 12,
ranking_options: {
score_threshold: 0.4,
},
filters: {
type: "and",
filters: [
{
type: "gte",
key: "folder",
value: `${repoPrefix}`,
},
{
type: "lte",
key: "folder",
value: `${repoPrefix}~`,
},
],
},
};
const answer = await env.AI.autorag(autoragPipeline).search(searchRequest);
let responseText =
`## Query\n\n${query}.\n\n## Response\n\n` ||
`No results found for: "${query}"`;
// Add source data if available
if (answer.data && answer.data.length > 0) {
const filteredData = answer.data.filter((item) => {
return item.filename.startsWith(`${repoData.owner}/${repoData.repo}/`);
});
if (filteredData.length > 0) {
responseText +=
"### Sources:\nImportant: you can fetch the full content of any source using the fetch_url_content tool\n";
const defaultBranch = await getRepoBranch(
repoData.owner,
repoData.repo,
env,
);
for (const item of filteredData) {
let rawUrl = constructGithubUrl(
repoData.owner,
repoData.repo,
defaultBranch,
item.filename.replace(`${repoData.owner}/${repoData.repo}/`, ""),
);
if (item.filename.endsWith(".ipynb.txt")) {
rawUrl = `https://pub-39b02ce1b5a441b2a4658c1fc71dbb9c.r2.dev/${repoData.owner}/${repoData.repo}/${item.filename}`;
}
responseText += `\n#### (${item.filename})[${rawUrl}] (Score: ${item.score.toFixed(2)})\n`;
if (item.content && item.content.length > 0) {
for (const content of item.content) {
if (content.text) {
responseText += `- ${content.text}\n`;
}
}
}
}
} else {
responseText = `No results found for: "${query}"`;
}
} else {
responseText = `No results found for: "${query}"`;
}
return {
searchQuery: answer.search_query || query,
content: [
{
type: "text",
text: responseText,
},
],
};
}
/**
* Search documentation using vector search
* Will fetch and index documentation if none exists
*/
export async function searchRepositoryDocumentationNaive({
repoData,
query,
forceReindex = false,
env,
ctx,
}: {
repoData: RepoData;
query: string;
forceReindex?: boolean;
env: CloudflareEnvironment;
ctx: any;
}): Promise<{
searchQuery: string;
content: { type: "text"; text: string }[];
}> {
// Initialize owner and repo
let owner: string | null =
repoData.owner ?? repoData.host.replace(/\./g, "_");
let repo: string | null = repoData.repo ?? "docs";
console.log(`Searching ${owner}/${repo}`);
try {
// Fetch the documentation - pass env
const docResult = await fetchDocumentation({ repoData, env, ctx });
const content = docResult.content[0].text;
const fileUsed = docResult.fileUsed;
console.log(
`Fetched documentation from ${fileUsed} (${content.length} characters)`,
);
// Format search results as text for MCP response, or provide a helpful message if none
const formattedText =
`### Search Results for: "${query}"\n\n` +
`No relevant documentation found for your query. It's either being indexed or the search query did not match any documentation.\n\n` +
`As a fallback, this is the documentation for ${owner}/${repo}:\n\n` +
`${content}\n\n` +
`If you'd like to retry the search, try changing the query to increase the likelihood of a match.`;
// Return search results in proper MCP format
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: formattedText,
},
],
};
} catch (error) {
console.error(`Error in searchRepositoryDocumentation: ${error}`);
return {
searchQuery: query,
content: [
{
type: "text" as const,
text:
`### Search Results for: "${query}"\n\n` +
`An error occurred while searching the documentation. Please try again later.`,
},
],
};
}
}
/**
* Search for code in a GitHub repository
* Uses the GitHub Search API to find code matching a query
* Supports pagination for retrieving more results
*/
export async function searchRepositoryCode({
repoData,
query,
page = 1,
env,
ctx,
}: {
repoData: RepoData;
query: string;
page?: number;
env: Env;
ctx: any;
}): Promise<{
searchQuery: string;
content: { type: "text"; text: string }[];
pagination?: {
totalCount: number;
currentPage: number;
perPage: number;
hasMorePages: boolean;
};
}> {
try {
// Initialize owner and repo from the provided repoData
const owner = repoData.owner;
const repo = repoData.repo;
if (!owner || !repo) {
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: `### Code Search Results for: "${query}"\n\nCannot perform code search without repository information.`,
},
],
};
}
// Use fixed resultsPerPage of 30 and normalize page value
const currentPage = Math.max(1, page);
const resultsPerPage = 30; // Fixed at 30 results per page
console.log(
`Searching code in ${owner}/${repo}" (page ${currentPage}, ${resultsPerPage} per page)`,
);
const data = await searchCode(
query,
owner,
repo,
env,
currentPage,
resultsPerPage,
);
if (!data) {
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: `### Code Search Results for: "${query}"\n\nFailed to search code in ${owner}/${repo}. GitHub API request failed.`,
},
],
};
}
// Check if we found any matches
if (data.total_count === 0 || !data.items || data.items.length === 0) {
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: `### Code Search Results for: "${query}"\n\nNo code matches found in ${owner}/${repo}.`,
},
],
};
}
// Calculate pagination information
const totalCount = data.total_count;
const hasMorePages = currentPage * resultsPerPage < totalCount;
const totalPages = Math.ceil(totalCount / resultsPerPage);
// Format the search results
let formattedResults = `### Code Search Results for: "${query}"\n\n`;
formattedResults += `Found ${totalCount} matches in ${owner}/${repo}.\n`;
formattedResults += `Page ${currentPage} of ${totalPages}.\n\n`;
for (const item of data.items) {
formattedResults += `#### ${item.name}\n`;
formattedResults += `- **Path**: ${item.path}\n`;
formattedResults += `- **URL**: ${item.html_url}\n`;
formattedResults += `- **Git URL**: ${item.git_url}\n`;
formattedResults += `- **Score**: ${item.score}\n\n`;
}
// Add pagination information to the response
if (hasMorePages) {
formattedResults += `_Showing ${data.items.length} of ${totalCount} results. Use pagination to see more results._\n\n`;
}
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: formattedResults,
},
],
pagination: {
totalCount,
currentPage,
perPage: resultsPerPage,
hasMorePages,
},
};
} catch (error) {
console.error("Error in searchRepositoryCode:", error);
return {
searchQuery: query,
content: [
{
type: "text" as const,
text: `### Code Search Results for: "${query}"\n\nAn error occurred while searching code. Please try again later.`,
},
],
};
}
}
export async function fetchUrlContent({ url, env }: { url: string; env: Env }) {
try {
// Use the robotsTxt checking function to respect robots.txt rules
const result = await fetchFileWithRobotsTxtCheck(url, env);
if (result.blockedByRobots) {
return {
url,
status: "blocked",
content: [
{
type: "text" as const,
text: `Access to ${url} is disallowed by robots.txt. GitMCP respects robots.txt directives.`,
},
],
};
}
if (!result.content) {
return {
url,
status: "not_found",
content: [
{
type: "text" as const,
text: `Content at ${url} could not be retrieved. The resource may not exist or may require authentication.`,
},
],
};
}
let finalContent = result.content;
// Convert HTML to markdown if content appears to be HTML
if (
finalContent.trim().startsWith("<!DOCTYPE") ||
finalContent.trim().startsWith("<html") ||
finalContent.includes("<body")
) {
try {
finalContent = htmlToMd(finalContent);
} catch (error) {
console.warn(`Error converting HTML to Markdown for ${url}: ${error}`);
// Continue with the original content if conversion fails
}
}
return {
url,
status: "success",
content: [
{
type: "text" as const,
text: finalContent,
},
],
};
} catch (error) {
console.error(`Error fetching ${url}:`, error);
return {
url,
status: "error",
content: [
{
type: "text" as const,
text: `Error fetching content from ${url}. Please try again later.`,
},
],
};
}
}
export const LIMIT = 51;
/**
* Enforces the 50-character limit on the combined server and tool names
* @param prefix - The prefix for the tool name (fetch_ or search_)
* @param repo - The repository name
* @param suffix - The suffix for the tool name (_documentation)
* @returns A tool name that ensures combined length with server name stays under 50 characters
*/
export function enforceToolNameLengthLimit(
prefix: string,
repo: string | null | undefined,
suffix: string,
): string {
if (!repo) {
console.error(
"Repository name is null/undefined in enforceToolNameLengthLimit",
);
return `${prefix}${suffix}`;
}
// Generate the server name to check combined length
const serverNameLen = generateServerName(repo).length;
// Replace non-alphanumeric characters with underscores
let repoName = repo.replace(/[^a-zA-Z0-9]/g, "_");
let toolName = `${prefix}${repoName}${suffix}`;
// Calculate combined length
const combinedLength = toolName.length + serverNameLen;
// If combined length is already under limit, return it
if (combinedLength <= LIMIT) {
return toolName;
}
const shorterSuffix = suffix === "_documentation" ? "_docs" : suffix;
toolName = `${prefix}${repoName}${shorterSuffix}`;
if (toolName.length + serverNameLen <= LIMIT) {
return toolName;
}
// Step 2: Shorten the repo name by removing words
const words = repoName.split("_");
if (words.length > 1) {
// Keep removing words from the end until we're under the limit or have only one word left
let shortenedRepo = repoName;
for (let i = words.length - 1; i > 0; i--) {
shortenedRepo = words.slice(0, i).join("_");
toolName = `${prefix}${shortenedRepo}${shorterSuffix}`;
if (toolName.length + serverNameLen <= LIMIT) {
return toolName;
}
}
}
const result = `${prefix}repo${shorterSuffix}`;
if (result.length + serverNameLen <= LIMIT) {
return result;
}
// Step 3: As a last resort, change repo name to "repo"
return `${prefix}${shorterSuffix}`.replace(/__/g, "_");
}
/**
* Generate a dynamic search tool name for the search_documentation tool based on the URL
* @param requestHost - The host from the request
* @param requestUrl - The full request URL (optional)
* @returns A descriptive string for the tool name
*/
export function generateSearchToolName({ urlType, repo }: RepoData): string {
try {
// Default tool name as fallback
let toolName = "search_documentation";
if (urlType == "subdomain" || urlType == "github") {
// Use enforceLengthLimit to ensure the tool name doesn't exceed 55 characters
return enforceToolNameLengthLimit("search_", repo, "_documentation");
}
// replace non-alphanumeric characters with underscores
return toolName.replace(/[^a-zA-Z0-9]/g, "_");
} catch (error) {
console.error("Error generating search tool name:", error);
// Return default tool name if there's any error parsing the URL
return "search_documentation";
}
}
/**
* Generate a dynamic description for the search_documentation tool based on the URL
* @param requestHost - The host from the request
* @param requestUrl - The full request URL (optional)
* @returns A descriptive string for the tool
*/
export function generateSearchToolDescription({
urlType,
owner,
repo,
}: RepoData): string {
try {
// Default description as fallback
let description =
"Semantically search within the fetched documentation for the current repository.";
if (urlType == "subdomain") {
description = `Semantically search within the fetched documentation from the ${owner}/${repo} GitHub Pages. Useful for specific queries.`;
} else if (urlType == "github") {
description = `Semantically search within the fetched documentation from GitHub repository: ${owner}/${repo}. Useful for specific queries.`;
}
return description;
} catch (error) {
// Return default description if there's any error parsing the URL
return "Search documentation for the current repository.";
}
}
/**
* Generate a dynamic description for the fetch_documentation tool based on the URL
* @param requestHost - The host from the request
* @param requestUrl - The full request URL (optional)
* @returns A descriptive string for the tool
*/
export function generateFetchToolDescription({
urlType,
owner,
repo,
}: Omit<RepoData, "host">): string {
try {
// Default description as fallback
let description = "Fetch entire documentation for the current repository.";
if (urlType == "subdomain") {
description = `Fetch entire documentation file from the ${owner}/${repo} GitHub Pages. Useful for general questions. Always call this tool first if asked about ${owner}/${repo}.`;
} else if (urlType == "github") {
description = `Fetch entire documentation file from GitHub repository: ${owner}/${repo}. Useful for general questions. Always call this tool first if asked about ${owner}/${repo}.`;
}
return description;
} catch (error) {
// Return default description if there's any error parsing the URL
return "Fetch documentation for the current repository.";
}
}
/**
* Generate a dynamic tool name for the fetch_documentation tool based on the URL
* @param requestHost - The host from the request
* @param requestUrl - The full request URL (optional)
* @returns A descriptive string for the tool
*/
export function generateFetchToolName({
urlType,
owner,
repo,
}: Omit<RepoData, "host">): string {
try {
// Default tool name as fallback
let toolName = "fetch_documentation";
if (urlType == "subdomain" || urlType == "github") {
// Use enforceLengthLimit to ensure the tool name doesn't exceed 55 characters
return enforceToolNameLengthLimit("fetch_", repo, "_documentation");
}
// replace non-alphanumeric characters with underscores
return toolName.replace(/[^a-zA-Z0-9]/g, "_");
} catch (error) {
console.error("Error generating tool name:", error);
// Return default tool name if there's any error parsing the URL
return "fetch_documentation";
}
}
/**
* Generate a dynamic tool name for the code search tool based on the URL
* @param repoData - The repository data object
* @returns A descriptive string for the tool
*/
export function generateCodeSearchToolName({
urlType,
repo,
}: RepoData): string {
try {
// Default tool name as fallback
let toolName = "search_code";
if (urlType == "subdomain" || urlType == "github") {
// Use enforceLengthLimit to ensure the tool name doesn't exceed 55 characters
return enforceToolNameLengthLimit("search_", repo, "_code");
}
// replace non-alphanumeric characters with underscores
return toolName.replace(/[^a-zA-Z0-9]/g, "_");
} catch (error) {
console.error("Error generating code search tool name:", error);
// Return default tool name if there's any error parsing the URL
return "search_code";
}
}
/**
* Generate a dynamic description for the code search tool based on the URL
* @param repoData - The repository data object
* @returns A descriptive string for the tool
*/
export function generateCodeSearchToolDescription({
owner,
repo,
}: RepoData): string {
return `Search for code within the GitHub repository: "${owner}/${repo}" using the GitHub Search API (exact match). Returns matching files for you to query further if relevant.`;
}
/**
* Recursively list every subfolder prefix under `startPrefix`.
* @param {R2Bucket} bucket – the Workers-bound R2 bucket
* @param {string} startPrefix – e.g. "path/to/folder/"
* @returns {Promise<string[]>}
*/