UNPKG

crewai-ts

Version:

TypeScript port of crewAI for agent-based workflows

210 lines 5.58 kB
/** * JSON Knowledge Source * Process structured JSON data as knowledge sources with optimized path-based access */ import { BaseKnowledgeSource, KnowledgeSourceOptions } from './BaseKnowledgeSource.js'; import { KnowledgeChunk, Metadata } from '../types.js'; /** * Schema configuration for JSON path extraction * Defines how to transform a specific path in the JSON structure */ export interface JSONPathSchema { /** * JSON path to extract (using dot notation or array indices) */ path: string; /** * Optional name for the extracted content */ name?: string; /** * How to format the extracted value * - 'raw': Keep as is (for primitives) * - 'string': Convert to string * - 'structured': Format as key-value pairs * - 'template': Use a template string with {path} placeholders * @default 'structured' */ format?: 'raw' | 'string' | 'structured' | 'template'; /** * Template string for 'template' format * Use {path} placeholders to reference other paths */ template?: string; /** * Only apply this schema if a condition is met */ condition?: { /** * Path to check */ path: string; /** * Operator for condition */ operator: 'equals' | 'contains' | 'exists' | 'gt' | 'lt'; /** * Value to compare against */ value?: any; }; /** * Additional metadata to add to chunks created from this path */ metadata?: Metadata; } /** * Options specific to JSON knowledge sources */ export interface JSONKnowledgeSourceOptions extends KnowledgeSourceOptions { /** * JSON data to process * Can be a JSON object or a stringified JSON */ data: Record<string, any> | string; /** * Schema configuration for path extraction * If not provided, all paths will be processed */ schema?: JSONPathSchema[]; /** * Additional metadata to attach to all chunks */ metadata?: Metadata; /** * Whether to flatten arrays * @default true */ flattenArrays?: boolean; /** * Maximum depth for nested objects * @default 5 */ maxDepth?: number; /** * Maximum array items to process * @default 100 */ maxArrayItems?: number; /** * Skip null or undefined values * @default true */ skipNulls?: boolean; /** * Use dot notation for paths * @default true */ useDotNotation?: boolean; /** * Include path in chunk content * @default true */ includePath?: boolean; } /** * Knowledge source for processing structured JSON data * Optimized for efficient path-based access and semantic chunking */ export declare class JSONKnowledgeSource extends BaseKnowledgeSource { /** * JSON data to process * @private */ private jsonData; /** * Configured JSON options with defaults * @private */ private jsonOptions; /** * Schema configuration for path extraction * @private */ private schema; /** * Constructor for JSONKnowledgeSource * @param options - Configuration options */ constructor(options: JSONKnowledgeSourceOptions); /** * Process and add JSON data to storage * Implements path-based extraction and optimized chunking * @override */ add(): Promise<void>; /** * Create a knowledge chunk from JSON content * @param content - Content for the chunk * @param additionalMetadata - Additional metadata or JSON path * @returns Knowledge chunk object * @private */ protected createChunk(content: string, additionalMetadata?: Metadata | string): KnowledgeChunk; /** * Process JSON data using schema configuration * @returns Array of knowledge chunks * @private */ private processWithSchema; /** * Process all paths in the JSON data * @returns Array of knowledge chunks * @private */ private processAllPaths; /** * Extract all paths from JSON data * @param data - JSON data * @param basePath - Base path for recursion * @param depth - Current depth * @returns Array of paths * @private */ private extractAllPaths; /** * Get value at specified JSON path * @param data - JSON data * @param path - Path to retrieve * @returns Value at path * @private */ private getValueAtPath; /** * Parse a JSON path into path parts * @param path - Path string * @returns Array of path parts * @private */ private parsePath; /** * Format value based on schema configuration * @param value - Value to format * @param schema - Schema configuration * @returns Formatted content string * @private */ private formatValue; /** * Evaluate a condition on the JSON data * @param condition - Condition to evaluate * @returns True if condition is met * @private */ private evaluateCondition; /** * Format a template string with path placeholders * @param template - Template string * @returns Formatted string * @private */ private formatTemplate; /** * Convert a value to string representation * @param value - Value to stringify * @returns String representation * @private */ private stringifyValue; } //# sourceMappingURL=JSONKnowledgeSource.d.ts.map