UNPKG

n8n

Version:

n8n Workflow Automation Tool

441 lines • 18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.searchKnowledgeOutputSchema = exports.searchKnowledgeInputSchema = exports.searchKnowledgeParsingSchema = exports.csvQueryInputSchema = exports.csvFilterSchema = exports.KNOWLEDGE_OPERATIONS = exports.DEFAULT_SEARCH_HEAD_LIMIT = void 0; exports.parseSearchKnowledgeInput = parseSearchKnowledgeInput; exports.getSearchKnowledgeOperation = getSearchKnowledgeOperation; const zod_1 = require("zod"); exports.DEFAULT_SEARCH_HEAD_LIMIT = 250; exports.KNOWLEDGE_OPERATIONS = [ 'list', 'search', 'read', 'csv_query', 'csv_profile', 'csv_distinct', 'csv_aggregate', ]; const lineRangeSchema = zod_1.z.object({ start: zod_1.z.number().int().min(1), end: zod_1.z.number().int().min(1), }); const searchOutputModeSchema = zod_1.z.enum(['files_with_matches', 'content', 'count']); const searchMatchModeSchema = zod_1.z.enum(['any', 'all_on_same_line', 'all_within_lines']); const csvAggregateFunctionSchema = zod_1.z.enum(['count', 'min', 'max', 'sum', 'avg']); exports.csvFilterSchema = zod_1.z.discriminatedUnion('op', [ zod_1.z.object({ column: zod_1.z.string().min(1), op: zod_1.z.literal('eq'), value: zod_1.z.string(), }), zod_1.z.object({ column: zod_1.z.string().min(1), op: zod_1.z.literal('in'), value: zod_1.z.array(zod_1.z.string()).min(1).max(50), }), zod_1.z.object({ column: zod_1.z.string().min(1), op: zod_1.z.literal('contains'), value: zod_1.z.string(), }), ]); const listInputSchema = zod_1.z.object({ operation: zod_1.z.literal('list') }).strict(); const searchInputSchema = zod_1.z .object({ operation: zod_1.z.literal('search'), query: zod_1.z.string().min(1).optional(), queries: zod_1.z.array(zod_1.z.string().min(1)).min(1).max(5).optional(), match_mode: searchMatchModeSchema.default('any'), output_mode: searchOutputModeSchema.default('files_with_matches'), caseInsensitive: zod_1.z.boolean().optional(), fixedStrings: zod_1.z.boolean().optional(), context: zod_1.z.number().int().min(0).max(5).optional(), file: zod_1.z.string().min(1).optional(), files: zod_1.z.array(zod_1.z.string()).max(10).optional(), offset: zod_1.z.number().int().min(0).default(0), head_limit: zod_1.z.number().int().min(0).default(exports.DEFAULT_SEARCH_HEAD_LIMIT), }) .strict(); const readInputSchema = zod_1.z .object({ operation: zod_1.z.literal('read'), file: zod_1.z.string().min(1), lineRange: lineRangeSchema.optional(), }) .strict(); exports.csvQueryInputSchema = zod_1.z .object({ operation: zod_1.z.literal('csv_query'), file: zod_1.z.string().min(1), select: zod_1.z.array(zod_1.z.string().min(1)).min(1).max(50).optional(), where: zod_1.z.array(exports.csvFilterSchema).max(10).optional(), rowNumber: zod_1.z.number().int().min(2).optional(), limit: zod_1.z.number().int().min(1).max(100).default(20), }) .strict(); const csvProfileInputSchema = zod_1.z .object({ operation: zod_1.z.literal('csv_profile'), file: zod_1.z.string().min(1), sampleSize: zod_1.z.number().int().min(1).max(20).default(5), distinctLimit: zod_1.z.number().int().min(10).max(500).default(100), }) .strict(); const csvDistinctInputSchema = zod_1.z .object({ operation: zod_1.z.literal('csv_distinct'), file: zod_1.z.string().min(1), column: zod_1.z.string().min(1), where: zod_1.z.array(exports.csvFilterSchema).max(10).optional(), limit: zod_1.z.number().int().min(1).max(200).default(50), }) .strict(); const csvAggregateInputSchema = zod_1.z .object({ operation: zod_1.z.literal('csv_aggregate'), file: zod_1.z.string().min(1), metric: zod_1.z.string().min(1).optional(), metrics: zod_1.z.array(zod_1.z.string().min(1)).min(1).max(10).optional(), functions: zod_1.z.array(csvAggregateFunctionSchema).min(1).max(5).default(['count']), where: zod_1.z.array(exports.csvFilterSchema).max(10).optional(), groupBy: zod_1.z.array(zod_1.z.string().min(1)).min(1).max(5).optional(), orderBy: zod_1.z .object({ column: zod_1.z.string().min(1), direction: zod_1.z.enum(['asc', 'desc']).default('asc'), }) .strict() .optional(), limit: zod_1.z.number().int().min(1).max(200).default(50), }) .strict(); exports.searchKnowledgeParsingSchema = zod_1.z.discriminatedUnion('operation', [ listInputSchema, searchInputSchema, readInputSchema, exports.csvQueryInputSchema, csvProfileInputSchema, csvDistinctInputSchema, csvAggregateInputSchema, ]); exports.searchKnowledgeInputSchema = { type: 'object', description: 'Use exactly one operation shape. Do not include fields from other operations. ' + 'Use csv_profile for unfamiliar CSVs, csv_query for rows, csv_distinct for values, and csv_aggregate for computed CSV answers.', additionalProperties: false, required: ['operation'], properties: { operation: { type: 'string', description: 'Operation to perform. Allowed values: list, search, read, csv_query, csv_profile, csv_distinct, csv_aggregate.', }, query: { type: 'string', minLength: 1, description: 'For operation=search only: search pattern. For conceptual multi-term lookup, prefer queries with match_mode instead of writing regex by hand.', }, queries: { type: 'array', minItems: 1, maxItems: 5, items: { type: 'string', minLength: 1 }, description: 'For operation=search only: multiple literal search terms for conceptual lookup without hand-written regex.', }, match_mode: { type: 'string', default: 'any', description: 'For operation=search with queries only: any, all_on_same_line, or all_within_lines. Use all_within_lines to find concepts near each other without regex.', }, output_mode: { type: 'string', description: 'For operation=search only: content shows matching lines, files_with_matches shows only matching files (default), count shows match counts. Use content only after narrowing to a file or exact phrase.', default: 'files_with_matches', }, caseInsensitive: { type: 'boolean', description: 'For operation=search only: run case-insensitive search.', }, fixedStrings: { type: 'boolean', description: 'For operation=search only: treat query as a fixed string instead of a regex. Defaults to true.', }, context: { type: 'integer', minimum: 0, maximum: 5, description: 'For operation=search only: number of surrounding context lines. Requires output_mode=content.', }, files: { type: 'array', maxItems: 10, items: { type: 'string' }, description: 'For operation=search only: optional file ids, relative paths, or exact file names to search. These are tool handles only; do not cite them to users.', }, offset: { type: 'integer', minimum: 0, default: 0, description: 'For operation=search only: number of files, counts, or matches to skip.', }, head_limit: { type: 'integer', minimum: 0, default: exports.DEFAULT_SEARCH_HEAD_LIMIT, description: 'For operation=search only: limit output to first N files/counts/lines. Defaults to 250. Pass 0 for unlimited.', }, file: { type: 'string', minLength: 1, description: 'For operation=read or CSV operations: file id, relative path, or exact file name. For operation=search: alias for a single files entry. This is a tool handle only; cite the returned fileName and lineRange instead.', }, lineRange: { type: 'object', additionalProperties: false, description: 'For operation=read only: optional line range to read.', properties: { start: { type: 'integer', minimum: 1 }, end: { type: 'integer', minimum: 1 }, }, }, where: { type: 'array', maxItems: 10, description: 'For CSV operations only: row filters ANDed together. Each filter has column, op, and value. Allowed op values: eq, in, contains. For op=in, value must be an array of strings.', items: { type: 'object', additionalProperties: true, required: ['column', 'op', 'value'], properties: { column: { type: 'string', minLength: 1 }, op: { type: 'string', description: 'Allowed values: eq, in, contains.', }, value: { description: 'String value for eq/contains, or array of strings for in. Local validation enforces the exact shape.', }, }, }, }, select: { type: 'array', minItems: 1, maxItems: 50, items: { type: 'string', minLength: 1 }, description: 'For operation=csv_query only: columns to return. Omit with rowNumber to return all columns for that row.', }, rowNumber: { type: 'integer', minimum: 2, description: 'For operation=csv_query only: exact CSV file line number to fetch. Header is line 1, so data rows usually start at line 2.', }, column: { type: 'string', minLength: 1, description: 'For operation=csv_distinct only: column whose values should be returned.', }, metric: { type: 'string', minLength: 1, description: 'For operation=csv_aggregate only: numeric metric column for min, max, sum, or avg.', }, metrics: { type: 'array', minItems: 1, maxItems: 10, items: { type: 'string', minLength: 1 }, description: 'For operation=csv_aggregate only: numeric metric columns for min, max, sum, or avg.', }, functions: { type: 'array', minItems: 1, maxItems: 5, items: { type: 'string', enum: ['count', 'min', 'max', 'sum', 'avg'] }, default: ['count'], description: 'For operation=csv_aggregate only: aggregate functions to compute. count does not require a metric.', }, groupBy: { type: 'array', minItems: 1, maxItems: 5, items: { type: 'string', minLength: 1 }, description: 'For operation=csv_aggregate only: columns to group aggregate results by.', }, orderBy: { type: 'object', additionalProperties: false, description: 'For operation=csv_aggregate only: sort grouped output by a group column or aggregate output column.', properties: { column: { type: 'string', minLength: 1 }, direction: { type: 'string', enum: ['asc', 'desc'], default: 'asc' }, }, }, sampleSize: { type: 'integer', minimum: 1, maximum: 20, default: 5, description: 'For operation=csv_profile only: number of sample rows to return.', }, distinctLimit: { type: 'integer', minimum: 10, maximum: 500, default: 100, description: 'For operation=csv_profile only: maximum distinct values tracked per column before marking that column as truncated.', }, limit: { type: 'integer', minimum: 1, maximum: 200, default: 20, description: 'For CSV operations only: maximum rows, groups, or distinct values to return. Defaults to 20 for csv_query, 50 for csv_distinct, and 50 for csv_aggregate.', }, }, }; const knowledgeFileOutputSchema = zod_1.z.object({ id: zod_1.z.string(), fileName: zod_1.z.string(), mimeType: zod_1.z.string(), fileSizeBytes: zod_1.z.number(), relativePath: zod_1.z.string(), }); const commandResultOutputSchema = zod_1.z.object({ command: zod_1.z.enum(['git_grep', 'cat', 'sed']), exitCode: zod_1.z.number().nullable(), stdout: zod_1.z.string(), stderr: zod_1.z.string(), truncated: zod_1.z.boolean(), citation: zod_1.z .object({ fileName: zod_1.z.string(), lineRange: lineRangeSchema.optional(), instruction: zod_1.z.string(), }) .optional(), }); const searchMatchOutputSchema = zod_1.z.object({ fileId: zod_1.z.string(), fileName: zod_1.z.string(), relativePath: zod_1.z.string(), lineNumber: zod_1.z.number(), text: zod_1.z.string(), readRange: lineRangeSchema, truncated: zod_1.z.boolean().optional(), }); const searchFileOutputSchema = zod_1.z.object({ id: zod_1.z.string(), fileName: zod_1.z.string(), relativePath: zod_1.z.string(), matchCount: zod_1.z.number(), }); const searchResultOutputSchema = zod_1.z.object({ mode: searchOutputModeSchema, query: zod_1.z.string(), queries: zod_1.z.array(zod_1.z.string()).optional(), matchMode: searchMatchModeSchema.optional(), totalMatchingFiles: zod_1.z.number(), totalMatchingLines: zod_1.z.number(), files: zod_1.z.array(searchFileOutputSchema), matches: zod_1.z.array(searchMatchOutputSchema), truncated: zod_1.z.boolean(), appliedLimit: zod_1.z.number().optional(), appliedOffset: zod_1.z.number().optional(), nextOffset: zod_1.z.number().optional(), hint: zod_1.z.string().optional(), }); const csvQueryResultOutputSchema = zod_1.z.object({ fileName: zod_1.z.string(), relativePath: zod_1.z.string(), columns: zod_1.z.array(zod_1.z.string()), rowNumbers: zod_1.z.array(zod_1.z.number()), rows: zod_1.z.array(zod_1.z.array(zod_1.z.string())), records: zod_1.z .array(zod_1.z.object({ rowNumber: zod_1.z.number(), fileLineNumber: zod_1.z.number(), values: zod_1.z.record(zod_1.z.string(), zod_1.z.string()), })) .optional(), rowCount: zod_1.z.number(), truncated: zod_1.z.boolean(), rowNumberBase: zod_1.z.string().optional(), ambiguity: zod_1.z .object({ matchedRows: zod_1.z.number(), message: zod_1.z.string(), suggestedColumns: zod_1.z.array(zod_1.z.string()), sampleDistinctValues: zod_1.z.record(zod_1.z.string(), zod_1.z.array(zod_1.z.string())).optional(), }) .optional(), }); const csvColumnProfileOutputSchema = zod_1.z.object({ name: zod_1.z.string(), inferredType: zod_1.z.enum(['empty', 'integer', 'number', 'boolean', 'date', 'string']), emptyCount: zod_1.z.number(), distinctCount: zod_1.z.number().optional(), distinctCountTruncated: zod_1.z.boolean().optional(), sampleValues: zod_1.z.array(zod_1.z.string()).optional(), }); const csvProfileOutputSchema = zod_1.z.object({ fileName: zod_1.z.string(), relativePath: zod_1.z.string(), columns: zod_1.z.array(zod_1.z.string()), rowCount: zod_1.z.number(), sampleRows: zod_1.z.array(zod_1.z.record(zod_1.z.string(), zod_1.z.string())), columnProfiles: zod_1.z.array(csvColumnProfileOutputSchema), likelyKeyColumns: zod_1.z.array(zod_1.z.string()), likelyDisambiguatingColumns: zod_1.z.array(zod_1.z.string()), }); const csvDistinctOutputSchema = zod_1.z.object({ fileName: zod_1.z.string(), relativePath: zod_1.z.string(), column: zod_1.z.string(), values: zod_1.z.array(zod_1.z.string()), distinctCount: zod_1.z.number(), truncated: zod_1.z.boolean(), }); const csvAggregateOutputSchema = zod_1.z.object({ fileName: zod_1.z.string(), relativePath: zod_1.z.string(), rowCount: zod_1.z.number(), functions: zod_1.z.array(csvAggregateFunctionSchema), metrics: zod_1.z.array(zod_1.z.string()), groupBy: zod_1.z.array(zod_1.z.string()).optional(), results: zod_1.z.array(zod_1.z.record(zod_1.z.string(), zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.null()]))), truncated: zod_1.z.boolean(), skippedNonNumeric: zod_1.z.record(zod_1.z.string(), zod_1.z.number()).optional(), }); exports.searchKnowledgeOutputSchema = zod_1.z.object({ operation: zod_1.z.enum(exports.KNOWLEDGE_OPERATIONS), files: zod_1.z.array(knowledgeFileOutputSchema), result: commandResultOutputSchema.optional(), search: searchResultOutputSchema.optional(), csv: csvQueryResultOutputSchema.optional(), csvProfile: csvProfileOutputSchema.optional(), csvDistinct: csvDistinctOutputSchema.optional(), csvAggregate: csvAggregateOutputSchema.optional(), error: zod_1.z.string().optional(), }); function parseSearchKnowledgeInput(input) { const parsed = exports.searchKnowledgeParsingSchema.parse(input); if (parsed.operation !== 'search' || parsed.file === undefined) return parsed; const { file, ...searchInput } = parsed; const files = Array.from(new Set([file, ...(parsed.files ?? [])])); if (files.length > 10) { throw new Error('Search can target at most 10 files.'); } return { ...searchInput, files, }; } function getSearchKnowledgeOperation(input) { const parsed = zod_1.z .object({ operation: zod_1.z.enum(exports.KNOWLEDGE_OPERATIONS), }) .safeParse(input); return parsed.success ? parsed.data.operation : 'list'; } //# sourceMappingURL=schemas.js.map