Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | 5x 5x 5x 79x 79x 79x 16x 16x 6x 6x 4x 4x 4x 4x 1x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 6x 6x 6x 1x 5x 5x 5x 9x 9x 3x 3x 5x 2x 3x 3x 3x 3x 3x 3x 3x 2x 2x 1x 4x 4x 4x 1x 3x 3x 3x 5x 5x 2x 2x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 7x 7x 7x 7x 8x 8x 6x 3x 3x 3x 3x 2x 1x 1x 8x 8x 4x 4x 8x 5x 8x 1x 1x 8x 2x 8x 8x 2x 8x 12x 12x 14x 14x 11x 5x 6x 3x 3x 3x 3x 11x 11x 9x 1x 8x 11x 1x 2x 1x 1x 11x 1x 1x 11x 7x 19x 10x 2x 8x 7x 1x 10x 9x 9x 22x 22x 1x 4x 1x | import { ProviderManager } from '../providers/ProviderManager.js'; import { WorkItem, ProviderAPIResponse, Priority } from '../types/index.js'; import { Tool } from '@modelcontextprotocol/sdk/types.js'; import { hasTextContent, isProviderAPIResponse, isLabelLike } from '../utils/typeGuards.js'; import { CacheManager, ProjectCacheData, UserRole } from '../cache/CacheManager.js'; export class WorkItemsManager { private cacheManager: CacheManager; constructor( protected providerManager: ProviderManager, cacheManager?: CacheManager, ) { this.cacheManager = cacheManager ?? new CacheManager(); // Set up cache refresh callback this.cacheManager.onCacheExpired = (cacheKey: string) => { void this.handleCacheExpired(cacheKey); }; } protected detectProviderFromProject(project: string): string { const [provider] = project.split(':'); return provider; } async listWorkItems(project?: string, filters?: Record<string, unknown>): Promise<WorkItem[]> { const workItems: WorkItem[] = []; if (project) { // Check if project has provider prefix (contains ':') if (project.includes(':')) { // Handle project with provider prefix const provider = this.detectProviderFromProject(project); const providerInstance = this.providerManager.getProvider(provider); if (!providerInstance) { throw new Error(`Provider ${provider} not found`); } const toolName = `${provider}_list_issues`; try { const result = await this.providerManager.callTool(toolName, { ...filters, project: project.split(':')[1], }); Eif (hasTextContent(result)) { const itemsJson = result.content[0].text; const parsedItems: unknown = JSON.parse(itemsJson); Eif (Array.isArray(parsedItems)) { for (const item of parsedItems) { Eif (isProviderAPIResponse(item)) { workItems.push(this.normalizeWorkItem(item, provider)); } } } } } catch (error) { if (error instanceof Error) { process.stderr.write(`Error listing work items from ${provider}: ${error.message}\n`); } else E{ process.stderr.write(`Error listing work items from ${provider}: ${String(error)}\n`); } } } else E{ // Handle project without provider prefix - search across all providers process.stderr.write(`Searching for project "${project}" across all providers...\n`); for (const provider of this.providerManager.getAllProviders()) { if (provider.status !== 'connected') continue; const listTools = ['list_issues', 'list_work_items', 'list_tasks']; for (const toolSuffix of listTools) { const toolName = `${provider.id}_${toolSuffix}`; if (provider.tools.has(toolName)) { try { // Search with project as a filter parameter const searchFilters = { ...filters, project: project, project_path: project, repository: project, repo: project, }; const result = await this.providerManager.callTool(toolName, searchFilters); if (hasTextContent(result)) { const itemsJson = result.content[0].text; const parsedItems: unknown = JSON.parse(itemsJson); if (Array.isArray(parsedItems)) { for (const item of parsedItems) { if (isProviderAPIResponse(item)) { workItems.push(this.normalizeWorkItem(item, provider.id)); } } } } break; } catch { // Continue to next provider on error - project might not exist in this provider process.stderr.write(`No results from ${provider.id} for project "${project}"\n`); } } } } } } else { for (const provider of this.providerManager.getAllProviders()) { if (provider.status !== 'connected') continue; const listTools = ['list_issues', 'list_work_items', 'list_tasks']; for (const toolSuffix of listTools) { const toolName = `${provider.id}_${toolSuffix}`; Eif (provider.tools.has(toolName)) { try { const result = await this.providerManager.callTool(toolName, filters ?? {}); Eif (hasTextContent(result)) { const itemsJson = result.content[0].text; const parsedItems: unknown = JSON.parse(itemsJson); Eif (Array.isArray(parsedItems)) { for (const item of parsedItems) { Eif (isProviderAPIResponse(item)) { workItems.push(this.normalizeWorkItem(item, provider.id)); } } } } break; } catch (error) { if (error instanceof Error) { process.stderr.write( `Error listing work items from ${provider.id}: ${error.message}\n`, ); } else { process.stderr.write( `Error listing work items from ${provider.id}: ${String(error)}\n`, ); } } } } } } return workItems; } async createWorkItem(project: string, item: Partial<WorkItem>): Promise<WorkItem> { const provider = this.detectProviderFromProject(project); const providerInstance = this.providerManager.getProvider(provider); if (!providerInstance) { throw new Error(`Provider ${provider} not found`); } const createTools = ['create_issue', 'create_work_item', 'create_task']; let toolName: string | null = null; for (const toolSuffix of createTools) { const candidateTool = `${provider}_${toolSuffix}`; if (providerInstance.tools.has(candidateTool)) { toolName = candidateTool; break; } } if (!toolName) { throw new Error(`No create tool found for provider ${provider}`); } const projectPath = project.split(':')[1]; const args = this.denormalizeWorkItem(item, provider, projectPath); const result = await this.providerManager.callTool(toolName, args); Eif (hasTextContent(result)) { const createdItemJson = result.content[0].text; try { const createdItem: unknown = JSON.parse(createdItemJson); Eif (isProviderAPIResponse(createdItem)) { return this.normalizeWorkItem(createdItem, provider); } } catch { throw new Error('Failed to create work item'); } } throw new Error('Failed to create work item'); } async updateWorkItem(workItemId: string, updates: Partial<WorkItem>): Promise<WorkItem> { const [provider, id] = workItemId.split(':'); const providerInstance = this.providerManager.getProvider(provider); if (!providerInstance) { throw new Error(`Provider ${provider} not found`); } const updateTools = ['update_issue', 'update_work_item', 'update_task']; let toolName: string | null = null; for (const toolSuffix of updateTools) { const candidateTool = `${provider}_${toolSuffix}`; if (providerInstance.tools.has(candidateTool)) { toolName = candidateTool; break; } } if (!toolName) { throw new Error(`No update tool found for provider ${provider}`); } const args = { id: id, ...this.denormalizeWorkItem(updates, provider), }; const result = await this.providerManager.callTool(toolName, args); Eif (hasTextContent(result)) { const updatedItemJson = result.content[0].text; const updatedItem: unknown = JSON.parse(updatedItemJson); Eif (isProviderAPIResponse(updatedItem)) { return this.normalizeWorkItem(updatedItem, provider); } } throw new Error('Failed to update work item'); } async transferWorkItem(workItemId: string, targetProject: string): Promise<WorkItem> { const sourceWorkItem = await this.getWorkItem(workItemId); if (!sourceWorkItem) { throw new Error(`Work item ${workItemId} not found`); } const newItem = await this.createWorkItem(targetProject, { title: sourceWorkItem.title, description: `Transferred from ${sourceWorkItem.provider}:${sourceWorkItem.id}\n\n${sourceWorkItem.description}`, type: sourceWorkItem.type, labels: [...sourceWorkItem.labels, 'transferred'], priority: sourceWorkItem.priority, }); await this.updateWorkItem(workItemId, { state: 'closed', description: `${sourceWorkItem.description}\n\n---\nTransferred to ${targetProject}:${newItem.id}`, }); return newItem; } private async getWorkItem(workItemId: string): Promise<WorkItem | null> { const [provider, id] = workItemId.split(':'); const providerInstance = this.providerManager.getProvider(provider); Iif (!providerInstance) { throw new Error(`Provider ${provider} not found`); } const getTools = ['get_issue', 'get_work_item', 'get_task']; let toolName: string | null = null; for (const toolSuffix of getTools) { const candidateTool = `${provider}_${toolSuffix}`; Eif (providerInstance.tools.has(candidateTool)) { toolName = candidateTool; break; } } Iif (!toolName) { return null; } try { const result = await this.providerManager.callTool(toolName, { id }); Eif (hasTextContent(result)) { const itemJson = result.content[0].text; const item: unknown = JSON.parse(itemJson); if (isProviderAPIResponse(item)) { return this.normalizeWorkItem(item, provider); } } } catch (error) { if (error instanceof Error) { process.stderr.write(`Error getting work item ${workItemId}: ${error.message}\n`); } else { process.stderr.write(`Error getting work item ${workItemId}: ${String(error)}\n`); } } return null; } private normalizeWorkItem(item: ProviderAPIResponse, provider: string): WorkItem { const assignees = this.normalizeAssignees(item); const author = this.normalizeAuthor(item); const normalized: WorkItem = { id: `${provider}:${item.id ?? item.number ?? item.iid}`, provider: provider as 'gitlab' | 'github' | 'azure', type: this.normalizeType(item.type ?? item.issue_type ?? 'issue'), title: item.title ?? item.name ?? item.summary ?? '', description: item.description ?? item.body ?? item.content ?? '', state: this.normalizeState(item.state ?? item.status ?? 'open'), author: author, assignees: assignees, labels: this.normalizeLabels(item), priority: (item.priority as Priority | undefined) ?? 'medium', createdAt: item.created_at ? new Date(item.created_at) : new Date(), updatedAt: item.updated_at ? new Date(item.updated_at) : new Date(), providerFields: this.createProviderFields(item, provider), }; return normalized; } denormalizeWorkItem( item: Partial<WorkItem>, provider: string, project?: string, ): Record<string, unknown> { const denormalized: Record<string, unknown> = {}; if (project) { if (provider === 'github') { const [owner, repo] = project.split('/'); denormalized.owner = owner; denormalized.repo = repo; } else if (provider === 'gitlab') { denormalized.project_id = project; } else Eif (provider === 'azure') { denormalized.project = project; } } if (item.title) denormalized.title = item.title; if (item.description) { denormalized.description = item.description; denormalized.body = item.description; } if (item.state) { denormalized.state = this.denormalizeState(item.state, provider); } if (item.assignees && item.assignees.length > 0) { denormalized.assignee = item.assignees[0].username; denormalized.assignees = item.assignees.map((a) => a.username); } if (item.labels) { denormalized.labels = item.labels; } Iif (item.milestone) { denormalized.milestone = item.milestone; } if (item.priority) { denormalized.priority = item.priority; } return denormalized; } normalizeType(type: string): WorkItem['type'] { const typeMap: Record<string, WorkItem['type']> = { issue: 'issue', task: 'task', epic: 'epic', story: 'story', bug: 'bug', incident: 'bug', feature: 'story', }; return typeMap[type.toLowerCase()] ?? 'issue'; } normalizeState(status: string): 'open' | 'closed' { const statusMap: Record<string, 'open' | 'closed'> = { open: 'open', opened: 'open', closed: 'closed', resolved: 'closed', done: 'closed', todo: 'open', new: 'open', }; return statusMap[status.toLowerCase()] ?? 'open'; } denormalizeState(state: 'open' | 'closed', provider: string): string { if (provider === 'github') { return state === 'closed' ? 'closed' : 'open'; } else if (provider === 'gitlab') { return state === 'closed' ? 'closed' : 'opened'; } else Eif (provider === 'azure') { const azureStateMap: Record<string, string> = { open: 'New', closed: 'Closed', }; return azureStateMap[state] || state; } return state; } normalizeAssignees(item: ProviderAPIResponse): import('../types/index.js').User[] { const assignees: import('../types/index.js').User[] = []; if (item.assignee) { if (typeof item.assignee === 'string') { assignees.push(this.createUser(item.assignee, item.assignee)); } else { assignees.push( this.createUser( item.assignee.username ?? item.assignee.login ?? 'unknown', item.assignee.name ?? item.assignee.username ?? item.assignee.login ?? 'unknown', ), ); } } if (item.assignees && Array.isArray(item.assignees)) { for (const assignee of item.assignees) { if (typeof assignee === 'string') { assignees.push(this.createUser(assignee, assignee)); } else { assignees.push( this.createUser( assignee.username ?? assignee.login ?? 'unknown', assignee.name ?? assignee.username ?? assignee.login ?? 'unknown', ), ); } } } if (item.assigned_to && assignees.length === 0) { Iif (typeof item.assigned_to === 'string') { assignees.push(this.createUser(item.assigned_to, item.assigned_to)); } else { assignees.push( this.createUser(item.assigned_to.name ?? 'unknown', item.assigned_to.name ?? 'unknown'), ); } } return assignees; } normalizeAuthor(_item: ProviderAPIResponse): import('../types/index.js').User { // Default author - in real implementation this would come from the API response return this.createUser('unknown', 'Unknown User'); } createUser(username: string, displayName: string): import('../types/index.js').User { return { id: username, username, displayName, provider: 'gitlab' as const, // Will be set properly in real implementation }; } createProviderFields( item: ProviderAPIResponse, provider: string, ): import('../types/index.js').ProviderSpecificFields { if (provider === 'gitlab') { return { iid: item.iid ?? 0, projectId: 0, // Would be extracted from API response weight: 'weight' in item && typeof item.weight === 'number' ? item.weight : undefined, confidential: false, } as import('../types/index.js').GitLabSpecificFields; } else if (provider === 'github') { return { number: item.number ?? 0, repository: 'unknown/unknown', stateReason: undefined, } as import('../types/index.js').GitHubSpecificFields; } else { return { workItemId: typeof item.id === 'number' ? item.id : 0, workItemType: item.type ?? 'Issue', state: item.state ?? item.status ?? 'New', } as import('../types/index.js').AzureSpecificFields; } } // Removed unused normalizeMilestone method normalizeLabels(item: ProviderAPIResponse): string[] { if (item.labels && Array.isArray(item.labels)) { const validLabels: Array<string | { name: string } | { title: string }> = item.labels.filter(isLabelLike); const labelTexts: string[] = validLabels.map((label) => typeof label === 'string' ? label : ('name' in label ? label.name : label.title) || '', ); return labelTexts.filter((labelText) => labelText.length > 0); } Eif (item.tags && Array.isArray(item.tags)) { return item.tags.filter((tag): tag is string => typeof tag === 'string'); } return []; } async searchProjects(query?: string): Promise<ProjectCacheData[]> { // Try to use cached data first const cachedResults = this.cacheManager.searchProjects(query); if (cachedResults.length > 0) { process.stderr.write(`[cache] Serving ${cachedResults.length} projects from cache\n`); return cachedResults; } // If no cache or cache is empty, fetch fresh data process.stderr.write('[cache] No cached projects found, fetching fresh data...\n'); const projects: ProjectCacheData[] = []; for (const provider of this.providerManager.getAllProviders()) { if (provider.status !== 'connected') continue; // Check if we have valid cache for this provider if (this.cacheManager.hasValidCache(provider.id, 'projects')) { const cached = this.cacheManager.getProjects(provider.id); if (cached) { projects.push(...cached); continue; // Skip API call for this provider } } // Fetch fresh data for this provider const providerProjects = await this.fetchProjectsFromProvider(provider.id); projects.push(...providerProjects); // Cache the results for this provider this.cacheManager.setProjects(provider.id, providerProjects); } // Apply query filter if provided if (query) { const searchTerm = query.toLowerCase(); return projects.filter( (project) => project.name.toLowerCase().includes(searchTerm) || project.id.toLowerCase().includes(searchTerm) || project.description?.toLowerCase().includes(searchTerm), ); } return projects; } /** * Fetch projects from a specific provider (used for cache warming) */ async fetchProjectsFromProvider(providerId: string): Promise<ProjectCacheData[]> { const provider = this.providerManager.getProvider(providerId); if (!provider || provider.status !== 'connected') { return []; } const projects: ProjectCacheData[] = []; // Try different project listing tools based on provider const projectTools = [ 'list_repositories', // Direct repo listing (preferred) 'list_user_repositories', // User's repos 'list_projects', // GitLab/Azure projects 'list_organizations', // GitHub organizations 'list_groups', // GitLab groups 'search_repositories', // GitHub search with user:username ]; for (const toolSuffix of projectTools) { const toolName = `${providerId}_${toolSuffix}`; if (provider.tools.has(toolName)) { try { // Prepare parameters based on tool type let toolParams = {}; if (toolName.includes('search_repositories')) { // For GitHub search, we need to get the authenticated username first let username = ''; if (providerId === 'github') { // Try to get username from get_me tool const getMeTool = `${providerId}_get_me`; if (provider.tools.has(getMeTool)) { try { const meResult = await this.providerManager.callTool(getMeTool, {}); if (hasTextContent(meResult)) { const meJson = meResult.content[0].text; const meData = JSON.parse(meJson) as Record<string, unknown>; username = typeof meData.login === 'string' ? meData.login : ''; } } catch { // If get_me fails, continue without username } } } if (username) { toolParams = { query: `user:${username}` }; } else { // Skip search if we can't get username continue; } } const result = await this.providerManager.callTool(toolName, toolParams); if (hasTextContent(result)) { const projectsJson = result.content[0].text; const parsedProjects: unknown = JSON.parse(projectsJson); // Handle both array responses and GitHub-style object responses let projectArray: unknown[] = []; if (Array.isArray(parsedProjects)) { projectArray = parsedProjects; } else if (typeof parsedProjects === 'object' && parsedProjects !== null) { // GitHub search API returns { items: [...], total_count: number } const searchResult = parsedProjects as Record<string, unknown>; if (Array.isArray(searchResult.items)) { projectArray = searchResult.items; } } if (projectArray.length > 0) { for (const project of projectArray) { if (typeof project === 'object' && project !== null) { const proj = project as Record<string, unknown>; const projectId = typeof proj.full_name === 'string' ? proj.full_name : typeof proj.path_with_namespace === 'string' ? proj.path_with_namespace : typeof proj.name === 'string' ? proj.name : typeof proj.id === 'string' || typeof proj.id === 'number' ? String(proj.id) : ''; const projectName = typeof proj.name === 'string' ? proj.name : typeof proj.title === 'string' ? proj.title : projectId; if (projectId && projectName) { // Also fetch project members if available const members = await this.fetchProjectMembers(providerId, projectId); projects.push({ id: `${providerId}:${projectId}`, name: projectName, provider: providerId, description: typeof proj.description === 'string' ? proj.description : undefined, url: typeof proj.html_url === 'string' ? proj.html_url : typeof proj.web_url === 'string' ? proj.web_url : undefined, members, }); } } } } } break; // Found a working tool for this provider } catch { // Continue to next tool continue; } } } return projects; } /** * Fetch project members for a specific project */ async fetchProjectMembers(providerId: string, projectPath: string): Promise<UserRole[]> { const provider = this.providerManager.getProvider(providerId); if (!provider || provider.status !== 'connected') { return []; } const memberTools = [ 'list_project_members', 'get_project_members', 'list_collaborators', 'get_collaborators', 'list_team_members', ]; for (const toolSuffix of memberTools) { const toolName = `${providerId}_${toolSuffix}`; if (provider.tools.has(toolName)) { try { const result = await this.providerManager.callTool(toolName, { project: projectPath, project_id: projectPath, repo: projectPath, repository: projectPath, }); if (hasTextContent(result)) { const membersJson = result.content[0].text; const parsedMembers: unknown = JSON.parse(membersJson); if (Array.isArray(parsedMembers)) { return parsedMembers.map((member) => { const mem = member as Record<string, unknown>; return { userId: String(mem.id ?? mem.user_id ?? mem.username), username: String(mem.username ?? mem.login ?? mem.name), displayName: String(mem.name ?? mem.display_name ?? mem.username ?? mem.login), email: typeof mem.email === 'string' ? mem.email : undefined, role: typeof mem.role === 'string' ? mem.role : typeof mem.permission === 'string' ? mem.permission : typeof mem.access_level === 'string' ? mem.access_level : 'member', accessLevel: typeof mem.access_level === 'number' ? mem.access_level : undefined, } as UserRole; }); } } break; // Found a working tool } catch { // Continue to next tool continue; } } } return []; // No members found or no working tool } /** * Handle cache expiration - trigger refresh */ private async handleCacheExpired(cacheKey: string): Promise<void> { const [type, provider] = cacheKey.split(':'); if (type === 'projects') { process.stderr.write(`[cache] Refreshing projects cache for ${provider}...\n`); try { const projects = await this.fetchProjectsFromProvider(provider); this.cacheManager.setProjects(provider, projects); } catch (error) { process.stderr.write( `[cache] Failed to refresh projects for ${provider}: ${String(error)}\n`, ); } } else if (type === 'users') { process.stderr.write(`[cache] Refreshing users cache for ${provider}...\n`); try { const users = await this.fetchUsersFromProvider(provider); this.cacheManager.setUsers(provider, users); } catch (error) { process.stderr.write(`[cache] Failed to refresh users for ${provider}: ${String(error)}\n`); } } } /** * Fetch users from a specific provider (used for cache warming) */ async fetchUsersFromProvider(providerId: string): Promise<UserRole[]> { const provider = this.providerManager.getProvider(providerId); if (!provider || provider.status !== 'connected') { return []; } const users: UserRole[] = []; // Try different user listing tools based on provider const userTools = [ 'search_users', // GitHub has this 'list_users', 'get_users', 'list_org_members', 'get_org_members', 'list_team_members', ]; for (const toolSuffix of userTools) { const toolName = `${providerId}_${toolSuffix}`; if (provider.tools.has(toolName)) { try { // Prepare parameters based on tool type let toolParams = {}; if (toolName.includes('search_users')) { // GitHub search_users requires a q parameter toolParams = { q: 'type:user' }; // Search for users } const result = await this.providerManager.callTool(toolName, toolParams); if (hasTextContent(result)) { const usersJson = result.content[0].text; const parsedUsers: unknown = JSON.parse(usersJson); // Handle both array responses and GitHub-style object responses let userArray: unknown[] = []; if (Array.isArray(parsedUsers)) { userArray = parsedUsers; } else if (typeof parsedUsers === 'object' && parsedUsers !== null) { // GitHub search API returns { items: [...], total_count: number } const searchResult = parsedUsers as Record<string, unknown>; if (Array.isArray(searchResult.items)) { userArray = searchResult.items; } } if (userArray.length > 0) { for (const user of userArray) { if (typeof user === 'object' && user !== null) { const usr = user as Record<string, unknown>; users.push({ userId: String(usr.id ?? usr.user_id ?? usr.username), username: String(usr.username ?? usr.login ?? usr.name), displayName: String(usr.name ?? usr.display_name ?? usr.username ?? usr.login), email: typeof usr.email === 'string' ? usr.email : undefined, role: typeof usr.role === 'string' ? usr.role : typeof usr.permission === 'string' ? usr.permission : typeof usr.access_level === 'string' ? usr.access_level : 'member', accessLevel: typeof usr.access_level === 'number' ? usr.access_level : undefined, }); } } } } break; // Found a working tool for this provider } catch { // Continue to next tool continue; } } } return users; } /** * Search users across all providers (with caching) */ async searchUsers(query?: string): Promise<UserRole[]> { // Try to use cached data first const cachedResults = this.cacheManager.searchUsers(query); if (cachedResults.length > 0) { process.stderr.write(`[cache] Serving ${cachedResults.length} users from cache\n`); return cachedResults; } // If no cache or cache is empty, fetch fresh data process.stderr.write('[cache] No cached users found, fetching fresh data...\n'); const users: UserRole[] = []; for (const provider of this.providerManager.getAllProviders()) { if (provider.status !== 'connected') continue; // Check if we have valid cache for this provider if (this.cacheManager.hasValidCache(provider.id, 'users')) { const cached = this.cacheManager.getUsers(provider.id); if (cached) { users.push(...cached); continue; // Skip API call for this provider } } // Fetch fresh data for this provider const providerUsers = await this.fetchUsersFromProvider(provider.id); users.push(...providerUsers); // Cache the results for this provider this.cacheManager.setUsers(provider.id, providerUsers); } // Apply query filter if provided if (query) { const searchTerm = query.toLowerCase(); return users.filter( (user) => user.username.toLowerCase().includes(searchTerm) || user.displayName.toLowerCase().includes(searchTerm) || user.email?.toLowerCase().includes(searchTerm), ); } return users; } /** * Get users for a specific project (from cache) */ getProjectUsers(projectId: string): UserRole[] { return this.cacheManager.getProjectUsers(projectId); } /** * Warm up caches for all connected providers */ async warmupCaches(): Promise<void> { process.stderr.write('[cache] Starting cache warmup...\n'); const providers = this.providerManager .getAllProviders() .filter((p) => p.status === 'connected'); // Warm up projects and users in parallel for each provider const warmupPromises = providers.map(async (provider) => { try { // Warm up projects cache process.stderr.write(`[cache] Warming up projects for ${provider.id}...\n`); const projects = await this.fetchProjectsFromProvider(provider.id); this.cacheManager.setProjects(provider.id, projects); // Warm up users cache process.stderr.write(`[cache] Warming up users for ${provider.id}...\n`); const users = await this.fetchUsersFromProvider(provider.id); this.cacheManager.setUsers(provider.id, users); process.stderr.write(`[cache] Warmup completed for ${provider.id}\n`); } catch (error) { process.stderr.write(`[cache] Warmup failed for ${provider.id}: ${String(error)}\n`); } }); await Promise.all(warmupPromises); process.stderr.write('[cache] Cache warmup completed for all providers\n'); } /** * Get cache statistics */ getCacheStats() { return this.cacheManager.getStats(); } /** * Get cache manager instance (for external use) */ getCacheManager(): CacheManager { return this.cacheManager; } createUnifiedTools(): Tool[] { return [ { name: 'nexus_search_projects', description: 'FIRST STEP: Search for projects/repositories across all providers when you need to find a project by partial name. This returns full project paths with provider prefixes (e.g., "gitlab:lantec/uscribe/recipes") that you can then use with nexus_list_work_items. ALWAYS use this first when user mentions project names like "recipes", "backend", etc. Results are cached for 15 minutes.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Optional search query to filter projects by name or description. If omitted, lists all available projects.', }, }, }, }, { name: 'nexus_search_users', description: 'Search for users across all configured providers. Returns user information including roles and access levels in projects. Results are cached for 15 minutes.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Optional search query to filter users by username, display name, or email. If omitted, lists all available users.', }, }, }, }, { name: 'nexus_get_project_users', description: 'Get users/members for a specific project. Returns cached user data with their roles in the project.', inputSchema: { type: 'object', required: ['project_id'], properties: { project_id: { type: 'string', description: 'Project identifier in format "provider:project_path" (e.g., "gitlab:myorg/myproject")', }, }, }, }, { name: 'nexus_cache_stats', description: 'Get cache statistics showing what data is cached and cache age/TTL information.', inputSchema: { type: 'object', properties: {}, }, }, { name: 'nexus_list_work_items', description: 'List work items (issues, tasks, etc.) from a SPECIFIC project. REQUIRES full project path with provider prefix (e.g., "gitlab:lantec/uscribe/backend", "github:microsoft/vscode"). IMPORTANT: If you only know partial project name (like "recipes" or "backend"), you MUST first use nexus_search_projects to find the full project path. DO NOT pass raw project names without provider prefix - this will cause errors. Examples of correct usage: project="gitlab:myorg/myrepo", project="github:owner/repo", project="azure:projectname".', inputSchema: { type: 'object', required: ['project'], properties: { project: { type: 'string', description: 'REQUIRED: Project identifier with provider prefix (e.g., "github:owner/repo", "gitlab:group/project", "azure:projectname"). NEVER use raw project names like "recipes" or "backend" - use nexus_search_projects first to find the full path.', }, status: { type: 'string', enum: ['open', 'closed', 'in_progress', 'all'], description: 'Filter by status', }, assignee: { type: 'string', description: 'Filter by assignee username', }, labels: { type: 'array', items: { type: 'string' }, description: 'Filter by labels', }, }, }, }, { name: 'nexus_create_work_item', description: 'Create a new work item in the specified project', inputSchema: { type: 'object', required: ['project', 'title'], properties: { project: { type: 'string', description: 'Project identifier (e.g., "github:owner/repo")', }, title: { type: 'string', description: 'Title of the work item', }, description: { type: 'string', description: 'Description or body of the work item', }, type: { type: 'string', enum: ['issue', 'task', 'epic', 'story', 'bug'], description: 'Type of work item', }, assignee: { type: 'string', description: 'Username to assign the item to', }, labels: { type: 'array', items: { type: 'string' }, description: 'Labels to apply', }, priority: { type: 'string', enum: ['low', 'normal', 'high', 'critical'], description: 'Priority level', }, }, }, }, { name: 'nexus_update_work_item', description: 'Update an existing work item', inputSchema: { type: 'object', required: ['id'], properties: { id: { type: 'string', description: 'Work item identifier (e.g., "github:123")', }, title: { type: 'string', description: 'New title', }, description: { type: 'string', description: 'New description', }, status: { type: 'string', enum: ['open', 'closed', 'in_progress'], description: 'New status', }, assignee: { type: 'string', description: 'New assignee username', }, labels: { type: 'array', items: { type: 'string' }, description: 'New labels', }, priority: { type: 'string', enum: ['low', 'normal', 'high', 'critical'], description: 'New priority', }, }, }, }, { name: 'nexus_transfer_work_item', description: 'Transfer a work item from one project/platform to another', inputSchema: { type: 'object', required: ['source_id', 'target_project'], properties: { source_id: { type: 'string', description: 'Source work item identifier (e.g., "gitlab:123")', }, target_project: { type: 'string', description: 'Target project identifier (e.g., "github:owner/repo")', }, close_source: { type: 'boolean', description: 'Whether to close the source item after transfer', default: true, }, }, }, }, ]; } } |