-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmemoryTool.tsx
More file actions
848 lines (747 loc) · 34.2 KB
/
memoryTool.tsx
File metadata and controls
848 lines (747 loc) · 34.2 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import type * as vscode from 'vscode';
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
import { createDirectoryIfNotExists, IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { FileType } from '../../../platform/filesystem/common/fileTypes';
import { ILogService } from '../../../platform/log/common/logService';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { URI } from '../../../util/vs/base/common/uri';
import { LanguageModelTextPart, LanguageModelToolResult, MarkdownString } from '../../../vscodeTypes';
import { IAgentMemoryService, isCopilotMemoryConfigEnabled, RepoMemoryEntry } from '../common/agentMemoryService';
import { IMemoryCleanupService } from '../common/memoryCleanupService';
import { ToolName } from '../common/toolNames';
import { ICopilotTool, ToolRegistry } from '../common/toolsRegistry';
import { formatUriForFileWidget } from '../common/toolUtils';
const MEMORY_BASE_DIR = 'memory-tool/memories';
const REPO_PATH_PREFIX = '/memories/repo';
const SESSION_PATH_PREFIX = '/memories/session';
type MemoryScope = 'user' | 'session' | 'repo';
interface IViewParams {
command: 'view';
path: string;
view_range?: [number, number];
}
interface ICreateParams {
command: 'create';
path: string;
file_text: string;
}
interface IStrReplaceParams {
command: 'str_replace';
path: string;
old_str: string;
new_str: string;
}
interface IInsertParams {
command: 'insert';
path: string;
insert_line: number;
insert_text?: string;
/** Models sometimes send `new_str` instead of `insert_text` */
new_str?: string;
}
interface IDeleteParams {
command: 'delete';
path: string;
}
interface IRenameParams {
command: 'rename';
old_path?: string;
new_path: string;
/** Models sometimes send `path` instead of `old_path` */
path?: string;
}
type MemoryToolParams = IViewParams | ICreateParams | IStrReplaceParams | IInsertParams | IDeleteParams | IRenameParams;
function normalizePath(path: string): string {
return path.endsWith('/') ? path : path + '/';
}
function isMemoriesRoot(path: string): boolean {
return normalizePath(path) === '/memories/';
}
function validatePath(path: string): string | undefined {
if (!normalizePath(path).startsWith('/memories/')) {
return 'Error: All memory paths must start with /memories/';
}
if (path.includes('..')) {
return 'Error: Path traversal is not allowed';
}
// Reject paths with empty segments (e.g. /memories//etc) or that resolve outside the base
const segments = path.split('/').filter(s => s.length > 0);
if (segments.some(s => s === '.')) {
return 'Error: Path traversal is not allowed';
}
// After splitting, first segment must be "memories"
if (segments[0] !== 'memories') {
return 'Error: All memory paths must start with /memories/';
}
return undefined;
}
function isRepoPath(path: string): boolean {
return path === REPO_PATH_PREFIX || path.startsWith(REPO_PATH_PREFIX + '/');
}
function isSessionPath(path: string): boolean {
return path === SESSION_PATH_PREFIX || path.startsWith(SESSION_PATH_PREFIX + '/');
}
/**
* Extracts a safe directory name from a chatSessionResource URI string.
* The URI is typically like `vscode-chat-session://local/<sessionId>`.
*/
export function extractSessionId(sessionResource: string): string {
const parsed = URI.parse(sessionResource);
// Extract the last path segment as the session ID
const segments = parsed.path.replace(/^\//, '').split('/');
const raw = segments[segments.length - 1] || parsed.authority || 'unknown';
// Sanitize to only safe characters for a directory name
return raw.replace(/[^a-zA-Z0-9_.-]/g, '_');
}
function formatLineNumber(line: number): string {
return String(line).padStart(6, ' ');
}
function formatFileContent(path: string, content: string): string {
const lines = content.split('\n');
const numbered = lines.map((line, i) => `${formatLineNumber(i + 1)}\t${line}`);
return `Here's the content of ${path} with line numbers:\n${numbered.join('\n')}`;
}
function makeSnippet(fileContent: string, editLine: number, path: string): string {
const lines = fileContent.split('\n');
const snippetRadius = 4;
const start = Math.max(0, editLine - 1 - snippetRadius);
const end = Math.min(lines.length, editLine - 1 + snippetRadius + 1);
const snippet = lines.slice(start, end);
const numbered = snippet.map((line, i) => `${formatLineNumber(start + i + 1)}\t${line}`);
return `The memory file has been edited. Here's the result of running \`cat -n\` on a snippet of ${path}:\n${numbered.join('\n')}`;
}
// --- Tool implementation ---
type MemoryToolOutcome = 'success' | 'error' | 'notFound' | 'notEnabled';
interface MemoryToolResult {
text: string;
outcome: MemoryToolOutcome;
}
export class MemoryTool implements ICopilotTool<MemoryToolParams> {
public static readonly toolName = ToolName.Memory;
public static readonly nonDeferred = true;
constructor(
@IFileSystemService private readonly fileSystemService: IFileSystemService,
@IAgentMemoryService private readonly agentMemoryService: IAgentMemoryService,
@IMemoryCleanupService private readonly memoryCleanupService: IMemoryCleanupService,
@IVSCodeExtensionContext private readonly extensionContext: IVSCodeExtensionContext,
@ILogService private readonly logService: ILogService,
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IExperimentationService private readonly experimentationService: IExperimentationService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
) {
if (this.configurationService.getExperimentBasedConfig(ConfigKey.MemoryToolEnabled, this.experimentationService)) {
this.memoryCleanupService.start();
}
}
prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<MemoryToolParams>, _token: CancellationToken): vscode.ProviderResult<vscode.PreparedToolInvocation> {
const command = options.input.command;
const path = command === 'rename' ? (options.input as IRenameParams).old_path ?? (options.input as IRenameParams).path : options.input.path;
return this._prepareLocalInvocation(command, path ?? '/memories/', options.chatSessionResource);
}
private _prepareLocalInvocation(command: string, path: string, chatSessionResource?: vscode.Uri): vscode.PreparedToolInvocation {
// Directory paths (e.g. /memories/, /memories/session/, /memories/session) — show verb only, no file widget.
// Use normalizePath to handle paths with or without trailing slash consistently.
const normalized = normalizePath(path);
if (path.endsWith('/') || normalized === '/memories/session/' || normalized === '/memories/repo/' || isMemoriesRoot(path)) {
switch (command) {
case 'view':
return { invocationMessage: l10n.t('Reading memory'), pastTenseMessage: l10n.t('Read memory') };
case 'delete':
return { invocationMessage: l10n.t('Deleting memory'), pastTenseMessage: l10n.t('Deleted memory') };
default:
return { invocationMessage: l10n.t('Updating memory'), pastTenseMessage: l10n.t('Updated memory') };
}
}
const fw = this._resolveFileWidget(path, chatSessionResource);
switch (command) {
case 'view':
return { invocationMessage: new MarkdownString(l10n.t('Reading memory {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Read memory {0}', fw)) };
case 'create':
return { invocationMessage: new MarkdownString(l10n.t('Creating memory file {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Created memory file {0}', fw)) };
case 'str_replace':
return { invocationMessage: new MarkdownString(l10n.t('Updating memory file {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Updated memory file {0}', fw)) };
case 'insert':
return { invocationMessage: new MarkdownString(l10n.t('Inserting into memory file {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Inserted into memory file {0}', fw)) };
case 'delete':
return { invocationMessage: new MarkdownString(l10n.t('Deleting memory {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Deleted memory {0}', fw)) };
case 'rename':
return { invocationMessage: new MarkdownString(l10n.t('Renaming memory {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Renamed memory {0}', fw)) };
default:
return { invocationMessage: new MarkdownString(l10n.t('Updating memory {0}', fw)), pastTenseMessage: new MarkdownString(l10n.t('Updated memory {0}', fw)) };
}
}
/**
* Resolves a local memory path to a file widget string for display in invocation messages.
* Constructs the URI directly from storage URIs to avoid validation that may throw.
*/
private _resolveFileWidget(path: string, chatSessionResource?: vscode.Uri): string {
const segments = path.split('/').filter(s => s.length > 0);
if (isSessionPath(path)) {
const storageUri = this.extensionContext.storageUri;
if (!storageUri) {
return path;
}
// Session paths: /memories/session/foo.md → skip 'memories' and 'session'
const relativeSegments = segments.slice(2);
const baseUri = URI.file(URI.from(storageUri).path);
const pathParts = chatSessionResource
? [MEMORY_BASE_DIR, extractSessionId(URI.from(chatSessionResource).toString()), ...relativeSegments]
: [MEMORY_BASE_DIR, ...relativeSegments];
return formatUriForFileWidget(URI.joinPath(baseUri, ...pathParts));
}
if (isRepoPath(path)) {
const storageUri = this.extensionContext.storageUri;
if (!storageUri) {
return path;
}
// Repo paths: /memories/repo/foo.md → skip 'memories' and 'repo'
const relativeSegments = segments.slice(2);
const baseUri = URI.file(URI.from(storageUri).path);
return formatUriForFileWidget(URI.joinPath(baseUri, MEMORY_BASE_DIR, 'repo', ...relativeSegments));
}
const globalStorageUri = this.extensionContext.globalStorageUri;
if (!globalStorageUri) {
return path;
}
// User paths: /memories/foo.md → skip 'memories'
const relativeSegments = segments.slice(1);
const baseUri = URI.file(URI.from(globalStorageUri).path);
return formatUriForFileWidget(URI.joinPath(baseUri, MEMORY_BASE_DIR, ...relativeSegments));
}
async invoke(options: vscode.LanguageModelToolInvocationOptions<MemoryToolParams>, _token: CancellationToken): Promise<vscode.LanguageModelToolResult> {
const params = options.input;
const sessionResource = options.chatSessionResource?.toString();
const resultText = await this._dispatch(params, sessionResource, options.chatRequestId, options.model);
return new LanguageModelToolResult([new LanguageModelTextPart(resultText)]);
}
private async _dispatch(params: MemoryToolParams, sessionResource?: string, requestId?: string, model?: vscode.LanguageModelChat): Promise<string> {
const path = params.command === 'rename' ? (params.old_path ?? params.path) : params.path;
if (!path) {
this._sendLocalTelemetry(params.command, 'user', 'error', requestId, model);
return 'Error: Missing required path parameter.';
}
const pathError = validatePath(path);
if (pathError) {
this._sendLocalTelemetry(params.command, 'user', 'error', requestId, model);
return pathError;
}
// Route /memories/repo/* to CAPI if enabled, otherwise local storage
if (isRepoPath(path)) {
const capiEnabled = await this.agentMemoryService.checkMemoryEnabled();
if (capiEnabled) {
const result = await this._dispatchRepoCAPI(params, path);
this._sendRepoTelemetry(params.command, result.outcome, requestId, model);
return result.text;
}
// Fall back to local file-based repo memory
const result = await this._dispatchLocal(params, 'repo', sessionResource);
this._sendLocalTelemetry(params.command, 'repo', result.outcome, requestId, model);
return result.text;
}
// Route /memories/session/* to session-scoped local storage
// Everything else under /memories/* goes to user-scoped storage
const scope: MemoryScope = isSessionPath(path) ? 'session' : 'user';
const result = await this._dispatchLocal(params, scope, sessionResource);
this._sendLocalTelemetry(params.command, scope, result.outcome, requestId, model);
return result.text;
}
private async _dispatchRepoCAPI(params: MemoryToolParams, path: string): Promise<MemoryToolResult> {
switch (params.command) {
case 'create':
return this._repoCreate(params);
default:
return { text: `Error: The '${params.command}' operation is not supported for repository memories at ${path}. Only 'create' is allowed for /memories/repo/.`, outcome: 'error' };
}
}
private async _repoCreate(params: ICreateParams): Promise<MemoryToolResult> {
try {
// Derive subject/category hint from the path (e.g. /memories/repo/testing.json → "testing")
const filename = params.path.split('/').pop() || 'memory';
const pathHint = filename.replace(/\.\w+$/, '');
// Parse the file_text as a memory entry.
// Accept either a JSON-formatted entry or a plain text fact.
let entry: RepoMemoryEntry;
try {
const parsed = JSON.parse(params.file_text);
entry = {
subject: parsed.subject || pathHint,
fact: parsed.fact || '',
citations: parsed.citations || '',
reason: parsed.reason || '',
category: parsed.category || pathHint,
};
} catch {
// Plain text: treat the whole content as a fact, use path as subject
entry = {
subject: pathHint,
fact: params.file_text,
citations: '',
reason: 'Stored from memory tool create command.',
category: pathHint,
};
}
const success = await this.agentMemoryService.storeRepoMemory(entry);
if (success) {
return { text: `File created successfully at: ${params.path}`, outcome: 'success' };
} else {
return { text: 'Error: Failed to store repository memory entry.', outcome: 'error' };
}
} catch (error) {
this.logService.error('[MemoryTool] Error creating repo memory:', error);
return { text: `Error: Cannot create repository memory: ${error.message}`, outcome: 'error' };
}
}
private _resolveUri(memoryPath: string, scope: MemoryScope, sessionResource?: string): URI {
// Validate path format and extract relative path components safely
const pathError = validatePath(memoryPath);
if (pathError) {
throw new Error(pathError);
}
// Extract path components by splitting, skipping empty and special segments
const segments = memoryPath.split('/').filter(s => s.length > 0);
// segments[0] is 'memories', segments[1] might be 'session' or 'repo', rest are file path components
let relativeSegments: string[];
if (scope === 'session') {
const storageUri = this.extensionContext.storageUri;
if (!storageUri) {
throw new Error('No workspace storage available. Session memory operations require an active workspace.');
}
// For session paths: /memories/session/foo.md → ['memories', 'session', 'foo.md']
// Skip 'memories' and 'session', keep rest
relativeSegments = segments.slice(2);
const baseUri = URI.from(storageUri);
let resolved: URI;
if (sessionResource) {
const sessionId = extractSessionId(sessionResource);
resolved = relativeSegments.length > 0
? URI.joinPath(baseUri, MEMORY_BASE_DIR, sessionId, ...relativeSegments)
: URI.joinPath(baseUri, MEMORY_BASE_DIR, sessionId);
} else {
resolved = relativeSegments.length > 0
? URI.joinPath(baseUri, MEMORY_BASE_DIR, ...relativeSegments)
: URI.joinPath(baseUri, MEMORY_BASE_DIR);
}
if (!this.memoryCleanupService.isMemoryUri(resolved)) {
throw new Error('Resolved path escapes the memory storage directory.');
}
return resolved;
}
if (scope === 'repo') {
const storageUri = this.extensionContext.storageUri;
if (!storageUri) {
throw new Error('No workspace storage available. Repository memory operations require an active workspace.');
}
// For repo paths: /memories/repo/foo.md → ['memories', 'repo', 'foo.md']
// Skip 'memories' and 'repo', keep rest
relativeSegments = segments.slice(2);
const baseUri = URI.from(storageUri);
const resolved = relativeSegments.length > 0
? URI.joinPath(baseUri, MEMORY_BASE_DIR, 'repo', ...relativeSegments)
: URI.joinPath(baseUri, MEMORY_BASE_DIR, 'repo');
if (!this.memoryCleanupService.isMemoryUri(resolved)) {
throw new Error('Resolved path escapes the memory storage directory.');
}
return resolved;
}
// User scope: /memories/foo.md → ['memories', 'foo.md']
// Skip 'memories', keep rest
relativeSegments = segments.slice(1);
const globalStorageUri = this.extensionContext.globalStorageUri;
if (!globalStorageUri) {
throw new Error('No global storage available. User memory operations require global storage.');
}
const resolved = relativeSegments.length > 0
? URI.joinPath(globalStorageUri, MEMORY_BASE_DIR, ...relativeSegments)
: URI.joinPath(globalStorageUri, MEMORY_BASE_DIR);
return resolved;
}
private async _dispatchLocal(params: MemoryToolParams, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
try {
switch (params.command) {
case 'view':
return this._localView(params.path, params.view_range, scope, sessionResource);
case 'create':
return this._localCreate(params, scope, sessionResource);
case 'str_replace':
return this._localStrReplace(params, scope, sessionResource);
case 'insert':
return this._localInsert(params, scope, sessionResource);
case 'delete':
return this._localDelete(params.path, scope, sessionResource);
case 'rename':
return this._localRename(params, scope, sessionResource);
default:
return { text: `Error: Unknown command '${(params as MemoryToolParams).command}'.`, outcome: 'error' };
}
} catch (error) {
this.logService.error('[MemoryTool] Local operation error:', error);
return { text: `Error: ${error.message}`, outcome: 'error' };
}
}
private async _localView(path: string, viewRange?: [number, number], scope: MemoryScope = 'user', sessionResource?: string): Promise<MemoryToolResult> {
// When viewing the top-level /memories/ directory with user scope, merge user + session contents
if (scope === 'user' && isMemoriesRoot(path)) {
return this._localViewMergedRoot(path, sessionResource);
}
const uri = this._resolveUri(path, scope, sessionResource);
if (scope === 'session') {
this.memoryCleanupService.markAccessed(uri);
}
let fileStat: vscode.FileStat;
try {
fileStat = await this.fileSystemService.stat(uri);
} catch {
this.logService.debug(`[MemoryTool] Failed to stat ${path}`);
if (isMemoriesRoot(path)) {
return { text: 'No memories found.', outcome: 'notFound' };
}
return { text: `No memories found in ${path}.`, outcome: 'notFound' };
}
if (fileStat.type === FileType.Directory) {
return { text: await this._listDirectory(path, uri), outcome: 'success' };
}
// Read file contents with line numbers
const content = await this.fileSystemService.readFile(uri);
const text = new TextDecoder().decode(content);
this.logService.debug(`[MemoryTool] Viewed memory file: ${path}`);
if (viewRange) {
const lines = text.split('\n');
const [start, end] = viewRange;
if (start < 1 || start > lines.length) {
return { text: `Error: Invalid view_range: start line ${start} is out of range [1, ${lines.length}].`, outcome: 'error' };
}
if (end < start || end > lines.length) {
return { text: `Error: Invalid view_range: end line ${end} is out of range [${start}, ${lines.length}].`, outcome: 'error' };
}
const sliced = lines.slice(start - 1, end);
const numbered = sliced.map((line, i) => `${formatLineNumber(start + i)}\t${line}`);
return { text: `Here's the content of ${path} (lines ${start}-${end}) with line numbers:\n${numbered.join('\n')}`, outcome: 'success' };
}
return { text: formatFileContent(path, text), outcome: 'success' };
}
private async _localViewMergedRoot(path: string, sessionResource?: string): Promise<MemoryToolResult> {
const lines: string[] = [];
let hasContent = false;
// List user-scoped files
try {
const userUri = this._resolveUri('/memories/', 'user');
const userEntries = await this.fileSystemService.readDirectory(userUri);
for (const [name, type] of userEntries) {
if (name.startsWith('.')) {
continue;
}
hasContent = true;
if (type === FileType.Directory) {
lines.push(`/memories/${name}/`);
} else {
lines.push(`/memories/${name}`);
}
}
} catch {
// User storage may not exist yet
}
// List current session's files under session/
if (sessionResource) {
try {
const sessionUri = this._resolveUri('/memories/session/', 'session', sessionResource);
const sessionEntries = await this.fileSystemService.readDirectory(sessionUri);
const sessionFiles: string[] = [];
for (const [name, type] of sessionEntries) {
if (name.startsWith('.')) {
continue;
}
if (type === FileType.File) {
sessionFiles.push(`/memories/session/${name}`);
}
}
// Add session/ header if there are files
if (sessionFiles.length > 0) {
hasContent = true;
lines.push('/memories/session/');
lines.push(...sessionFiles);
} else {
// Add session/ entry even if empty, to show it exists
lines.push('/memories/session/');
}
} catch {
// Session storage may not exist yet, but still mention it
lines.push('/memories/session/');
}
} else {
// No session resource, still mention session directory exists
lines.push('/memories/session/');
}
// List local repo memory files under repo/ (only when CAPI is not enabled)
const capiEnabled = isCopilotMemoryConfigEnabled(this.authenticationService, this.configurationService, this.experimentationService);
if (!capiEnabled) {
try {
const repoUri = this._resolveUri('/memories/repo/', 'repo');
const repoEntries = await this.fileSystemService.readDirectory(repoUri);
const repoFiles: string[] = [];
for (const [name, type] of repoEntries) {
if (name.startsWith('.')) {
continue;
}
if (type === FileType.Directory) {
repoFiles.push(`/memories/repo/${name}/`);
} else {
repoFiles.push(`/memories/repo/${name}`);
}
}
if (repoFiles.length > 0) {
hasContent = true;
lines.push('/memories/repo/');
lines.push(...repoFiles);
} else {
lines.push('/memories/repo/');
}
} catch {
// Repo storage may not exist yet, but still mention it
lines.push('/memories/repo/');
}
}
if (!hasContent) {
return { text: 'No memories found.', outcome: 'notFound' };
}
return { text: `Here are the files and directories in ${path}:\n${lines.join('\n')}`, outcome: 'success' };
}
private async _listDirectory(path: string, uri: URI, maxDepth: number = 2, currentDepth: number = 0): Promise<string> {
if (currentDepth >= maxDepth) {
return '';
}
const entries = await this.fileSystemService.readDirectory(uri);
const lines: string[] = [];
// Sort: directories first, then files. Exclude hidden items and the repo directory (CAPI-backed).
const sorted = entries
.filter(([name]) => !name.startsWith('.') && name !== 'repo')
.sort(([, a], [, b]) => {
if (a === FileType.Directory && b !== FileType.Directory) {
return -1;
}
if (a !== FileType.Directory && b === FileType.Directory) {
return 1;
}
return 0;
});
for (const [name, type] of sorted) {
const childUri = URI.joinPath(uri, name);
const childPath = path.endsWith('/') ? `${path}${name}` : `${path}/${name}`;
const prefix = ' '.repeat(currentDepth);
if (type === FileType.Directory) {
lines.push(`${prefix}${name}/`);
const subLines = await this._listDirectory(childPath, childUri, maxDepth, currentDepth + 1);
if (subLines) {
lines.push(subLines);
}
} else {
try {
const stat = await this.fileSystemService.stat(childUri);
lines.push(`${prefix}${stat.size}\t${childPath}`);
} catch {
lines.push(`${prefix}${name}`);
}
}
}
if (currentDepth === 0) {
return `Here are the files and directories up to 2 levels deep in ${path}, excluding hidden items:\n${lines.join('\n')}`;
}
return lines.join('\n');
}
private async _localCreate(params: ICreateParams, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
const uri = this._resolveUri(params.path, scope, sessionResource);
// Check if file exists
try {
await this.fileSystemService.stat(uri);
this.logService.debug(`[MemoryTool] Create failed - file already exists: ${params.path}`);
return { text: `Error: File ${params.path} already exists`, outcome: 'error' };
} catch {
// File doesn't exist — good
}
try {
// Ensure parent directory exists
const parentUri = URI.joinPath(uri, '..');
await createDirectoryIfNotExists(this.fileSystemService, parentUri);
const content = new TextEncoder().encode(params.file_text);
await this.fileSystemService.writeFile(uri, content);
if (scope === 'session') {
this.memoryCleanupService.markAccessed(uri);
}
this.logService.debug(`[MemoryTool] Created memory file: ${params.path}`);
return { text: `File created successfully at: ${params.path}`, outcome: 'success' };
} catch (error) {
this.logService.error(`[MemoryTool] Failed to create file ${params.path}:`, error);
throw error;
}
}
private async _localStrReplace(params: IStrReplaceParams, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
const uri = this._resolveUri(params.path, scope, sessionResource);
if (scope === 'session') {
this.memoryCleanupService.markAccessed(uri);
}
let content: string;
try {
const buffer = await this.fileSystemService.readFile(uri);
content = new TextDecoder().decode(buffer);
} catch {
this.logService.debug(`[MemoryTool] str_replace failed - file not found: ${params.path}`);
return { text: `The path ${params.path} does not exist. Please provide a valid path.`, outcome: 'notFound' };
}
const occurrences: number[] = [];
let searchStart = 0;
while (true) {
const idx = content.indexOf(params.old_str, searchStart);
if (idx === -1) {
break;
}
const lineNumber = content.substring(0, idx).split('\n').length;
occurrences.push(lineNumber);
searchStart = idx + 1;
}
if (occurrences.length === 0) {
this.logService.debug(`[MemoryTool] str_replace failed - pattern not found in ${params.path}`);
return { text: `No replacement was performed, old_str \`${params.old_str}\` did not appear verbatim in ${params.path}.`, outcome: 'error' };
}
if (occurrences.length > 1) {
this.logService.debug(`[MemoryTool] str_replace failed - multiple matches in ${params.path}`);
return { text: `No replacement was performed. Multiple occurrences of old_str \`${params.old_str}\` in lines: ${occurrences.join(', ')}. Please ensure it is unique.`, outcome: 'error' };
}
const newContent = content.replace(params.old_str, params.new_str);
await this.fileSystemService.writeFile(uri, new TextEncoder().encode(newContent));
this.logService.debug(`[MemoryTool] Updated memory file: ${params.path}`);
return { text: makeSnippet(newContent, occurrences[0], params.path), outcome: 'success' };
}
private async _localInsert(params: IInsertParams, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
const uri = this._resolveUri(params.path, scope, sessionResource);
if (scope === 'session') {
this.memoryCleanupService.markAccessed(uri);
}
// The model may send `new_str` instead of `insert_text`
const insertText = params.insert_text ?? params.new_str;
if (!insertText) {
this.logService.debug(`[MemoryTool] insert failed - missing insert_text for ${params.path}`);
return { text: 'Error: Missing required insert_text parameter for insert.', outcome: 'error' };
}
let content: string;
try {
const buffer = await this.fileSystemService.readFile(uri);
content = new TextDecoder().decode(buffer);
} catch {
this.logService.debug(`[MemoryTool] insert failed - file not found: ${params.path}`);
return { text: `Error: The path ${params.path} does not exist`, outcome: 'notFound' };
}
const lines = content.split('\n');
const nLines = lines.length;
if (params.insert_line < 0 || params.insert_line > nLines) {
this.logService.debug(`[MemoryTool] insert failed - invalid line number ${params.insert_line} for file with ${nLines} lines`);
return { text: `Error: Invalid \`insert_line\` parameter: ${params.insert_line}. It should be within the range of lines of the file: [0, ${nLines}].`, outcome: 'error' };
}
const newLines = insertText.split('\n');
lines.splice(params.insert_line, 0, ...newLines);
const newContent = lines.join('\n');
await this.fileSystemService.writeFile(uri, new TextEncoder().encode(newContent));
this.logService.debug(`[MemoryTool] Inserted into memory file: ${params.path}`);
return { text: makeSnippet(newContent, params.insert_line + 1, params.path), outcome: 'success' };
}
private async _localDelete(path: string, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
const uri = this._resolveUri(path, scope, sessionResource);
try {
await this.fileSystemService.stat(uri);
} catch {
this.logService.debug(`[MemoryTool] delete failed - path not found: ${path}`);
return { text: `Error: The path ${path} does not exist`, outcome: 'notFound' };
}
await this.fileSystemService.delete(uri, { recursive: true });
this.logService.debug(`[MemoryTool] Deleted memory file: ${path}`);
return { text: `Successfully deleted ${path}`, outcome: 'success' };
}
private async _localRename(params: IRenameParams, scope: MemoryScope, sessionResource?: string): Promise<MemoryToolResult> {
// The model may send `path` instead of `old_path`
const oldPath = params.old_path ?? params.path;
if (!oldPath) {
this.logService.debug(`[MemoryTool] rename failed - missing old_path`);
return { text: 'Error: Missing required old_path parameter for rename.', outcome: 'error' };
}
const newPathError = validatePath(params.new_path);
if (newPathError) {
return { text: newPathError, outcome: 'error' };
}
// Prevent renaming across different scopes
const newScope: MemoryScope = isRepoPath(params.new_path) ? 'repo' : isSessionPath(params.new_path) ? 'session' : 'user';
if (scope !== newScope) {
return { text: 'Error: Cannot rename across different memory scopes.', outcome: 'error' };
}
const srcUri = this._resolveUri(oldPath, scope, sessionResource);
const destUri = this._resolveUri(params.new_path, scope, sessionResource);
try {
await this.fileSystemService.stat(srcUri);
} catch {
this.logService.debug(`[MemoryTool] rename failed - source not found: ${oldPath}`);
return { text: `Error: The path ${oldPath} does not exist`, outcome: 'notFound' };
}
try {
await this.fileSystemService.stat(destUri);
this.logService.debug(`[MemoryTool] rename failed - destination exists: ${params.new_path}`);
return { text: `Error: The destination ${params.new_path} already exists`, outcome: 'error' };
} catch {
// Destination doesn't exist — good
}
// Ensure parent directory of destination exists
const destParent = URI.joinPath(destUri, '..');
await createDirectoryIfNotExists(this.fileSystemService, destParent);
await this.fileSystemService.rename(srcUri, destUri);
if (scope === 'session') {
this.memoryCleanupService.markAccessed(destUri);
}
this.logService.debug(`[MemoryTool] Renamed memory file: ${oldPath} → ${params.new_path}`);
return { text: `Successfully renamed ${oldPath} to ${params.new_path}`, outcome: 'success' };
}
private _sendLocalTelemetry(command: string, scope: MemoryScope, toolOutcome: MemoryToolOutcome, requestId?: string, model?: vscode.LanguageModelChat): void {
/* __GDPR__
"memoryToolInvoked" : {
"owner": "digitarald",
"comment": "Tracks memory tool invocations for local user, session, and repo scopes",
"command": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The memory command executed (view, create, str_replace, insert, delete, rename)" },
"scope": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The memory scope: user, session, or repo" },
"toolOutcome": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Normalized outcome: success, error, notFound, notEnabled" },
"requestId": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The id of the current request turn" },
"model": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The model that invoked the tool" }
}
*/
this.telemetryService.sendMSFTTelemetryEvent('memoryToolInvoked', {
command,
scope,
toolOutcome,
requestId,
model: model?.id,
});
}
private _sendRepoTelemetry(command: string, toolOutcome: MemoryToolOutcome, requestId?: string, model?: vscode.LanguageModelChat): void {
/* __GDPR__
"memoryRepoToolInvoked" : {
"owner": "digitarald",
"comment": "Tracks repository memory tool invocations",
"command": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The memory command executed" },
"toolOutcome": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Normalized outcome: success, error, notFound, notEnabled" },
"requestId": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The id of the current request turn" },
"model": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The model that invoked the tool" }
}
*/
this.telemetryService.sendMSFTTelemetryEvent('memoryRepoToolInvoked', {
command,
toolOutcome,
requestId,
model: model?.id,
});
}
}
ToolRegistry.registerTool(MemoryTool);