'use strict'; var crypto = require('crypto'); // ../memory/dist/chunk-WCGXQIEN.js // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/diff/8.0.3/c2a1d77e3f595587fc71d833e2045f3b897998b15167d647e8b9eeaae17f7bb2/node_modules/diff/libesm/diff/base.js var Diff = class { diff(oldStr, newStr, options = {}) { let callback; if (typeof options === "function") { callback = options; options = {}; } else if ("callback" in options) { callback = options.callback; } const oldString = this.castInput(oldStr, options); const newString = this.castInput(newStr, options); const oldTokens = this.removeEmpty(this.tokenize(oldString, options)); const newTokens = this.removeEmpty(this.tokenize(newString, options)); return this.diffWithOptionsObj(oldTokens, newTokens, options, callback); } diffWithOptionsObj(oldTokens, newTokens, options, callback) { var _a; const done = (value) => { value = this.postProcess(value, options); if (callback) { setTimeout(function() { callback(value); }, 0); return void 0; } else { return value; } }; const newLen = newTokens.length, oldLen = oldTokens.length; let editLength = 1; let maxEditLength = newLen + oldLen; if (options.maxEditLength != null) { maxEditLength = Math.min(maxEditLength, options.maxEditLength); } const maxExecutionTime = (_a = options.timeout) !== null && _a !== void 0 ? _a : Infinity; const abortAfterTimestamp = Date.now() + maxExecutionTime; const bestPath = [{ oldPos: -1, lastComponent: void 0 }]; let newPos = this.extractCommon(bestPath[0], newTokens, oldTokens, 0, options); if (bestPath[0].oldPos + 1 >= oldLen && newPos + 1 >= newLen) { return done(this.buildValues(bestPath[0].lastComponent, newTokens, oldTokens)); } let minDiagonalToConsider = -Infinity, maxDiagonalToConsider = Infinity; const execEditLength = () => { for (let diagonalPath = Math.max(minDiagonalToConsider, -editLength); diagonalPath <= Math.min(maxDiagonalToConsider, editLength); diagonalPath += 2) { let basePath; const removePath = bestPath[diagonalPath - 1], addPath = bestPath[diagonalPath + 1]; if (removePath) { bestPath[diagonalPath - 1] = void 0; } let canAdd = false; if (addPath) { const addPathNewPos = addPath.oldPos - diagonalPath; canAdd = addPath && 0 <= addPathNewPos && addPathNewPos < newLen; } const canRemove = removePath && removePath.oldPos + 1 < oldLen; if (!canAdd && !canRemove) { bestPath[diagonalPath] = void 0; continue; } if (!canRemove || canAdd && removePath.oldPos < addPath.oldPos) { basePath = this.addToPath(addPath, true, false, 0, options); } else { basePath = this.addToPath(removePath, false, true, 1, options); } newPos = this.extractCommon(basePath, newTokens, oldTokens, diagonalPath, options); if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) { return done(this.buildValues(basePath.lastComponent, newTokens, oldTokens)) || true; } else { bestPath[diagonalPath] = basePath; if (basePath.oldPos + 1 >= oldLen) { maxDiagonalToConsider = Math.min(maxDiagonalToConsider, diagonalPath - 1); } if (newPos + 1 >= newLen) { minDiagonalToConsider = Math.max(minDiagonalToConsider, diagonalPath + 1); } } } editLength++; }; if (callback) { (function exec() { setTimeout(function() { if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) { return callback(void 0); } if (!execEditLength()) { exec(); } }, 0); })(); } else { while (editLength <= maxEditLength && Date.now() <= abortAfterTimestamp) { const ret = execEditLength(); if (ret) { return ret; } } } } addToPath(path, added, removed, oldPosInc, options) { const last = path.lastComponent; if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) { return { oldPos: path.oldPos + oldPosInc, lastComponent: { count: last.count + 1, added, removed, previousComponent: last.previousComponent } }; } else { return { oldPos: path.oldPos + oldPosInc, lastComponent: { count: 1, added, removed, previousComponent: last } }; } } extractCommon(basePath, newTokens, oldTokens, diagonalPath, options) { const newLen = newTokens.length, oldLen = oldTokens.length; let oldPos = basePath.oldPos, newPos = oldPos - diagonalPath, commonCount = 0; while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(oldTokens[oldPos + 1], newTokens[newPos + 1], options)) { newPos++; oldPos++; commonCount++; if (options.oneChangePerToken) { basePath.lastComponent = { count: 1, previousComponent: basePath.lastComponent, added: false, removed: false }; } } if (commonCount && !options.oneChangePerToken) { basePath.lastComponent = { count: commonCount, previousComponent: basePath.lastComponent, added: false, removed: false }; } basePath.oldPos = oldPos; return newPos; } equals(left, right, options) { if (options.comparator) { return options.comparator(left, right); } else { return left === right || !!options.ignoreCase && left.toLowerCase() === right.toLowerCase(); } } removeEmpty(array) { const ret = []; for (let i = 0; i < array.length; i++) { if (array[i]) { ret.push(array[i]); } } return ret; } // eslint-disable-next-line @typescript-eslint/no-unused-vars castInput(value, options) { return value; } // eslint-disable-next-line @typescript-eslint/no-unused-vars tokenize(value, options) { return Array.from(value); } join(chars) { return chars.join(""); } postProcess(changeObjects, options) { return changeObjects; } get useLongestToken() { return false; } buildValues(lastComponent, newTokens, oldTokens) { const components = []; let nextComponent; while (lastComponent) { components.push(lastComponent); nextComponent = lastComponent.previousComponent; delete lastComponent.previousComponent; lastComponent = nextComponent; } components.reverse(); const componentLen = components.length; let componentPos = 0, newPos = 0, oldPos = 0; for (; componentPos < componentLen; componentPos++) { const component = components[componentPos]; if (!component.removed) { if (!component.added && this.useLongestToken) { let value = newTokens.slice(newPos, newPos + component.count); value = value.map(function(value2, i) { const oldValue = oldTokens[oldPos + i]; return oldValue.length > value2.length ? oldValue : value2; }); component.value = this.join(value); } else { component.value = this.join(newTokens.slice(newPos, newPos + component.count)); } newPos += component.count; if (!component.added) { oldPos += component.count; } } else { component.value = this.join(oldTokens.slice(oldPos, oldPos + component.count)); oldPos += component.count; } } return components; } }; // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/diff/8.0.3/c2a1d77e3f595587fc71d833e2045f3b897998b15167d647e8b9eeaae17f7bb2/node_modules/diff/libesm/diff/line.js var LineDiff = class extends Diff { constructor() { super(...arguments); this.tokenize = tokenize; } equals(left, right, options) { if (options.ignoreWhitespace) { if (!options.newlineIsToken || !left.includes("\n")) { left = left.trim(); } if (!options.newlineIsToken || !right.includes("\n")) { right = right.trim(); } } else if (options.ignoreNewlineAtEof && !options.newlineIsToken) { if (left.endsWith("\n")) { left = left.slice(0, -1); } if (right.endsWith("\n")) { right = right.slice(0, -1); } } return super.equals(left, right, options); } }; var lineDiff = new LineDiff(); function diffLines(oldStr, newStr, options) { return lineDiff.diff(oldStr, newStr, options); } function tokenize(value, options) { if (options.stripTrailingCr) { value = value.replace(/\r\n/g, "\n"); } const retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/); if (!linesAndNewlines[linesAndNewlines.length - 1]) { linesAndNewlines.pop(); } for (let i = 0; i < linesAndNewlines.length; i++) { const line = linesAndNewlines[i]; if (i % 2 && !options.newlineIsToken) { retLines[retLines.length - 1] += line; } else { retLines.push(line); } } return retLines; } // ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/diff/8.0.3/c2a1d77e3f595587fc71d833e2045f3b897998b15167d647e8b9eeaae17f7bb2/node_modules/diff/libesm/patch/create.js function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) { let optionsObj; if (!options) { optionsObj = {}; } else if (typeof options === "function") { optionsObj = { callback: options }; } else { optionsObj = options; } if (typeof optionsObj.context === "undefined") { optionsObj.context = 4; } const context = optionsObj.context; if (optionsObj.newlineIsToken) { throw new Error("newlineIsToken may not be used with patch-generation functions, only with diffing functions"); } if (!optionsObj.callback) { return diffLinesResultToPatch(diffLines(oldStr, newStr, optionsObj)); } else { const { callback } = optionsObj; diffLines(oldStr, newStr, Object.assign(Object.assign({}, optionsObj), { callback: (diff) => { const patch = diffLinesResultToPatch(diff); callback(patch); } })); } function diffLinesResultToPatch(diff) { if (!diff) { return; } diff.push({ value: "", lines: [] }); function contextLines(lines) { return lines.map(function(entry) { return " " + entry; }); } const hunks = []; let oldRangeStart = 0, newRangeStart = 0, curRange = [], oldLine = 1, newLine = 1; for (let i = 0; i < diff.length; i++) { const current = diff[i], lines = current.lines || splitLines(current.value); current.lines = lines; if (current.added || current.removed) { if (!oldRangeStart) { const prev = diff[i - 1]; oldRangeStart = oldLine; newRangeStart = newLine; if (prev) { curRange = context > 0 ? contextLines(prev.lines.slice(-context)) : []; oldRangeStart -= curRange.length; newRangeStart -= curRange.length; } } for (const line of lines) { curRange.push((current.added ? "+" : "-") + line); } if (current.added) { newLine += lines.length; } else { oldLine += lines.length; } } else { if (oldRangeStart) { if (lines.length <= context * 2 && i < diff.length - 2) { for (const line of contextLines(lines)) { curRange.push(line); } } else { const contextSize = Math.min(lines.length, context); for (const line of contextLines(lines.slice(0, contextSize))) { curRange.push(line); } const hunk = { oldStart: oldRangeStart, oldLines: oldLine - oldRangeStart + contextSize, newStart: newRangeStart, newLines: newLine - newRangeStart + contextSize, lines: curRange }; hunks.push(hunk); oldRangeStart = 0; newRangeStart = 0; curRange = []; } } oldLine += lines.length; newLine += lines.length; } } for (const hunk of hunks) { for (let i = 0; i < hunk.lines.length; i++) { if (hunk.lines[i].endsWith("\n")) { hunk.lines[i] = hunk.lines[i].slice(0, -1); } else { hunk.lines.splice(i + 1, 0, "\\ No newline at end of file"); i++; } } } return { oldFileName, newFileName, oldHeader, newHeader, hunks }; } } function splitLines(text) { const hasTrailingNl = text.endsWith("\n"); const result = text.split("\n").map((line) => line + "\n"); if (hasTrailingNl) { result.pop(); } else { result.push(result.pop().slice(0, -1)); } return result; } // ../memory/dist/chunk-WCGXQIEN.js var WORKING_MEMORY_STATE_ID = "working-memory"; var WORKING_MEMORY_STATE_PROCESSOR_ID = "working-memory-state"; var WorkingMemoryStateProcessor = class { constructor(memory, memoryConfig) { this.memory = memory; this.memoryConfig = memoryConfig; } memory; memoryConfig; id = WORKING_MEMORY_STATE_PROCESSOR_ID; stateId = WORKING_MEMORY_STATE_ID; async computeStateSignal(args) { const template = await this.memory.getWorkingMemoryTemplate({ memoryConfig: this.memoryConfig }); if (!template) return; const data = await this.memory.getWorkingMemory({ threadId: args.threadId, resourceId: args.resourceId, memoryConfig: this.memoryConfig }); const contents = data?.trim(); if (!contents) return; const cacheKey = stableWorkingMemoryCacheKey({ format: template.format, data: contents }); const shouldMakeSnapshot = !args.contextWindow.hasSnapshot; if (args.tracking?.currentCacheKey === cacheKey && !shouldMakeSnapshot) return; const mergedConfig = this.memory.getMergedThreadConfig(this.memoryConfig); const scope = mergedConfig.workingMemory?.scope ?? "resource"; const deltaCandidate = template.format === "markdown" && !shouldMakeSnapshot ? buildMarkdownDelta({ lastSnapshot: args.lastSnapshot, deltasSinceSnapshot: args.deltasSinceSnapshot, nextContents: contents }) : void 0; if (deltaCandidate) { return { id: WORKING_MEMORY_STATE_ID, mode: "delta", cacheKey, tagName: "working-memory", contents: deltaCandidate.contents, delta: deltaCandidate.contents, // Stash the full post-edit text on the signal so the next turn can // diff against the most recently emitted state instead of the older // snapshot. Invisible to the model. value: contents, attributes: { format: template.format, scope, patch: "unified-diff" } }; } return { id: WORKING_MEMORY_STATE_ID, mode: "snapshot", cacheKey, tagName: "working-memory", contents, // Mirror contents in value so the first delta after a snapshot has a // typed prior-state to diff against without falling back to contents. value: contents, attributes: { format: template.format, scope } }; } }; function stableWorkingMemoryCacheKey(input) { const hash = crypto.createHash("sha256"); hash.update(input.format); hash.update("\0"); hash.update(input.data ?? ""); return `sha256:${hash.digest("hex")}`; } function buildMarkdownDelta(args) { const { lastSnapshot, deltasSinceSnapshot, nextContents } = args; const latestDelta = deltasSinceSnapshot.at(-1); const prior = pickStringValue(readSignalValue(latestDelta)) ?? pickStringValue(readSignalValue(lastSnapshot)) ?? (typeof lastSnapshot?.contents === "string" ? lastSnapshot.contents : void 0); if (!prior) return; const patch = renderHunksOnly(prior, nextContents); return { contents: patch }; } function pickStringValue(value) { return typeof value === "string" ? value : void 0; } function readSignalValue(signal) { return signal?.metadata?.value; } function renderHunksOnly(prior, next) { const { hunks } = structuredPatch("", "", prior, next, "", "", { context: 0 }); return hunks.map((hunk) => { const header = `@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`; const lines = hunk.lines.filter((line) => !line.startsWith("\\ No newline at end of file")); return [header, ...lines].join("\n"); }).join("\n"); } exports.WORKING_MEMORY_STATE_ID = WORKING_MEMORY_STATE_ID; exports.WORKING_MEMORY_STATE_PROCESSOR_ID = WORKING_MEMORY_STATE_PROCESSOR_ID; exports.WorkingMemoryStateProcessor = WorkingMemoryStateProcessor; exports.stableWorkingMemoryCacheKey = stableWorkingMemoryCacheKey; //# sourceMappingURL=chunk-2ONW75MN.cjs.map //# sourceMappingURL=chunk-2ONW75MN.cjs.map