UNPKG

mlld

Version:

mlld: llm scripting language

264 lines (262 loc) 8.5 kB
import { isStructuredValue, wrapStructured } from './chunk-RKGZ44GZ.mjs'; import { isLoadContentResult } from './chunk-TZUIAYSF.mjs'; import { __name, __publicField } from './chunk-NJQT543K.mjs'; import { AsyncLocalStorage } from 'async_hooks'; var asyncLocalStorage = new AsyncLocalStorage(); var _a; var MetadataShelf = (_a = class { constructor() { __publicField(this, "shelf", /* @__PURE__ */ new Map()); __publicField(this, "singleFileMetadata", null); __publicField(this, "structuredShelf", /* @__PURE__ */ new WeakMap()); } /** * Store LoadContentResult objects on the shelf before unwrapping */ storeMetadata(value) { if (isStructuredValue(value) && value.type === "array") { const items = value.data; for (const item of items) { if (isLoadContentResult(item)) { this.getBucket(item.content).push(item); } } if (value.data && typeof value.data === "object") { this.structuredShelf.set(value.data, value); } } else if (isLoadContentResult(value)) { this.getBucket(value.content).push(value); this.singleFileMetadata = value; } else if (isStructuredValue(value)) { if (value.data && typeof value.data === "object") { this.structuredShelf.set(value.data, value); } } } /** * Attempt to restore metadata to returned values from JS functions */ restoreMetadata(value) { const structuredRestored = this.restoreStructuredFromShelf(value); if (structuredRestored) { return structuredRestored; } if (Array.isArray(value)) { const restored = []; let hasRestorable = false; for (const item of value) { if (typeof item === "string") { const restoredItem = this.shiftFromBucket(item); if (restoredItem) { restored.push(restoredItem); hasRestorable = true; continue; } } if (typeof item === "string" && this.singleFileMetadata && this.singleFileMetadata.content === item) { restored.push(this.singleFileMetadata); hasRestorable = true; continue; } restored.push(item); } return hasRestorable ? restored : value; } if (typeof value === "string" && this.singleFileMetadata) { const bucketMatch = this.shiftFromBucket(value); if (bucketMatch) { return bucketMatch; } if (this.singleFileMetadata.content === value) { return this.singleFileMetadata; } const original = this.singleFileMetadata; const restored = { ...original, content: value // Use the transformed content }; if (process.env.MLLD_DEBUG === "true") { console.error("[MetadataShelf] Auto-restoring single file metadata:", { originalContent: original.content.substring(0, 50) + "...", newContent: value.substring(0, 50) + "...", filename: original.filename }); } return restored; } return value; } restoreStructuredFromShelf(value) { const original = value && typeof value === "object" ? this.structuredShelf.get(value) : void 0; if (!original) { return null; } const text = this.computeStructuredText(original, value); const restored = wrapStructured(value, original.type, text, original.metadata); this.structuredShelf.set(value, restored); return restored; } computeStructuredText(original, data) { if (original.type === "text") { return typeof data === "string" ? data : String(data ?? ""); } if (original.type === "array") { if (Array.isArray(data)) { if (typeof data.toString === "function" && data.toString !== Array.prototype.toString) { return data.toString(); } try { return JSON.stringify(data); } catch { return data.map((item) => String(item)).join("\n"); } } return String(data ?? ""); } if (data && typeof data === "object" && "content" in data && typeof data.content === "string") { return data.content; } try { return JSON.stringify(data); } catch { return String(data ?? ""); } } /** * Clear the shelf to prevent memory leaks */ clear() { this.shelf.clear(); this.singleFileMetadata = null; this.structuredShelf = /* @__PURE__ */ new WeakMap(); } debugSnapshot() { return { loadContentEntries: this.shelf.size, hasSingleFileMetadata: Boolean(this.singleFileMetadata), structuredEntries: "weak" }; } getBucket(content) { let bucket = this.shelf.get(content); if (!bucket) { bucket = []; this.shelf.set(content, bucket); } return bucket; } shiftFromBucket(content) { const bucket = this.shelf.get(content); if (!bucket || bucket.length === 0) { return void 0; } const value = bucket.shift(); if (!bucket.length) { this.shelf.delete(content); } return value; } }, __name(_a, "MetadataShelf"), _a); var _AutoUnwrapManager = class _AutoUnwrapManager { /** * Auto-unwrap LoadContentResult objects to their content property * while preserving metadata on the thread-local shelf * * @param value - The value to potentially unwrap * @returns The unwrapped content or the original value */ static unwrap(value) { const shelf = asyncLocalStorage.getStore(); if (!shelf) { throw new Error("AutoUnwrapManager.unwrap called outside executeWithPreservation context"); } if (isStructuredValue(value)) { if (value.internal && value.internal.keepStructured) { return value; } shelf.storeMetadata(value); return value.data; } if (Array.isArray(value)) { const hasLoadContentResults = value.some((item) => isLoadContentResult(item)); if (hasLoadContentResults) { for (const item of value) { if (isLoadContentResult(item)) { shelf.storeMetadata(item); } } return value.map((item) => isLoadContentResult(item) ? item.content : item); } } const isLegacyLCR = isLoadContentResult(value); const isStructuredArray = isStructuredValue(value) && value.type === "array"; if (process.env.MLLD_DEBUG === "true" && (isLegacyLCR || isStructuredArray)) { console.error("[AutoUnwrapManager.unwrap] Unwrapping:", { type: isStructuredArray ? "StructuredValue[array]" : "LoadContentResult", shelfInContext: !!asyncLocalStorage.getStore() }); } shelf.storeMetadata(value); if (isLoadContentResult(value)) { return value.content; } if (isStructuredArray) { return value.data; } return value; } /** * Execute a function with metadata preservation * Sets up a new async context with its own metadata shelf * * @param fn - The function to execute * @returns The result with restored metadata if applicable */ static async executeWithPreservation(fn) { const shelf = new MetadataShelf(); try { const result = await asyncLocalStorage.run(shelf, fn); if (process.env.MLLD_DEBUG === "true") { console.error("[AutoUnwrapManager] Result before restoration:", result); console.error("[AutoUnwrapManager] Shelf snapshot:", shelf.debugSnapshot()); } const restored = shelf.restoreMetadata(result); if (process.env.MLLD_DEBUG === "true") { console.error("[AutoUnwrapManager] Result after restoration:", restored); } return restored; } finally { shelf.clear(); } } /** * Restore metadata from the current context's shelf * Used when we have a result that might contain unwrapped content * * @param value - The value to potentially restore * @returns The value with restored metadata if applicable */ static restore(value) { const shelf = asyncLocalStorage.getStore(); if (!shelf) { return value; } return shelf.restoreMetadata(value); } /** * Clear the current context's shelf * Should be called after restoration to prevent memory leaks */ static clear() { const shelf = asyncLocalStorage.getStore(); if (shelf) { shelf.clear(); } } }; __name(_AutoUnwrapManager, "AutoUnwrapManager"); var AutoUnwrapManager = _AutoUnwrapManager; export { AutoUnwrapManager }; //# sourceMappingURL=chunk-QTYWAF2L.mjs.map //# sourceMappingURL=chunk-QTYWAF2L.mjs.map