summaryrefslogtreecommitdiffhomepage
path: root/src/tools.ts
blob: 0cc5b51fd6d7dd467947b21f44c34689435fde14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
import type { App } from "obsidian";
import { TFile } from "obsidian";

// Tool context JSON imports
import searchFilesCtx from "./context/tools/search-files.json";
import readFileCtx from "./context/tools/read-file.json";
import deleteFileCtx from "./context/tools/delete-file.json";
import getCurrentNoteCtx from "./context/tools/get-current-note.json";
import editFileCtx from "./context/tools/edit-file.json";
import grepSearchCtx from "./context/tools/grep-search.json";
import createFileCtx from "./context/tools/create-file.json";
import moveFileCtx from "./context/tools/move-file.json";
import setFrontmatterCtx from "./context/tools/set-frontmatter.json";
import batchSearchFilesCtx from "./context/tools/batch-search-files.json";
import batchGrepSearchCtx from "./context/tools/batch-grep-search.json";
import batchDeleteFileCtx from "./context/tools/batch-delete-file.json";
import batchMoveFileCtx from "./context/tools/batch-move-file.json";
import batchSetFrontmatterCtx from "./context/tools/batch-set-frontmatter.json";
import batchEditFileCtx from "./context/tools/batch-edit-file.json";
import saveImageCtx from "./context/tools/save-image.json";
import { getCurrentAttachments, clearCurrentAttachments } from "./image-attachments";

/**
 * Schema for an Ollama tool definition (function calling).
 */
export interface OllamaToolDefinition {
	type: "function";
	function: {
		name: string;
		description: string;
		parameters: {
			type: "object";
			required: string[];
			properties: Record<string, { type: string; description: string }>;
		};
	};
}

/**
 * Shape of a tool context JSON file.
 */
interface ToolContext {
	id: string;
	label: string;
	description: string;
	friendlyName: string;
	requiresApproval: boolean;
	batchOf?: string;
	definition: OllamaToolDefinition;
}

/**
 * Cast a tool context JSON import to the ToolContext type.
 * The JSON imports are typed as their literal shapes; this asserts
 * they conform to the ToolContext interface at the boundary.
 */
function asToolContext(json: Record<string, unknown>): ToolContext {
	return json as unknown as ToolContext;
}

/**
 * Metadata for a tool the user can enable/disable.
 */
export interface ToolEntry {
	id: string;
	label: string;
	description: string;
	friendlyName: string;
	requiresApproval: boolean;
	/** If set, this batch tool is auto-enabled when the named base tool is enabled. */
	batchOf?: string;
	approvalMessage?: (args: Record<string, unknown>) => string;
	summarize: (args: Record<string, unknown>) => string;
	summarizeResult: (result: string) => string;
	definition: OllamaToolDefinition;
	execute: (app: App, args: Record<string, unknown>) => Promise<string>;
}

/**
 * Execute the "search_files" tool.
 * Returns a newline-separated list of vault file paths matching the query.
 */
async function executeSearchFiles(app: App, args: Record<string, unknown>): Promise<string> {
	const query = typeof args["query"] === "string" ? args["query"].toLowerCase() : "";
	if (query === "") {
		// Detect common misuse: model passed batch_search_files params to search_files
		if (args["queries"] !== undefined) {
			return "Error: query parameter is required. You passed 'queries' (plural) — use search_files with a single 'query' string, or use batch_search_files for multiple queries.";
		}
		return "Error: query parameter is required.";
	}

	const files = app.vault.getFiles();
	const matches: string[] = [];

	for (const file of files) {
		if (file.path.toLowerCase().includes(query)) {
			matches.push(file.path);
		}
	}

	if (matches.length === 0) {
		return "No files found matching the query.";
	}

	// Cap results to avoid overwhelming the context
	const maxResults = 50;
	const limited = matches.slice(0, maxResults);
	const suffix = matches.length > maxResults
		? `\n... and ${matches.length - maxResults} more results.`
		: "";

	return limited.join("\n") + suffix;
}

/**
 * Execute the "read_file" tool.
 * Returns the full text content of a file by its vault path,
 * plus parsed frontmatter as a JSON block if present.
 */
