File Parsing & Handling
Relevant source files
The following files were used as context for generating this wiki page:
- packages/core/files/parsers/parsers.test.ts
- packages/web/src/lib/agent/agent-helpers.ts
- packages/core/api/systems-documentation.ts
- packages/web/src/components/systems/sections/ContextSection.tsx
- packages/core/api/response-helpers.ts
- packages/web/src/lib/agent/agent-prompts.ts
- packages/core/documentation/documentation-utils.ts
The superglue platform implements a robust file handling system for processing various data formats, managing document uploads for knowledge bases, and resolving file references during agent-led tool execution. This system enables the conversion of raw binary data into structured formats like JSON, CSV, and XML, supporting both manual uploads and automated scraping.
Core Parsing Architecture
The project utilizes a strategy-based registry to detect and parse files. The FileStrategyRegistry manages multiple specialized strategies that identify file types via magic numbers (signatures) or structural analysis.
Detection and Strategy Priority
The system prioritizes decompression and high-structure formats to ensure accurate identification. For instance, GZIP is tested before other binary formats, and specialized ZIP structures like Excel (.xlsx) or DOCX are identified before falling back to a generic ZIP parser.
Supported Formats and Strategies
The following table summarizes the primary parsing strategies and their detection mechanisms:
| Strategy | File Type | Detection Mechanism | Output Format |
|---|---|---|---|
| JSONStrategy | JSON | Brackets/Braces + JSON.parse | Object/Array |
| CSVStrategy | CSV | Delimiter detection (comma, semi-colon, pipe, tab) | Array of Objects |
| XMLStrategy | XML | Tag patterns (e.g., <?xml) | Object (Uppercase keys) |
| YAMLStrategy | YAML | Document markers (---) or Key-Value patterns | Object |
| ZIPStrategy | ZIP | ZIP signature; filters macOS metadata | Record of Files |
| PDFStrategy | PDF | %PDF signature | Text Content |
| HTMLStrategy | HTML | <!DOCTYPE html> or <html> tags | Structured Object |
Sources: packages/core/files/parsers/parsers.test.ts:10-250, packages/core/files/parsers/parsers.test.ts:320-335, packages/core/documentation/documentation-utils.ts:180-205
Resilient JSON Parsing
Superglue includes a ResilientJsonParser to handle malformed JSON frequently encountered in LLM outputs or legacy system exports. It applies a series of repair strategies sequentially to fix common syntax errors.
Repair Strategies
- Triple Quote Repair: Converts triple-quoted strings (often containing nested JSON) into proper JSON objects.
- Single Quote Repair: Converts single quotes to double quotes while preserving apostrophes in double-quoted strings.
- Control Character Repair: Escapes unescaped newlines and tabs within JSON strings.
- Trailing Comma Repair: Removes invalid trailing commas in objects and arrays.
- Aggressive Fallback: Extracts the first valid JSON block from a string containing prefix/suffix garbage text.
Sources: packages/core/files/parsers/parsers.test.ts:494-680, packages/core/files/parsers/parsers.test.ts:745-775
Agent File Handling & References
The AI agent interacts with files using a specific referencing syntax (file::key). Before execution, the system resolves these references into actual data envelopes.
Reference Resolution Flow
When the agent provides a payload containing file:: strings, resolveFileReferences processes them based on their suffix:
file::filename: Resolves to the extracted/parsed content (string or object)..raw: Provides raw binary data in base64 format with metadata (filename, content type)..base64: Returns the raw base64 string directly (limited to 500 MB)..extracted: Explicitly requests the parsed text/object content.
Documentation Knowledge Base Management
The system manages files within a "Knowledge Base" for specific systems. Files are added via manual uploads or automated URL scraping.
Upload and Storage
Manual uploads involve generating a signed URL via generateUploadUrl and creating a FileReference in the datastore. The UI filters blocked extensions like .zip and .gz for documentation context to ensure content is readable by the agent.
Scraping and OpenAPI Detection
When a URL is added to a system's context, the systems-documentation module triggers a scrape. It automatically attempts to detect OpenAPI specifications by looking for Swagger settings or JSON/YAML patterns in the scraped HTML.
| Parameter | Type | Description |
|---|---|---|
fileName | String | Original name of the uploaded file. |
contentLength | Number | Size in bytes for UI formatting. |
source | Enum | upload, scrape, or openapi. |
status | Enum | PENDING, PROCESSING, COMPLETED, FAILED. |
Sources: packages/core/api/systems-documentation.ts:15-100, packages/web/src/components/systems/sections/ContextSection.tsx:15-50, packages/core/api/response-helpers.ts:130-155
Conclusion
The file handling system provides a seamless bridge between raw external data and the structured requirements of an AI integration platform. By combining automated format detection, resilient parsing for malformed data, and a flexible referencing system for agents, it ensures that diverse file types can be utilized effectively for tool building and knowledge retrieval.