//#region src/agent/tools/structured-output.ts
var Element = class Element {
	tag;
	attributes = /* @__PURE__ */ new Map();
	children = [];
	textContent;
	isCData = false;
	constructor(tag) {
		this.tag = tag;
	}
	/**
	* Create a new element
	*/
	static create(tag) {
		return new Element(tag);
	}
	/**
	* Create a file content element
	*/
	static fileContent(path, content, options) {
		const el = Element.create("file_content").attr("path", path);
		if (options?.startLine !== void 0) el.attr("start_line", options.startLine);
		if (options?.endLine !== void 0) el.attr("end_line", options.endLine);
		if (options?.totalLines !== void 0) el.attr("total_lines", options.totalLines);
		if (options?.language) el.attr("language", options.language);
		return el.cdata(content);
	}
	/**
	* Create a directory listing element
	*/
	static directoryListing(path, entries) {
		const el = Element.create("directory_listing").attr("path", path);
		for (const entry of entries) {
			const entryEl = Element.create("entry").attr("name", entry.name).attr("type", entry.type);
			if (entry.size !== void 0) entryEl.attr("size", entry.size);
			if (entry.modified) entryEl.attr("modified", entry.modified.toISOString());
			el.child(entryEl);
		}
		return el;
	}
	/**
	* Create a search results element
	*/
	static searchResults(pattern, results, options) {
		const el = Element.create("search_results").attr("pattern", pattern);
		if (options?.totalResults !== void 0) el.attr("total_results", options.totalResults);
		if (options?.truncated) el.attr("truncated", "true");
		for (const result of results) {
			const resultEl = Element.create("result").attr("path", result.path).attr("line", result.line);
			if (result.column !== void 0) resultEl.attr("column", result.column);
			resultEl.child(Element.create("match").text(result.match));
			if (result.context) resultEl.child(Element.create("context").cdata(result.context));
			el.child(resultEl);
		}
		return el;
	}
	/**
	* Create a command output element
	*/
	static commandOutput(command, output, options) {
		const el = Element.create("command_output").attr("command", command);
		if (options?.exitCode !== void 0) el.attr("exit_code", options.exitCode);
		if (options?.executionTimeMs !== void 0) el.attr("execution_time_ms", options.executionTimeMs);
		if (options?.workingDirectory) el.attr("working_directory", options.workingDirectory);
		return el.cdata(output);
	}
	/**
	* Create an error element
	*/
	static error(message, options) {
		const el = Element.create("error").attr("message", message);
		if (options?.type) el.attr("type", options.type);
		if (options?.code) el.attr("code", options.code);
		if (options?.recoverable !== void 0) el.attr("recoverable", options.recoverable);
		if (options?.suggestion) el.child(Element.create("suggestion").text(options.suggestion));
		return el;
	}
	/**
	* Create a success element
	*/
	static success(message, details) {
		const el = Element.create("success").attr("message", message);
		if (details) for (const [key, value] of Object.entries(details)) el.child(Element.create(key).text(value));
		return el;
	}
	/**
	* Escape XML special characters for attribute values
	*/
	escapeXml(text) {
		return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
	}
	/**
	* Add an attribute
	*/
	attr(key, value) {
		if (value !== void 0 && value !== null) this.attributes.set(key, this.escapeXml(String(value)));
		return this;
	}
	/**
	* Add multiple attributes
	*/
	attrs(attributes) {
		for (const [key, value] of Object.entries(attributes)) this.attr(key, value);
		return this;
	}
	/**
	* Set text content (XML escaped)
	*/
	text(content) {
		this.textContent = this.escapeXml(content);
		this.isCData = false;
		return this;
	}
	/**
	* Set CDATA content (not XML escaped)
	*/
	cdata(content) {
		this.textContent = content;
		this.isCData = true;
		return this;
	}
	/**
	* Add a child element
	*/
	child(child) {
		this.children.push(child);
		return this;
	}
	/**
	* Add multiple child elements
	*/
	addChildren(children) {
		for (const child of children) this.children.push(child);
		return this;
	}
	/**
	* Render the element as XML string
	*/
	render(indent = "") {
		const attrs = [...this.attributes.entries()].map(([k, v]) => ` ${k}="${this.escapeXml(v)}"`).join("");
		if (!this.textContent && this.children.length === 0) return `${indent}<${this.tag}${attrs}/>`;
		const childrenStr = this.children.map((c) => c.render(indent + "  ")).join("\n");
		let content;
		if (this.isCData) content = `<![CDATA[${this.textContent}]]>`;
		else if (this.textContent) content = this.textContent;
		else content = "";
		if (childrenStr) if (content) return `${indent}<${this.tag}${attrs}>\n${indent}  ${content}\n${childrenStr}\n${indent}</${this.tag}>`;
		else return `${indent}<${this.tag}${attrs}>\n${childrenStr}\n${indent}</${this.tag}>`;
		else return `${indent}<${this.tag}${attrs}>${content}</${this.tag}>`;
	}
	/**
	* Render as compact XML (no indentation)
	*/
	renderCompact() {
		const attrs = [...this.attributes.entries()].map(([k, v]) => ` ${k}="${this.escapeXml(v)}"`).join("");
		if (!this.textContent && this.children.length === 0) return `<${this.tag}${attrs}/>`;
		const childrenStr = this.children.map((c) => c.renderCompact()).join("");
		let content;
		if (this.isCData) content = `<![CDATA[${this.textContent}]]>`;
		else if (this.textContent) content = this.textContent;
		else content = "";
		return `<${this.tag}${attrs}>${content}${childrenStr}</${this.tag}>`;
	}
	/**
	* Get the tag name
	*/
	getTag() {
		return this.tag;
	}
	/**
	* Get an attribute value
	*/
	getAttr(key) {
		return this.attributes.get(key);
	}
	/**
	* Get all attributes
	*/
	getAttrs() {
		return Object.fromEntries(this.attributes);
	}
	/**
	* Get text content
	*/
	getText() {
		return this.textContent;
	}
	/**
	* Get children
	*/
	getChildren() {
		return [...this.children];
	}
	/**
	* Check if element has children
	*/
	hasChildren() {
		return this.children.length > 0;
	}
	/**
	* Check if element has text content
	*/
	hasText() {
		return this.textContent !== void 0 && this.textContent.length > 0;
	}
	/**
	* Clone the element
	*/
	clone() {
		const cloned = new Element(this.tag);
		cloned.attributes = new Map(this.attributes);
		cloned.textContent = this.textContent;
		cloned.isCData = this.isCData;
		cloned.children = this.children.map((c) => c.clone());
		return cloned;
	}
	/**
	* Convert to JSON representation
	*/
	toJSON() {
		return {
			tag: this.tag,
			attributes: Object.fromEntries(this.attributes),
			text: this.textContent,
			cdata: this.isCData,
			children: this.children.map((c) => c.toJSON())
		};
	}
	/**
	* Create from JSON
	*/
	static fromJSON(json) {
		const el = new Element(json.tag);
		if (json.attributes) for (const [key, value] of Object.entries(json.attributes)) el.attr(key, value);
		if (json.text) if (json.cdata) el.cdata(json.text);
		else el.text(json.text);
		if (json.children) for (const childJson of json.children) el.child(Element.fromJSON(childJson));
		return el;
	}
};
var XMLParser = class XMLParser {
	/**
	* Parse XML string to Element
	* Note: This is a simple parser for the output format, not a full XML parser
	*/
	static parse(xml) {
		const tagMatch = xml.match(/<([\w_]+)([^>]*)>([\s\S]*?)<\/\1>/);
		if (!tagMatch) {
			const selfClosingMatch = xml.match(/<([\w_]+)([^>]*)\/>/);
			if (selfClosingMatch) {
				const [, tag, attrsStr] = selfClosingMatch;
				const el = Element.create(tag);
				XMLParser.parseAttributes(el, attrsStr);
				return el;
			}
			return null;
		}
		const [, tag, attrsStr, content] = tagMatch;
		const el = Element.create(tag);
		XMLParser.parseAttributes(el, attrsStr);
		const cdataMatch = content.match(/<!\[CDATA\[([\s\S]*?)\]\]>/);
		if (cdataMatch) el.cdata(cdataMatch[1]);
		else {
			const childRegex = /<([\w_]+)([^>]*)>([\s\S]*?)<\/\1>/g;
			let childMatch;
			let hasChildren = false;
			while ((childMatch = childRegex.exec(content)) !== null) {
				const child = XMLParser.parse(childMatch[0]);
				if (child) {
					el.child(child);
					hasChildren = true;
				}
			}
			if (!hasChildren) {
				const trimmed = content.trim();
				if (trimmed) el.text(trimmed);
			}
		}
		return el;
	}
	static parseAttributes(el, attrsStr) {
		const attrRegex = /(\w+)="([^"]*)"/g;
		let match;
		while ((match = attrRegex.exec(attrsStr)) !== null) el.attr(match[1], match[2]);
	}
};
const StructuredOutput = {
	Element,
	XMLParser,
	fileContent: Element.fileContent,
	directoryListing: Element.directoryListing,
	searchResults: Element.searchResults,
	commandOutput: Element.commandOutput,
	error: Element.error,
	success: Element.success,
	renderList(elements, separator = "\n\n") {
		return elements.map((e) => e.render()).join(separator);
	},
	wrapInRoot(rootTag, children) {
		const root = Element.create(rootTag);
		for (const child of children) root.child(child);
		return root;
	}
};
//#endregion
export { Element, StructuredOutput, StructuredOutput as default, XMLParser };

//# sourceMappingURL=structured-output.js.map