async function executeReadFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const file = app.vault.getAbstractFileByPath(filePath);
	if (file === null || !(file instanceof TFile)) {
		return `Error: File not found at path "${filePath}".`;
	}

	try {
		const content = await app.vault.cachedRead(file);

		// Include parsed frontmatter as JSON if available
		const cache = app.metadataCache.getFileCache(file);
		if (cache?.frontmatter !== undefined) {
			const fm: Record<string, unknown> = {};
			for (const [key, value] of Object.entries(cache.frontmatter)) {
				if (key !== "position") {
					fm[key] = value;
				}
			}
			const fmJson = JSON.stringify(fm, null, 2);
			return `--- FRONTMATTER (parsed) ---\n${fmJson}\n--- END FRONTMATTER ---\n\n--- FILE CONTENT ---\n${content}\n--- END FILE CONTENT ---`;
		}

		return content;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error reading file: ${msg}`;
	}
}

/**
 * Execute the "delete_file" tool.
 * Deletes a file by its vault path (moves to trash).
 */
async function executeDeleteFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const file = app.vault.getAbstractFileByPath(filePath);
	if (file === null || !(file instanceof TFile)) {
		return `Error: File not found at path "${filePath}".`;
	}

	try {
		await app.vault.trash(file, true);
		return `File "${filePath}" has been deleted (moved to system trash).`;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error deleting file: ${msg}`;
	}
}

/**
 * Execute the "grep_search" tool.
 * Searches file contents for a text query, returning matching lines with context.
 */
async function executeGrepSearch(app: App, args: Record<string, unknown>): Promise<string> {
	const query = typeof args["query"] === "string" ? args["query"] : "";
	if (query === "") {
		if (args["queries"] !== undefined) {
			return "Error: query parameter is required. You passed 'queries' (plural) — use grep_search with a single 'query' string, or use batch_grep_search for multiple queries.";
		}
		return "Error: query parameter is required.";
	}

	const filePattern = typeof args["file_pattern"] === "string" ? args["file_pattern"].toLowerCase() : "";
	const queryLower = query.toLowerCase();

	const files = app.vault.getMarkdownFiles();
	const results: string[] = [];
	const maxResults = 50;
	let totalMatches = 0;

	for (const file of files) {
		if (totalMatches >= maxResults) break;

		// Optional file pattern filter
		if (filePattern !== "" && !file.path.toLowerCase().includes(filePattern)) {
			continue;
		}

		try {
			const content = await app.vault.cachedRead(file);
			const lines = content.split("\n");

			for (let i = 0; i < lines.length; i++) {
				const line = lines[i];
				if (line !== undefined && line.toLowerCase().includes(queryLower)) {
					results.push(`${file.path}:${i + 1}: ${line.trim()}`);
					totalMatches++;
					if (totalMatches >= maxResults) break;
				}
			}
		} catch {
			// Skip files that can't be read
		}
	}

	if (results.length === 0) {
		return "No matches found.";
	}

	const suffix = totalMatches >= maxResults
		? `\n... results capped at ${maxResults}. Narrow your query for more specific results.`
		: "";

	return results.join("\n") + suffix;
}

/**
 * Execute the "create_file" tool.
 * Creates a new file at the given vault path with the provided content.
 */
async function executeCreateFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const content = typeof args["content"] === "string" ? args["content"] : "";

	// Check if file already exists
	const existing = app.vault.getAbstractFileByPath(filePath);
	if (existing !== null) {
		return `Error: A file already exists at "${filePath}". Use edit_file to modify it.`;
	}

	try {
		// Ensure parent folder exists
		const lastSlash = filePath.lastIndexOf("/");
		if (lastSlash > 0) {
			const folderPath = filePath.substring(0, lastSlash);
			const folder = app.vault.getFolderByPath(folderPath);
			if (folder === null) {
				await app.vault.createFolder(folderPath);
			}
		}

		await app.vault.create(filePath, content);
		return `File created at "${filePath}".`;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error creating file: ${msg}`;
	}
}

/**
 * Execute the "move_file" tool.
 * Moves or renames a file, auto-updating all links.
 */
async function executeMoveFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const newPath = typeof args["new_path"] === "string" ? args["new_path"] : "";
	if (newPath === "") {
		return "Error: new_path parameter is required.";
	}

	const file = app.vault.getAbstractFileByPath(filePath);
	if (file === null || !(file instanceof TFile)) {
		return `Error: File not found at path "${filePath}".`;
	}

	// Check if destination already exists
	const destExists = app.vault.getAbstractFileByPath(newPath);
	if (destExists !== null) {
		return `Error: A file or folder already exists at "${newPath}".`;
	}

	try {
		// Ensure target folder exists
		const lastSlash = newPath.lastIndexOf("/");
		if (lastSlash > 0) {
			const folderPath = newPath.substring(0, lastSlash);
			const folder = app.vault.getFolderByPath(folderPath);
			if (folder === null) {
				await app.vault.createFolder(folderPath);
			}
		}

		await app.fileManager.renameFile(file, newPath);
		return `File moved from "${filePath}" to "${newPath}". All links have been updated.`;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error moving file: ${msg}`;
	}
}

