UNPKG

crewai-ts

Version:

TypeScript port of crewAI for agent-based workflows

96 lines 3.53 kB
/** * Advanced Agent Parser * Parses ReAct-style LLM outputs with memory-efficient processing and robust error handling */ import { AgentAction, AgentFinish, AgentParserOptions } from './types.js'; import { BaseAgent } from '../BaseAgent.js'; /** * CrewAgentParser class * Parses agent outputs in ReAct format with memory efficiency and advanced error handling * * Optimizations include: * - Memory-efficient regex patterns to minimize allocation * - Single-pass parsing for most common patterns * - Smart JSON parsing with syntax repair capabilities * - Incremental text processing to handle large outputs */ export declare class CrewAgentParser { private static ACTION_PATTERN; private static THOUGHT_PATTERN; private static MARKDOWN_CODE_BLOCK; private agent?; private options; /** * Create a new CrewAgentParser instance * * @param agent Optional agent instance for error tracking and context * @param options Parsing options for customization */ constructor(agent?: BaseAgent, options?: AgentParserOptions); /** * Static method to parse text into an AgentAction or AgentFinish * Allows usage without instantiating the class * * @param text Text to parse * @returns Either an AgentAction or AgentFinish based on parsed content * @throws OutputParserException if parsing fails */ static parseText(text: string): AgentAction | AgentFinish; /** * Parse agent output text into structured formats * Handles both action requests and final answers with robust error handling * * @param text Raw text to parse * @returns Either an AgentAction or AgentFinish based on parsed content * @throws OutputParserException if parsing fails */ parse(text: string): AgentAction | AgentFinish; /** * Extract thought content from text * * @param text Text to extract thought from * @returns Extracted thought or empty string if not found */ private extractThought; /** * Clean action string by removing non-essential formatting characters * Memory-efficient implementation that minimizes string operations * * @param text Action text to clean * @returns Cleaned action string */ private cleanAction; /** * Safely repair malformed JSON without throwing exceptions * Uses multiple repair strategies with memory efficiency in mind * * @param input Potentially malformed JSON string * @returns Repaired JSON string or original string if repair fails/unnecessary */ private safeRepairJson; /** * Standardize quotes in a JSON string (convert single quotes to double quotes) * Handles nested quotes and escaped characters * * @param input JSON string with potentially mixed quotes * @returns String with standardized quotes */ private standardizeQuotes; /** * Repair unbalanced brackets and quotes in JSON * Handles various malformed JSON patterns with minimal memory usage * * @param input Potentially malformed JSON with unbalanced elements * @returns Repaired JSON string */ private repairUnbalancedElements; /** * Remove markdown formatting from text * Optimized for common patterns in LLM outputs * * @param text Text with potential markdown formatting * @returns Cleaned text with markdown removed */ private removeMarkdownFormatting; } //# sourceMappingURL=AgentParser.d.ts.map