List 列出符合条件的 Memory
(ctx context.Context, namespace string, filters ...Filter)
| 229 | |
| 230 | // List 列出符合条件的 Memory |
| 231 | func (s *PostgreSQLStore) List(ctx context.Context, namespace string, filters ...Filter) ([]*LogicMemory, error) { |
| 232 | if s.closed { |
| 233 | return nil, ErrStoreClosed |
| 234 | } |
| 235 | |
| 236 | opts := ApplyFilters(filters...) |
| 237 | |
| 238 | // 构建查询 |
| 239 | query := fmt.Sprintf(` |
| 240 | SELECT id, namespace, scope, type, category, key, value, description, |
| 241 | source_type, confidence, sources, provenance_created_at, provenance_updated_at, provenance_version, |
| 242 | access_count, last_accessed, metadata, created_at, updated_at |
| 243 | FROM %s |
| 244 | WHERE 1=1 |
| 245 | `, s.tableName) |
| 246 | |
| 247 | args := []any{} |
| 248 | argIndex := 1 |
| 249 | |
| 250 | if namespace != "" { |
| 251 | query += fmt.Sprintf(" AND namespace = $%d", argIndex) |
| 252 | args = append(args, namespace) |
| 253 | argIndex++ |
| 254 | } |
| 255 | |
| 256 | if opts.Type != "" { |
| 257 | query += fmt.Sprintf(" AND type = $%d", argIndex) |
| 258 | args = append(args, opts.Type) |
| 259 | argIndex++ |
| 260 | } |
| 261 | |
| 262 | if opts.Scope != "" { |
| 263 | query += fmt.Sprintf(" AND scope = $%d", argIndex) |
| 264 | args = append(args, opts.Scope) |
| 265 | argIndex++ |
| 266 | } |
| 267 | |
| 268 | if opts.MinConfidence > 0 { |
| 269 | query += fmt.Sprintf(" AND confidence >= $%d", argIndex) |
| 270 | args = append(args, opts.MinConfidence) |
| 271 | // argIndex++ 不需要,后续没有使用 |
| 272 | } |
| 273 | |
| 274 | // 排序 |
| 275 | switch opts.OrderBy { |
| 276 | case OrderByConfidence: |
| 277 | query += " ORDER BY confidence DESC" |
| 278 | case OrderByLastAccessed: |
| 279 | query += " ORDER BY last_accessed DESC" |
| 280 | case OrderByCreatedAt: |
| 281 | query += " ORDER BY created_at DESC" |
| 282 | case OrderByAccessCount: |
| 283 | query += " ORDER BY access_count DESC" |
| 284 | default: |
| 285 | query += " ORDER BY confidence DESC" |
| 286 | } |
| 287 | |
| 288 | // 限制数量 |
no test coverage detected