UNPKG

@tehreet/conduit

Version:

LLM API gateway with intelligent routing, robust process management, and health monitoring

115 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetadataHandler = void 0; const stream_1 = require("stream"); const log_1 = require("../utils/log"); class MetadataHandler { /** * Creates a transform stream that injects metadata as the first line */ static createMetadataInjector(metadata) { let metadataInjected = false; return new stream_1.Transform({ transform(chunk, encoding, callback) { if (!metadataInjected) { // Inject metadata as first line const metadataLine = JSON.stringify({ type: 'conduit_metadata', data: metadata, }) + '\n'; this.push(Buffer.from(metadataLine)); metadataInjected = true; } // Pass through the original chunk this.push(chunk); callback(); }, }); } /** * Extracts metadata from NDJSON stream */ static extractFromStream(data) { try { const lines = data.split('\n').filter(line => line.trim()); for (const line of lines) { try { const parsed = JSON.parse(line); if (parsed.type === 'conduit_metadata') { return parsed.data; } } catch (error) { // Continue to next line if JSON parsing fails } } } catch (error) { (0, log_1.log)('Error extracting metadata from stream:', error); } return null; } /** * Creates a stream processor that handles both metadata and content */ static createStreamProcessor(onMetadata) { let buffer = ''; return new stream_1.Transform({ transform(chunk, encoding, callback) { buffer += chunk.toString(); const lines = buffer.split('\n'); buffer = lines.pop() || ''; // Keep incomplete line in buffer for (const line of lines) { if (!line.trim()) continue; try { const parsed = JSON.parse(line); if (parsed.type === 'conduit_metadata') { // Handle metadata onMetadata(parsed.data); } else { // Pass through regular content this.push(line + '\n'); } } catch (error) { // If not valid JSON, pass through as-is this.push(line + '\n'); } } callback(); }, flush(callback) { // Handle any remaining data in buffer if (buffer.trim()) { this.push(buffer); } callback(); }, }); } /** * Injects metadata into an existing stream */ static injectIntoStream(stream, metadata) { const injector = this.createMetadataInjector(metadata); return stream.pipe(injector); } /** * Creates a metadata object for injection */ static createMetadata(routingDecision, additionalData = {}) { return { type: 'conduit_metadata', data: { routingDecision, timestamp: new Date().toISOString(), conduitVersion: '2.0.0', ...additionalData, }, }; } } exports.MetadataHandler = MetadataHandler; //# sourceMappingURL=metadata-handler.js.map