AppIntents EntityPropertyQuery, how does "Filter Entity where" work?

When you correctly implement EntityPropertyQuery on an AppEntity, Shortcuts will expose a "Find Entity" action that calls into entities(matching:mode:sortedBy:limit:). This is demoed in the "Dive into App Intents" session and works as expected.

However, with this action, you can change the "All Entity" input to a list variable which changes the action text from "Find All Entity" to "Filter Entity where" still giving you the same filter, sort and limit options. This appears to work as expected too. But, what's unexpected is that this filter action does not appear to call any method on my AppEntity code. It doesn't call entities(matching:mode:sortedBy:limit:). One would think there would need to be a filter(entities:matching:mode:sortedBy:limit:) to implement this functionality. But Shortcut just seems to do it all on it's own. I'm mostly wondering, how is this even working?

Here's some example code:

import AppIntents

let books = [
  BookEntity(id: 0, title: "A Family Affair"),
  BookEntity(id: 1, title: "Atlas of the Heart"),
  BookEntity(id: 2, title: "Atomic Habits"),
  BookEntity(id: 3, title: "Memphis"),
  BookEntity(id: 4, title: "Run Rose Run"),
  BookEntity(id: 5, title: "The Maid"),
  BookEntity(id: 6, title: "The Match"),
  BookEntity(id: 7, title: "Where the Crawdads Sing"),
]

struct BookEntity: AppEntity, Identifiable {
  static var typeDisplayRepresentation: TypeDisplayRepresentation = "Book"
  var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "\(title)") }
  static var defaultQuery = BookQuery()

  var id: Int

  @Property(title: "Title")
  var title: String

  init(id: Int, title: String) {
    self.id = id
    self.title = title
  }
}

struct BookQuery: EntityQuery {
  func entities(for identifiers: [Int]) async throws -> [BookEntity] {
    return identifiers.map { id in books[id] }
  }
}

extension BookQuery: EntityPropertyQuery {
  static var properties = QueryProperties {
    Property(\BookEntity.$title) {
      EqualToComparator { str in { book in book.title == str } }
      ContainsComparator { str in { book in book.title.contains(str) } }
    }
  }

  static var sortingOptions = SortingOptions {
    SortableBy(\BookEntity.$title)
  }

  func entities(
    matching comparators: [(BookEntity) -> Bool],
    mode: ComparatorMode,
    sortedBy: [Sort<BookEntity>],
    limit: Int?
  ) async throws -> [BookEntity] {
    books.filter { book in comparators.allSatisfy { comparator in comparator(book) } }
  }
}

The example Shortcut first invokes entities(matching:mode:sortedBy:limit:) with comparators=[], sortedBy=[], limit=nil to fetch all Book entities. Next the filter step correctly applies the title contains filter but never calls entities(matching:mode:sortedBy:limit:) or even the body of the ContainsComparator. But the output is correctly filtered.

Late, but I ponder the same question also when I was implementing App Intent. My conclusion is that the shortcut app somehow knows how to perform the filtering and sorting by itself, hence the list of XXComparator and SortableBy that comes by default and there isn't any sample code that show us how to make custom versions of this.

This behavior is good in such that we get some functionality for free, but it is bad that we can't control the filtering mechanism.

In my app, for the "Find" action, I am able to perform a case insensitive search in my entities(matching) code, but when it appears in the "Filter" action, the search is always case sensitive and there doesn't seems like a way to control it.

What made it frustrating for me also is that, the UI of "Add Filter" in shortcuts app, can't seems to be reproduced by using normal Intent Parameter, and can only be shown as such when we use the EntityPropertyQuery!

AppIntents EntityPropertyQuery, how does "Filter Entity where" work?
 
 
Q