import { memoryGet, memorySearch } from "../prompt/memory/index.js";
import { recordDreamingRecalls } from "../memory/dreaming/short-term-store.js";
import { Type } from "@sinclair/typebox";
//#region src/agent/tools/memory-tool.ts
const MemorySearchSchema = Type.Object({
	query: Type.String(),
	maxResults: Type.Optional(Type.Number()),
	minScore: Type.Optional(Type.Number())
});
function createMemorySearchTool(options) {
	const { workspaceDir, memoriesDir } = options;
	return {
		name: "memory_search",
		label: "🔍 Memory Search",
		description: "Mandatory recall step: semantically search profile MEMORY.md, agent-home `memories/*.md`, and workspace `memory/*.md` before answering questions about prior work, decisions, dates, people, preferences, or todos; returns top snippets with path + lines.",
		parameters: MemorySearchSchema,
		async execute(_toolCallId, params, _signal) {
			const { query, maxResults, minScore } = params;
			try {
				const results = await memorySearch(workspaceDir, query, {
					maxResults,
					minScore,
					memoriesDir
				});
				recordDreamingRecalls({
					workspaceDir,
					query,
					matches: results
				}).catch(() => {});
				const withCitations = results.map((entry) => ({
					...entry,
					citation: `${entry.file}#L${entry.lineNumbers[0]}${entry.lineNumbers.length > 1 ? `-L${entry.lineNumbers[entry.lineNumbers.length - 1]}` : ""}`,
					snippet: `${entry.lines.trim()}\n\nSource: ${entry.file}#L${entry.lineNumbers[0]}${entry.lineNumbers.length > 1 ? `-L${entry.lineNumbers[entry.lineNumbers.length - 1]}` : ""}`
				}));
				return {
					content: [{
						type: "text",
						text: JSON.stringify({
							results: withCitations,
							provider: "simple"
						}, null, 2)
					}],
					details: { results: withCitations }
				};
			} catch (err) {
				const message = err instanceof Error ? err.message : String(err);
				return {
					content: [{
						type: "text",
						text: `Search error: ${message}`
					}],
					details: { error: message }
				};
			}
		}
	};
}
const MemoryGetSchema = Type.Object({
	path: Type.String(),
	from: Type.Optional(Type.Number()),
	lines: Type.Optional(Type.Number())
});
function createMemoryGetTool(options) {
	const { workspaceDir, memoriesDir } = options;
	return {
		name: "memory_get",
		label: "📄 Memory Get",
		description: "Safe snippet read from MEMORY.md or memory/*.md with optional from/lines; use after memory_search to pull only the needed lines and keep context small.",
		parameters: MemoryGetSchema,
		async execute(_toolCallId, params, _signal) {
			const { path, from, lines } = params;
			try {
				const result = memoryGet(workspaceDir, path, from, lines, memoriesDir);
				if (!result) return {
					content: [{
						type: "text",
						text: `File not found: ${path}`
					}],
					details: {
						path,
						text: ""
					}
				};
				return {
					content: [{
						type: "text",
						text: result.content
					}],
					details: {
						path,
						text: result.content,
						lineNumbers: result.lineNumbers
					}
				};
			} catch (err) {
				const message = err instanceof Error ? err.message : String(err);
				return {
					content: [{
						type: "text",
						text: `Read error: ${message}`
					}],
					details: { error: message }
				};
			}
		}
	};
}
//#endregion
export { createMemoryGetTool, createMemorySearchTool };

//# sourceMappingURL=memory-tool.js.map