2024-09-10 06:16:41 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
|
|
export type SearchRequest = z.infer<typeof searchRequestSchema>;
|
|
|
|
|
export const searchRequestSchema = z.object({
|
|
|
|
|
query: z.string(),
|
|
|
|
|
numResults: z.number(),
|
2024-09-10 19:24:12 +00:00
|
|
|
whole: z.boolean().optional(),
|
2024-09-10 06:16:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type SearchResponse = z.infer<typeof searchResponseSchema>;
|
|
|
|
|
export type SearchResult = SearchResponse["Result"];
|
2024-09-10 19:24:12 +00:00
|
|
|
export type SearchResultFile = NonNullable<SearchResult["Files"]>[number];
|
2024-09-10 06:16:41 +00:00
|
|
|
export type SearchResultFileMatch = SearchResultFile["ChunkMatches"][number];
|
|
|
|
|
export type SearchResultRange = z.infer<typeof rangeSchema>;
|
|
|
|
|
export type SearchResultLocation = z.infer<typeof locationSchema>;
|
|
|
|
|
|
|
|
|
|
// @see : https://github.com/TaqlaAI/zoekt/blob/main/api.go#L212
|
|
|
|
|
const locationSchema = z.object({
|
|
|
|
|
// 0-based byte offset from the beginning of the file
|
|
|
|
|
ByteOffset: z.number(),
|
|
|
|
|
// 1-based line number from the beginning of the file
|
|
|
|
|
LineNumber: z.number(),
|
|
|
|
|
// 1-based column number (in runes) from the beginning of line
|
|
|
|
|
Column: z.number(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const rangeSchema = z.object({
|
|
|
|
|
Start: locationSchema,
|
|
|
|
|
End: locationSchema,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const searchResponseSchema = z.object({
|
|
|
|
|
Result: z.object({
|
|
|
|
|
Duration: z.number(),
|
|
|
|
|
FileCount: z.number(),
|
2024-09-10 19:59:42 +00:00
|
|
|
MatchCount: z.number(),
|
2024-09-10 06:16:41 +00:00
|
|
|
Files: z.array(z.object({
|
|
|
|
|
FileName: z.string(),
|
|
|
|
|
Repository: z.string(),
|
|
|
|
|
Version: z.string(),
|
|
|
|
|
Language: z.string(),
|
|
|
|
|
Branches: z.array(z.string()),
|
|
|
|
|
ChunkMatches: z.array(z.object({
|
|
|
|
|
Content: z.string(),
|
|
|
|
|
Ranges: z.array(rangeSchema),
|
|
|
|
|
FileName: z.boolean(),
|
|
|
|
|
ContentStart: locationSchema,
|
|
|
|
|
Score: z.number(),
|
|
|
|
|
})),
|
|
|
|
|
Checksum: z.string(),
|
|
|
|
|
Score: z.number(),
|
|
|
|
|
// Set if `whole` is true.
|
2024-09-10 19:24:12 +00:00
|
|
|
Content: z.string().optional(),
|
|
|
|
|
})).nullable(),
|
2024-09-10 06:16:41 +00:00
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type FileSourceRequest = z.infer<typeof fileSourceRequestSchema>;
|
|
|
|
|
export const fileSourceRequestSchema = z.object({
|
|
|
|
|
fileName: z.string(),
|
|
|
|
|
repository: z.string()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type FileSourceResponse = z.infer<typeof fileSourceResponseSchema>;
|
|
|
|
|
|
|
|
|
|
export const fileSourceResponseSchema = z.object({
|
|
|
|
|
source: z.string(),
|
|
|
|
|
});
|