List 列出符合条件的 Memory
(ctx context.Context, namespace string, filters ...Filter)
| 97 | |
| 98 | // List 列出符合条件的 Memory |
| 99 | func (s *InMemoryStore) List(ctx context.Context, namespace string, filters ...Filter) ([]*LogicMemory, error) { |
| 100 | s.mu.RLock() |
| 101 | defer s.mu.RUnlock() |
| 102 | |
| 103 | if s.closed { |
| 104 | return nil, ErrStoreClosed |
| 105 | } |
| 106 | |
| 107 | opts := ApplyFilters(filters...) |
| 108 | |
| 109 | var result []*LogicMemory |
| 110 | for _, memory := range s.memories { |
| 111 | // 过滤 namespace |
| 112 | if namespace != "" && memory.Namespace != namespace { |
| 113 | continue |
| 114 | } |
| 115 | |
| 116 | // 过滤类型 |
| 117 | if opts.Type != "" && memory.Type != opts.Type { |
| 118 | continue |
| 119 | } |
| 120 | |
| 121 | // 过滤作用域 |
| 122 | if opts.Scope != "" && memory.Scope != opts.Scope { |
| 123 | continue |
| 124 | } |
| 125 | |
| 126 | // 过滤置信度 |
| 127 | if memory.Provenance != nil && memory.Provenance.Confidence < opts.MinConfidence { |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | // 返回拷贝 |
| 132 | copied := *memory |
| 133 | result = append(result, &copied) |
| 134 | } |
| 135 | |
| 136 | // 排序 |
| 137 | sortMemories(result, opts.OrderBy) |
| 138 | |
| 139 | // 限制数量 |
| 140 | if opts.MaxResults > 0 && len(result) > opts.MaxResults { |
| 141 | result = result[:opts.MaxResults] |
| 142 | } |
| 143 | |
| 144 | return result, nil |
| 145 | } |
| 146 | |
| 147 | // SearchByType 按类型搜索 |
| 148 | func (s *InMemoryStore) SearchByType(ctx context.Context, namespace, memoryType string) ([]*LogicMemory, error) { |