// src/work/boundary.ts
var WORK_LAYER_CONTEXT_OPEN = "[WORK_LAYER_CONTEXT";
var WORK_LAYER_CONTEXT_CLOSE = "[/WORK_LAYER_CONTEXT]";
var WORK_LAYER_CONTEXT_ESCAPED_OPEN = "[WORK_LAYER_ESCAPED_CONTEXT";
var WORK_LAYER_CONTEXT_ESCAPED_CLOSE = "[/WORK_LAYER_CONTEXT_ESC]";
function wrapWorkLayerContext(content, options) {
  const linkToMemory = options?.linkToMemory === true;
  const header = `${WORK_LAYER_CONTEXT_OPEN} link_to_memory=${linkToMemory ? "true" : "false"}]`;
  const payload = content.trim().replaceAll(WORK_LAYER_CONTEXT_OPEN, WORK_LAYER_CONTEXT_ESCAPED_OPEN).replaceAll(WORK_LAYER_CONTEXT_CLOSE, WORK_LAYER_CONTEXT_ESCAPED_CLOSE);
  return `${header}
${payload}
${WORK_LAYER_CONTEXT_CLOSE}`;
}
function applyWorkExtractionBoundary(conversation) {
  if (conversation.trim().length === 0) return "";
  let bounded = conversation;
  const OPEN_TAG = "[WORK_LAYER_CONTEXT";
  const CLOSE_TAG = "[/WORK_LAYER_CONTEXT]";
  let searchFrom = 0;
  while (true) {
    const openIdx = bounded.indexOf(OPEN_TAG, searchFrom);
    if (openIdx < 0) break;
    if (openIdx > 0 && bounded[openIdx - 1] !== "\n") {
      searchFrom = openIdx + OPEN_TAG.length;
      continue;
    }
    const closeBracket = bounded.indexOf("]", openIdx + OPEN_TAG.length);
    if (closeBracket < 0) break;
    const header = bounded.substring(openIdx + OPEN_TAG.length, closeBracket);
    const closeIdx = bounded.indexOf(CLOSE_TAG, closeBracket + 1);
    if (closeIdx < 0) break;
    const flagMatch = header.match(/\blink_to_memory=(true|false)/);
    const encMatch = header.match(/\bencoding=(base64)/);
    const flag = flagMatch?.[1];
    const encoding = encMatch?.[1];
    let bodyStart = closeBracket + 1;
    if (bounded[bodyStart] === "\n") bodyStart++;
    let bodyEnd = closeIdx;
    if (bodyEnd > bodyStart && bounded[bodyEnd - 1] === "\n") bodyEnd--;
    const body = bounded.substring(bodyStart, bodyEnd);
    const shouldLink = flag === "true";
    const prefix = openIdx > 0 ? bounded.substring(0, openIdx) : "";
    const suffix = bounded.substring(closeIdx + CLOSE_TAG.length);
    if (!shouldLink) {
      bounded = prefix + suffix;
      searchFrom = prefix.length;
    } else if (encoding === "base64") {
      try {
        const decoded = Buffer.from(body.trim(), "base64").toString("utf8").trim();
        bounded = prefix + decoded + suffix;
        searchFrom = prefix.length + decoded.length;
      } catch {
        bounded = prefix + suffix;
        searchFrom = prefix.length;
      }
    } else {
      bounded = prefix + body.trim() + suffix;
      searchFrom = prefix.length + body.trim().length;
    }
  }
  let strippedUnterminated = bounded;
  const opener = "[WORK_LAYER_CONTEXT";
  const closer = "[/WORK_LAYER_CONTEXT]";
  const lastOpenerIdx = bounded.lastIndexOf(opener);
  if (lastOpenerIdx >= 0) {
    const isLineStart = lastOpenerIdx === 0 || bounded[lastOpenerIdx - 1] === "\n";
    if (isLineStart) {
      const afterOpener = bounded.indexOf("]", lastOpenerIdx);
      if (afterOpener >= 0) {
        const closerAfter = bounded.indexOf(closer, afterOpener);
        if (closerAfter < 0) {
          const bracketContent = bounded.substring(lastOpenerIdx, afterOpener + 1);
          if (bracketContent.includes("link_to_memory=") || bracketContent.includes("encoding=")) {
            strippedUnterminated = lastOpenerIdx > 0 ? bounded.substring(0, lastOpenerIdx - 1) : "";
          }
        }
      }
    }
  }
  const restoredEscapes = strippedUnterminated.replaceAll(WORK_LAYER_CONTEXT_ESCAPED_OPEN, WORK_LAYER_CONTEXT_OPEN).replaceAll(WORK_LAYER_CONTEXT_ESCAPED_CLOSE, WORK_LAYER_CONTEXT_CLOSE);
  const cleanedLines = restoredEscapes.split("\n").map((line) => line.trimEnd());
  return cleanedLines.join("\n").replace(/\n{3,}/g, "\n\n").trim();
}

export {
  wrapWorkLayerContext,
  applyWorkExtractionBoundary
};
//# sourceMappingURL=chunk-EEQLFRUM.js.map