/**
 * Execute the "get_current_note" tool.
 * Returns the vault-relative path of the currently active note.
 */
async function executeGetCurrentNote(app: App, _args: Record<string, unknown>): Promise<string> {
	const file = app.workspace.getActiveFile();
	if (file === null) {
		return "Error: No note is currently open.";
	}
	return file.path;
}

/**
 * Execute the "edit_file" tool.
 * Performs a find-and-replace on the file content using vault.process() for atomicity.
 */
async function executeEditFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const oldText = typeof args["old_text"] === "string" ? args["old_text"] : "";
	const newText = typeof args["new_text"] === "string" ? args["new_text"] : "";

	// Reject no-op edits where old_text and new_text are identical
	if (oldText === newText) {
		return `Error: old_text and new_text are identical — no change would occur. Provide different text for new_text, or skip this edit.`;
	}

	const file = app.vault.getAbstractFileByPath(filePath);
	if (file === null || !(file instanceof TFile)) {
		return `Error: File not found at path "${filePath}".`;
	}

	try {
		if (oldText === "") {
			// Empty old_text: only allowed when the file is empty (write initial content)
			let replaced = false;
			await app.vault.process(file, (data) => {
				if (data.length !== 0) {
					return data;
				}
				replaced = true;
				return newText;
			});

			if (!replaced) {
				return `Error: old_text is empty but "${filePath}" is not empty. You must read the file first with read_file and provide the exact text you want to replace as old_text.`;
			}

			return `Successfully wrote content to empty file "${filePath}".`;
		}

		let replaced = false;
		await app.vault.process(file, (data) => {
			if (!data.includes(oldText)) {
				return data;
			}
			replaced = true;
			return data.replace(oldText, newText);
		});

		if (!replaced) {
			return `Error: The specified old_text was not found in "${filePath}". Make sure you read the file first with read_file and copy the exact text.`;
		}

		return `Successfully edited "${filePath}".`;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error editing file: ${msg}`;
	}
}

/**
 * Execute the "set_frontmatter" tool.
 * Atomically sets or updates frontmatter properties using processFrontMatter().
 * The `properties` argument is a JSON object whose keys are set/overwritten in the YAML block.
 * To remove a property, set its value to null.
 */
async function executeSetFrontmatter(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	let properties = args["properties"];

	// The model may pass properties as a JSON string — parse it
	if (typeof properties === "string") {
		try {
			properties = JSON.parse(properties) as unknown;
		} catch {
			return "Error: properties must be a valid JSON object. Failed to parse the string.";
		}
	}

	if (typeof properties !== "object" || properties === null || Array.isArray(properties)) {
		return "Error: properties must be a JSON object with key-value pairs.";
	}

	const propsObj = properties as Record<string, unknown>;
	if (Object.keys(propsObj).length === 0) {
		return "Error: properties object is empty. Provide at least one key to set.";
	}

	const file = app.vault.getAbstractFileByPath(filePath);
	if (file === null || !(file instanceof TFile)) {
		return `Error: File not found at path "${filePath}".`;
	}

	try {
		const keysSet: string[] = [];
		const keysRemoved: string[] = [];

		await app.fileManager.processFrontMatter(file, (fm) => {
			for (const [key, value] of Object.entries(propsObj)) {
				if (value === null) {
					// Remove the property
					if (key in fm) {
						delete fm[key];
						keysRemoved.push(key);
					}
				} else {
					fm[key] = value;
					keysSet.push(key);
				}
			}
		});

		const parts: string[] = [];
		if (keysSet.length > 0) {
			parts.push(`Set: ${keysSet.join(", ")}`);
		}
		if (keysRemoved.length > 0) {
			parts.push(`Removed: ${keysRemoved.join(", ")}`);
		}

		return `Frontmatter updated for "${filePath}". ${parts.join(". ")}.`;
	} catch (err: unknown) {
		const msg = err instanceof Error ? err.message : "Unknown error";
		return `Error updating frontmatter: ${msg}`;
	}
}

// ---------------------------------------------------------------------------
// Save image tool
// ---------------------------------------------------------------------------

/**
 * Map MIME types to file extensions.
 */
function mimeToExtension(mimeType: string): string {
	const map: Record<string, string> = {
		"image/jpeg": ".jpg",
		"image/png": ".png",
		"image/gif": ".gif",
		"image/webp": ".webp",
		"image/bmp": ".bmp",
		"image/svg+xml": ".svg",
	};
	return map[mimeType] ?? ".png";
}

/**
 * Execute the "save_image" tool.
 * Saves attached image(s) to the vault at the specified path.
 */
async function executeSaveImage(app: App, args: Record<string, unknown>): Promise<string> {
	const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
	if (filePath === "") {
		return "Error: file_path parameter is required.";
	}

	const attachments = getCurrentAttachments();
	if (attachments.length === 0) {
		return "Error: No images are attached to the current message.";
	}

	const savedPaths: string[] = [];
	const errors: string[] = [];

	for (let i = 0; i < attachments.length; i++) {
		const attachment = attachments[i];
		if (attachment === undefined) continue;

		const ext = mimeToExtension(attachment.mimeType);
		const fullPath = attachments.length === 1
			? `${filePath}${ext}`
			: `${filePath}_${i + 1}${ext}`;

		// Check if file already exists
		const existing = app.vault.getAbstractFileByPath(fullPath);
		if (existing !== null) {
			errors.push(`"${fullPath}" already exists — skipped.`);
			continue;
		}

		// Ensure parent folder exists
		const lastSlash = fullPath.lastIndexOf("/");
		if (lastSlash > 0) {
			const folderPath = fullPath.substring(0, lastSlash);
			const folder = app.vault.getFolderByPath(folderPath);
			if (folder === null) {
				await app.vault.createFolder(folderPath);
			}
		}

		try {
			await app.vault.createBinary(fullPath, attachment.arrayBuffer);
			savedPaths.push(fullPath);
		} catch (err: unknown) {
			const msg = err instanceof Error ? err.message : "Unknown error";
			errors.push(`"${fullPath}": ${msg}`);
		}
	}

	clearCurrentAttachments();

	const parts: string[] = [];
	if (savedPaths.length > 0) {
		parts.push(`Saved ${savedPaths.length} image(s):\n${savedPaths.map(p => `- ${p}`).join("\n")}`);
	}
	if (errors.length > 0) {
		parts.push(`Errors:\n${errors.map(e => `- ${e}`).join("\n")}`);
	}

	return parts.join("\n\n");
}

// ---------------------------------------------------------------------------
// Batch tool execute functions
// ---------------------------------------------------------------------------

/**
 * Helper: parse an array-typed argument that may arrive as a JSON string.
 */
function parseArrayArg(value: unknown): unknown[] | null {
	if (Array.isArray(value)) return value;
	if (typeof value === "string") {
		try {
			const parsed = JSON.parse(value) as unknown;
			if (Array.isArray(parsed)) return parsed;
		} catch { /* fall through */ }
	}
	return null;
}

/**
 * Execute the "batch_search_files" tool.
 * Runs multiple search queries and returns combined results.
 */
async function executeBatchSearchFiles(app: App, args: Record<string, unknown>): Promise<string> {
	const queries = parseArrayArg(args["queries"]);
	if (queries === null || queries.length === 0) {
		return "Error: queries parameter must be a non-empty array of strings.";
	}

	const results: string[] = [];
	for (let i = 0; i < queries.length; i++) {
		const q = queries[i];
		const query = typeof q === "string" ? q : "";
		const result = await executeSearchFiles(app, { query });
		results.push(`--- Query ${i + 1}: "${query}" ---\n${result}`);
	}

	return results.join("\n\n");
}

/**
 * Execute the "batch_grep_search" tool.
 * Runs multiple content searches and returns combined results.
 */
async function executeBatchGrepSearch(app: App, args: Record<string, unknown>): Promise<string> {
	const queries = parseArrayArg(args["queries"]);
	if (queries === null || queries.length === 0) {
		return "Error: queries parameter must be a non-empty array of search query objects.";
	}

	const results: string[] = [];
	for (let i = 0; i < queries.length; i++) {
		const q = queries[i];
		if (typeof q !== "object" || q === null) {
			results.push(`--- Query ${i + 1} ---\nError: each query must be an object with a "query" field.`);
			continue;
		}
		const queryObj = q as Record<string, unknown>;
		const result = await executeGrepSearch(app, queryObj);
		const queryText = typeof queryObj["query"] === "string" ? queryObj["query"] : "";
		const filePattern = typeof queryObj["file_pattern"] === "string" ? ` (in "${queryObj["file_pattern"]}")` : "";
		results.push(`--- Query ${i + 1}: "${queryText}"${filePattern} ---\n${result}`);
	}

	return results.join("\n\n");
}

/**
 * Execute the "batch_delete_file" tool.
 * Deletes multiple files, continuing on failure and reporting per-file results.
 */
async function executeBatchDeleteFile(app: App, args: Record<string, unknown>): Promise<string> {
	const filePaths = parseArrayArg(args["file_paths"]);
	if (filePaths === null || filePaths.length === 0) {
		return "Error: file_paths parameter must be a non-empty array of strings.";
	}

	const results: string[] = [];
	let successes = 0;
	let failures = 0;

	for (const fp of filePaths) {
		const filePath = typeof fp === "string" ? fp : "";
		const result = await executeDeleteFile(app, { file_path: filePath });
		if (result.startsWith("Error")) {
			failures++;
		} else {
			successes++;
		}
		results.push(`${filePath}: ${result}`);
	}

	const summary = `Batch delete complete: ${successes} succeeded, ${failures} failed.`;
	return `${summary}\n\n${results.join("\n")}`;
}

/**
 * Execute the "batch_move_file" tool.
 * Moves/renames multiple files, continuing on failure.
 */
async function executeBatchMoveFile(app: App, args: Record<string, unknown>): Promise<string> {
	const operations = parseArrayArg(args["operations"]);
	if (operations === null || operations.length === 0) {
		return "Error: operations parameter must be a non-empty array of {file_path, new_path} objects.";
	}

	const results: string[] = [];
	let successes = 0;
	let failures = 0;

	for (const op of operations) {
		if (typeof op !== "object" || op === null) {
			results.push("(invalid entry): Error: each operation must be an object with file_path and new_path.");
			failures++;
			continue;
		}
		const opObj = op as Record<string, unknown>;
		const filePath = typeof opObj["file_path"] === "string" ? opObj["file_path"] : "";
		const newPath = typeof opObj["new_path"] === "string" ? opObj["new_path"] : "";
		const result = await executeMoveFile(app, { file_path: filePath, new_path: newPath });
		if (result.startsWith("Error")) {
			failures++;
		} else {
			successes++;
		}
		results.push(`${filePath} → ${newPath}: ${result}`);
	}

	const summary = `Batch move complete: ${successes} succeeded, ${failures} failed.`;
	return `${summary}\n\n${results.join("\n")}`;
}

/**
 * Execute the "batch_set_frontmatter" tool.
 * Sets frontmatter on multiple files, continuing on failure.
 */
async function executeBatchSetFrontmatter(app: App, args: Record<string, unknown>): Promise<string> {
	const operations = parseArrayArg(args["operations"]);
	if (operations === null || operations.length === 0) {
		return "Error: operations parameter must be a non-empty array of {file_path, properties} objects.";
	}

	const results: string[] = [];
	let successes = 0;
	let failures = 0;

	for (const op of operations) {
		if (typeof op !== "object" || op === null) {
			results.push("(invalid entry): Error: each operation must be an object with file_path and properties.");
			failures++;
			continue;
		}
		const opObj = op as Record<string, unknown>;
		const filePath = typeof opObj["file_path"] === "string" ? opObj["file_path"] : "";
		const result = await executeSetFrontmatter(app, { file_path: filePath, properties: opObj["properties"] });
		if (result.startsWith("Error")) {
			failures++;
		} else {
			successes++;
		}
		results.push(`${filePath}: ${result}`);
	}

	const summary = `Batch frontmatter update complete: ${successes} succeeded, ${failures} failed.`;
	return `${summary}\n\n${results.join("\n")}`;
}

/**
 * Execute the "batch_edit_file" tool.
 * Performs multiple file edits, continuing on failure.
 */
async function executeBatchEditFile(app: App, args: Record<string, unknown>): Promise<string> {
	const operations = parseArrayArg(args["operations"]);
	if (operations === null || operations.length === 0) {
		return "Error: operations parameter must be a non-empty array of {file_path, old_text, new_text} objects.";
	}

	const results: string[] = [];
	let successes = 0;
	let failures = 0;

	for (const op of operations) {
		if (typeof op !== "object" || op === null) {
			results.push("(invalid entry): Error: each operation must be an object with file_path, old_text, and new_text.");
			failures++;
			continue;
		}
		const opObj = op as Record<string, unknown>;
		const filePath = typeof opObj["file_path"] === "string" ? opObj["file_path"] : "";
		const result = await executeEditFile(app, {
			file_path: filePath,
			old_text: opObj["old_text"],
			new_text: opObj["new_text"],
		});
		if (result.startsWith("Error")) {
			failures++;
		} else {
			successes++;
		}
		results.push(`${filePath}: ${result}`);
	}

	const summary = `Batch edit complete: ${successes} succeeded, ${failures} failed.`;
	return `${summary}\n\n${results.join("\n")}`;
}

/**
 * All available tools for the plugin.
 * Metadata (id, label, description, friendlyName, requiresApproval, batchOf, definition)
 * is loaded from JSON context files in src/context/tools/.
 * Only runtime logic (summarize, summarizeResult, approvalMessage, execute) is defined here.
 */
export const TOOL_REGISTRY: ToolEntry[] = [
	{
		...asToolContext(searchFilesCtx as Record<string, unknown>),
		summarize: (args) => {
			const query = typeof args["query"] === "string" ? args["query"] : "";
			if (query === "" && args["queries"] !== undefined) {
				return "(wrong params: used 'queries' instead of 'query')";
			}
			return `"${query}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result === "No files found matching the query.") {
				return "No results found";
			}
			const lines = result.split("\n").filter((l) => l.length > 0);
			const moreMatch = result.match(/\.\.\.\s*and\s+(\d+)\s+more/);
			const extraCount = moreMatch !== null ? parseInt(moreMatch[1] ?? "0", 10) : 0;
			const count = lines.length - (moreMatch !== null ? 1 : 0) + extraCount;
			return `${count} result${count === 1 ? "" : "s"} found`;
		},
		execute: executeSearchFiles,
	},
	{
		...asToolContext(readFileCtx as Record<string, unknown>),
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			return `"/${filePath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			const lines = result.split("\n").length;
			return `${lines} line${lines === 1 ? "" : "s"} read`;
		},
		execute: executeReadFile,
	},
	{
		...asToolContext(deleteFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			return `Delete "${filePath}"?`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			return `"/${filePath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result.includes("declined")) {
				return "Declined by user";
			}
			return "File deleted";
		},
		execute: executeDeleteFile,
	},
	{
		...asToolContext(getCurrentNoteCtx as Record<string, unknown>),
		summarize: () => "Checking active note",
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			return `"/${result}"`;
		},
		execute: executeGetCurrentNote,
	},
	{
		...asToolContext(editFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			return `Edit "${filePath}"?`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			return `"/${filePath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result.includes("declined")) {
				return "Declined by user";
			}
			return "File edited";
		},
		execute: executeEditFile,
	},
	{
		...asToolContext(grepSearchCtx as Record<string, unknown>),
		summarize: (args) => {
			const query = typeof args["query"] === "string" ? args["query"] : "";
			const filePattern = typeof args["file_pattern"] === "string" ? args["file_pattern"] : "";
			if (query === "" && args["queries"] !== undefined) {
				return "(wrong params: used 'queries' instead of 'query')";
			}
			const suffix = filePattern !== "" ? ` in "${filePattern}"` : "";
			return `"${query}"${suffix}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result === "No matches found.") {
				return "No results found";
			}
			const lines = result.split("\n").filter((l) => l.length > 0 && !l.startsWith("..."));
			const cappedMatch = result.match(/results capped at (\d+)/);
			const count = cappedMatch !== null ? `${cappedMatch[1] ?? "?"}+` : `${lines.length}`;
			return `${count} match${lines.length === 1 ? "" : "es"} found`;
		},
		execute: executeGrepSearch,
	},
	{
		...asToolContext(createFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			return `Create "${filePath}"?`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			return `"/${filePath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result.includes("declined")) {
				return "Declined by user";
			}
			return "File created";
		},
		execute: executeCreateFile,
	},
	{
		...asToolContext(moveFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			const newPath = typeof args["new_path"] === "string" ? args["new_path"] : "unknown";
			return `Move "${filePath}" to "${newPath}"?`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			const newPath = typeof args["new_path"] === "string" ? args["new_path"] : "";
			return `"/${filePath}" \u2192 "/${newPath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result.includes("declined")) {
				return "Declined by user";
			}
			return "File moved";
		},
		execute: executeMoveFile,
	},
	{
		...asToolContext(setFrontmatterCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			const props = typeof args["properties"] === "object" && args["properties"] !== null
				? Object.keys(args["properties"] as Record<string, unknown>)
				: [];
			return `Update frontmatter in "${filePath}"? Properties: ${props.join(", ")}`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			const props = typeof args["properties"] === "object" && args["properties"] !== null
				? Object.keys(args["properties"] as Record<string, unknown>)
				: [];
			return `"/${filePath}" \u2014 ${props.join(", ")}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) {
				return result;
			}
			if (result.includes("declined")) {
				return "Declined by user";
			}
			return "Frontmatter updated";
		},
		execute: executeSetFrontmatter,
	},
	{
		...asToolContext(saveImageCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "unknown";
			const count = getCurrentAttachments().length;
			return `Save ${count} image(s) to "${filePath}"?`;
		},
		summarize: (args) => {
			const filePath = typeof args["file_path"] === "string" ? args["file_path"] : "";
			const count = getCurrentAttachments().length;
			return `${count} image(s) \u2192 "/${filePath}"`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			if (result.includes("declined")) return "Declined by user";
			const match = result.match(/Saved (\d+) image/);
			if (match !== null) return `${match[1]} image(s) saved`;
			return "Images saved";
		},
		execute: executeSaveImage,
	},
	// --- Batch tools ---
	{
		...asToolContext(batchSearchFilesCtx as Record<string, unknown>),
		summarize: (args) => {
			const queries = parseArrayArg(args["queries"]);
			const count = queries !== null ? queries.length : 0;
			return `${count} search quer${count === 1 ? "y" : "ies"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			const sections = result.split("--- Query").length - 1;
			return `${sections} search${sections === 1 ? "" : "es"} completed`;
		},
		execute: executeBatchSearchFiles,
	},
	{
		...asToolContext(batchGrepSearchCtx as Record<string, unknown>),
		summarize: (args) => {
			const queries = parseArrayArg(args["queries"]);
			const count = queries !== null ? queries.length : 0;
			return `${count} content search${count === 1 ? "" : "es"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			const sections = result.split("--- Query").length - 1;
			return `${sections} search${sections === 1 ? "" : "es"} completed`;
		},
		execute: executeBatchGrepSearch,
	},
	{
		...asToolContext(batchDeleteFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const filePaths = parseArrayArg(args["file_paths"]);
			if (filePaths === null || filePaths.length === 0) return "Delete files?";
			const list = filePaths.map((fp) => `  \u2022 ${typeof fp === "string" ? fp : "(invalid)"}`);
			return `Delete ${filePaths.length} file${filePaths.length === 1 ? "" : "s"}?\n${list.join("\n")}`;
		},
		summarize: (args) => {
			const filePaths = parseArrayArg(args["file_paths"]);
			const count = filePaths !== null ? filePaths.length : 0;
			return `${count} file${count === 1 ? "" : "s"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			if (result.includes("declined")) return "Declined by user";
			const match = result.match(/(\d+) succeeded, (\d+) failed/);
			if (match !== null) return `${match[1]} deleted, ${match[2]} failed`;
			return "Batch delete complete";
		},
		execute: executeBatchDeleteFile,
	},
	{
		...asToolContext(batchMoveFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const operations = parseArrayArg(args["operations"]);
			if (operations === null || operations.length === 0) return "Move files?";
			const list = operations.map((op) => {
				if (typeof op !== "object" || op === null) return "  \u2022 (invalid entry)";
				const o = op as Record<string, unknown>;
				const from = typeof o["file_path"] === "string" ? o["file_path"] : "?";
				const to = typeof o["new_path"] === "string" ? o["new_path"] : "?";
				return `  \u2022 ${from} \u2192 ${to}`;
			});
			return `Move ${operations.length} file${operations.length === 1 ? "" : "s"}?\n${list.join("\n")}`;
		},
		summarize: (args) => {
			const operations = parseArrayArg(args["operations"]);
			const count = operations !== null ? operations.length : 0;
			return `${count} file${count === 1 ? "" : "s"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			if (result.includes("declined")) return "Declined by user";
			const match = result.match(/(\d+) succeeded, (\d+) failed/);
			if (match !== null) return `${match[1]} moved, ${match[2]} failed`;
			return "Batch move complete";
		},
		execute: executeBatchMoveFile,
	},
	{
		...asToolContext(batchSetFrontmatterCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const operations = parseArrayArg(args["operations"]);
			if (operations === null || operations.length === 0) return "Update frontmatter?";
			const list = operations.map((op) => {
				if (typeof op !== "object" || op === null) return "  \u2022 (invalid entry)";
				const o = op as Record<string, unknown>;
				const fp = typeof o["file_path"] === "string" ? o["file_path"] : "?";
				let propsStr = "";
				if (typeof o["properties"] === "object" && o["properties"] !== null) {
					propsStr = Object.keys(o["properties"] as Record<string, unknown>).join(", ");
				} else if (typeof o["properties"] === "string") {
					try {
						const parsed = JSON.parse(o["properties"]) as Record<string, unknown>;
						propsStr = Object.keys(parsed).join(", ");
					} catch { propsStr = "(properties)"; }
				}
				return `  \u2022 ${fp}: ${propsStr}`;
			});
			return `Update frontmatter on ${operations.length} file${operations.length === 1 ? "" : "s"}?\n${list.join("\n")}`;
		},
		summarize: (args) => {
			const operations = parseArrayArg(args["operations"]);
			const count = operations !== null ? operations.length : 0;
			return `${count} file${count === 1 ? "" : "s"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			if (result.includes("declined")) return "Declined by user";
			const match = result.match(/(\d+) succeeded, (\d+) failed/);
			if (match !== null) return `${match[1]} updated, ${match[2]} failed`;
			return "Batch frontmatter update complete";
		},
		execute: executeBatchSetFrontmatter,
	},
	{
		...asToolContext(batchEditFileCtx as Record<string, unknown>),
		approvalMessage: (args) => {
			const operations = parseArrayArg(args["operations"]);
			if (operations === null || operations.length === 0) return "Edit files?";
			const list = operations.map((op) => {
				if (typeof op !== "object" || op === null) return "  \u2022 (invalid entry)";
				const o = op as Record<string, unknown>;
				const fp = typeof o["file_path"] === "string" ? o["file_path"] : "?";
				return `  \u2022 ${fp}`;
			});
			return `Edit ${operations.length} file${operations.length === 1 ? "" : "s"}?\n${list.join("\n")}`;
		},
		summarize: (args) => {
			const operations = parseArrayArg(args["operations"]);
			const count = operations !== null ? operations.length : 0;
			return `${count} file${count === 1 ? "" : "s"}`;
		},
		summarizeResult: (result) => {
			if (result.startsWith("Error")) return result;
			if (result.includes("declined")) return "Declined by user";
			const match = result.match(/(\d+) succeeded, (\d+) failed/);
			if (match !== null) return `${match[1]} edited, ${match[2]} failed`;
			return "Batch edit complete";
		},
		execute: executeBatchEditFile,
	},
];

/**
 * Get the default enabled state for all tools (all disabled).
 */
export function getDefaultToolStates(): Record<string, boolean> {
	const states: Record<string, boolean> = {};
	for (const tool of TOOL_REGISTRY) {
		// Batch tools inherit from their parent — no separate toggle
		if (tool.batchOf !== undefined) continue;
		states[tool.id] = false;
	}
	return states;
}

/**
 * Look up a tool entry by function name.
 */
export function findToolByName(name: string): ToolEntry | undefined {
	return TOOL_REGISTRY.find((t) => t.definition.function.name === name);
}