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 | 26x 26x 26x 26x 26x 26x 26x 104x 52x 52x 43x 43x 43x 9x 26x 88x 88x 87x 87x 36x 36x 36x 9x 16x 16x 16x 15x 35x 35x 35x 11x 52x 26x 9x 26x 4x 26x 3x 3x | import * as z from 'zod';
import { BrowseSearchSchema, type BrowseSearchInput } from './schema-readonly';
import { ToolRegistry, EnhancedToolDefinition } from '../../types';
import { assertActionAllowed } from '../utils';
import { gitlab, GitLabApiError, paths, toQuery } from '../../utils/gitlab-api';
import { stripProjectSecrets } from '../core/project-response';
import { StructuredToolError } from '../../utils/error-handler';
// GitLab 16.0 lib/api/helpers.rb:521-522 wraps object errors in message;
// the REST client serializes that object with JSON.stringify.
// Match complete messages so quoted query text is not mistaken for a backend refusal.
const UNAVAILABLE_SCOPE_ERRORS = new Set(
[
'scope does not have a valid value',
'Scope not supported without Elasticsearch!',
'Scope supported only with advanced search',
'Scope supported only with advanced search or exact code search',
].flatMap((message) => [message, JSON.stringify({ error: message })]),
);
/** Cross-project search availability depends on the configured search backend, not only tier. */
function throwSearchError(error: unknown, input: BrowseSearchInput): never {
const { scope } = input;
// GitLab 16.0 ee/lib/ee/api/search.rb:19-27; newer releases also allow exact code search.
if (
error instanceof GitLabApiError &&
error.status === 400 &&
input.action !== 'project' &&
(scope === 'blobs' || scope === 'commits' || scope === 'notes' || scope === 'wiki_blobs') &&
UNAVAILABLE_SCOPE_ERRORS.has(error.details)
) {
const backend = scope === 'blobs' ? 'Advanced Search or exact code search' : 'Advanced Search';
const suggestedFix = `Use browse_search with action 'project', project_id and scope '${scope}' to search within one project.`;
throw new StructuredToolError({
error_code: 'API_ERROR',
tool: 'browse_search',
action: input.action,
http_status: error.status,
message: `GitLab rejected ${input.action} scope '${scope}': ${backend} is unavailable for this search.`,
suggested_fix: suggestedFix,
gitlab_error: error.details,
});
}
throw error;
}
/**
* Search tools registry - 1 read-only CQRS tool
*
* browse_search (Query): global, project, group
*
* Search is read-only by design - no manage_search tool needed.
*/
export const searchToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
// ============================================================================
// browse_search - CQRS Query Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
'browse_search',
{
name: 'browse_search',
description:
'Search across GitLab resources globally or within a scope. Actions: global (entire instance), project (within specific project), group (within specific group). Searchable: projects, issues, merge_requests, milestones, users, blobs (code), commits, wiki_blobs, notes.',
inputSchema: z.toJSONSchema(BrowseSearchSchema),
requirements: {
default: { tier: 'free' },
actions: {
group: { tier: 'free', notes: 'Group-scoped search' },
},
},
gate: { envVar: 'USE_SEARCH', defaultValue: true },
handler: async (args: unknown): Promise<unknown> => {
const input = BrowseSearchSchema.parse(args);
assertActionAllowed('browse_search', input.action);
try {
switch (input.action) {
case 'global': {
// TypeScript knows: input has scope, search, and optional filters
const { scope, ...params } = input;
// Build query params excluding action (not an API parameter)
const query = toQuery(params, ['action']);
// Global search endpoint
const results = await gitlab.get<unknown[]>('search', {
query: { ...query, scope },
});
return {
scope,
count: results.length,
results: scope === 'projects' ? stripProjectSecrets(results) : results,
};
}
case 'project': {
// TypeScript knows: input has project_id, scope, search, and optional filters
const { project_id, scope, ref, ...params } = input;
// Build query params excluding action (project_id, scope, ref are already destructured)
const query = toQuery(params, ['action']);
// Project-scoped search endpoint
const results = await gitlab.get<unknown[]>(`${paths.project(project_id)}/search`, {
query: { ...query, scope, ...(ref && { ref }) },
});
return {
project_id,
scope,
count: results.length,
results: scope === 'projects' ? stripProjectSecrets(results) : results,
};
}
case 'group': {
// TypeScript knows: input has group_id, scope, search, and optional filters
const { group_id, scope, ...params } = input;
// Build query params excluding action (group_id, scope are already destructured)
const query = toQuery(params, ['action']);
// Group-scoped search endpoint
const results = await gitlab.get<unknown[]>(`${paths.group(group_id)}/search`, {
query: { ...query, scope },
});
return {
group_id,
scope,
count: results.length,
results: scope === 'projects' ? stripProjectSecrets(results) : results,
};
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
} catch (error) {
throwSearchError(error, input);
}
},
},
],
]);
/**
* Get read-only tool names from the registry
* Search is entirely read-only, so all tools are read-only
*/
export function getSearchReadOnlyToolNames(): string[] {
return ['browse_search'];
}
/**
* Get all tool definitions from the registry
*/
export function getSearchToolDefinitions(): EnhancedToolDefinition[] {
return Array.from(searchToolRegistry.values());
}
/**
* Get filtered tools based on read-only mode
* Since search is read-only, this always returns all tools
*/
export function getFilteredSearchTools(readOnlyMode: boolean = false): EnhancedToolDefinition[] {
// Search is always read-only, so readOnlyMode doesn't affect filtering
void readOnlyMode;
return getSearchToolDefinitions();
}
|