11/**
2- * Memory CRUD tools: remember, recall, recall_recent, search, list, list_tags, delete, move.
2+ * Memory CRUD tools: remember, recall, search, list, list_tags, delete, move.
33 */
44
55import fs from 'fs' ;
@@ -8,7 +8,7 @@ import type {
88 Ctx ,
99 RememberArgs ,
1010 RecallArgs ,
11- RecallRecentArgs ,
11+ ListArgs ,
1212 SearchArgs ,
1313 DeleteArgs ,
1414 MoveArgs ,
@@ -111,52 +111,6 @@ export function createMemoryTools(ctx: Ctx) {
111111 } ) ;
112112 }
113113
114- function handleRecallRecent ( args : RecallRecentArgs ) {
115- const n = typeof args . n === 'number' ? Math . max ( 1 , Math . floor ( args . n ) ) : 10 ;
116- const folderFilterRaw = typeof args . folder === 'string' ? args . folder : null ;
117- const folderFilter = folderFilterRaw ? sanitizeFolder ( folderFilterRaw ) : null ;
118- if ( folderFilterRaw && ! folderFilter ) {
119- return toolResult ( { items : [ ] , error : 'Invalid folder path' } , true ) ;
120- }
121- const tagFilter = typeof args . tag === 'string' ? args . tag : null ;
122-
123- const index = indexDao . getIndex ( ) ;
124- const items = [ ] ;
125- for ( const key of Object . keys ( index . index ) ) {
126- if ( ! key . endsWith ( '.md' ) ) continue ;
127- const value = index . index [ key ] ;
128- const folder = path . dirname ( key ) ;
129- const entryKey = path . basename ( key , '.md' ) ;
130-
131- if ( folderFilter && folder !== folderFilter ) continue ;
132- if ( tagFilter && ( ! Array . isArray ( value . tags ) || ! value . tags . includes ( tagFilter ) ) ) continue ;
133-
134- const filePath = path . join ( storeRoot , key ) ;
135- const stats = fileStats ( filePath ) ;
136- items . push ( {
137- key : entryKey ,
138- folder,
139- title : value . title || toTitle ( entryKey ) ,
140- tags : Array . isArray ( value . tags ) ? value . tags : [ ] ,
141- updatedAt : stats ? stats . mtime . toISOString ( ) : new Date ( ) . toISOString ( ) ,
142- preview : '' ,
143- } ) ;
144- }
145-
146- items . sort ( ( a , b ) => new Date ( b . updatedAt ) . getTime ( ) - new Date ( a . updatedAt ) . getTime ( ) ) ;
147-
148- return toolResult ( {
149- items : items . slice ( 0 , n ) . map ( ( item ) => {
150- const filePath = resolveFilePath ( item . folder , item . key ) ;
151- const parsed = safeParseFile ( filePath ) ;
152- return {
153- ...item ,
154- preview : ( parsed ?. body || '' ) . slice ( 0 , 400 ) ,
155- } ;
156- } ) ,
157- } ) ;
158- }
159-
160114 function handleSearch ( args : SearchArgs ) {
161115 const query = args . query ;
162116 if ( ! query || typeof query !== 'string' ) {
@@ -219,12 +173,14 @@ export function createMemoryTools(ctx: Ctx) {
219173 return toolResult ( { tags : Array . from ( tagSet ) . sort ( ) } ) ;
220174 }
221175
222- function handleList ( args : RecallRecentArgs ) {
176+ function handleList ( args : ListArgs ) {
223177 const folderFilterRaw = typeof args . folder === 'string' ? args . folder : null ;
224178 const folderFilter = folderFilterRaw ? sanitizeFolder ( folderFilterRaw ) : null ;
225179 if ( folderFilterRaw && ! folderFilter ) {
226180 return toolResult ( { items : [ ] , error : 'Invalid folder path' } , true ) ;
227181 }
182+ const tagFilter = typeof args . tag === 'string' ? args . tag : null ;
183+ const limit = typeof args . limit === 'number' ? Math . max ( 1 , Math . floor ( args . limit ) ) : null ;
228184 const index = indexDao . getIndex ( ) ;
229185
230186 const items = [ ] ;
@@ -233,6 +189,7 @@ export function createMemoryTools(ctx: Ctx) {
233189 const value = index . index [ key ] ;
234190 const folder = path . dirname ( key ) ;
235191 if ( folderFilter && folder !== folderFilter ) continue ;
192+ if ( tagFilter && ( ! Array . isArray ( value . tags ) || ! value . tags . includes ( tagFilter ) ) ) continue ;
236193
237194 const filePath = path . join ( storeRoot , key ) ;
238195 const stats = fileStats ( filePath ) ;
@@ -247,6 +204,20 @@ export function createMemoryTools(ctx: Ctx) {
247204 }
248205
249206 items . sort ( ( a , b ) => new Date ( b . updatedAt ) . getTime ( ) - new Date ( a . updatedAt ) . getTime ( ) ) ;
207+
208+ if ( limit !== null ) {
209+ return toolResult ( {
210+ items : items . slice ( 0 , limit ) . map ( ( item ) => {
211+ const filePath = resolveFilePath ( item . folder , item . key ) ;
212+ const parsed = safeParseFile ( filePath ) ;
213+ return {
214+ ...item ,
215+ preview : ( parsed ?. body || '' ) . slice ( 0 , 400 ) ,
216+ } ;
217+ } ) ,
218+ } ) ;
219+ }
220+
250221 return toolResult ( { items } ) ;
251222 }
252223
@@ -332,7 +303,6 @@ export function createMemoryTools(ctx: Ctx) {
332303 return {
333304 handleRemember,
334305 handleRecall,
335- handleRecallRecent,
336306 handleSearch,
337307 handleListTags,
338308 handleList,
0 commit comments