Siri enters loop of requesting parameter when running AppIntent

I want to add shortcut and Siri support using the new AppIntents framework. Running my intent using shortcuts or from spotlight works fine, as the touch based UI for the disambiguation is shown. However, when I ask Siri to perform this action, she gets into a loop of asking me the question to set the parameter.

My AppIntent is implemented as following:

struct StartSessionIntent: AppIntent {
    static var title: LocalizedStringResource = "start_recording"

    @Parameter(title: "activity", requestValueDialog: IntentDialog("which_activity"))
    var activity: ActivityEntity

    @MainActor
    func perform() async throws -> some IntentResult & ProvidesDialog {
        let activityToSelect: ActivityEntity = self.activity
        guard let selectedActivity = Activity[activityToSelect.name] else {
            return .result(dialog: "activity_not_found")
        }
        ...
        return .result(dialog: "recording_started \(selectedActivity.name.localized())")
    }
}

The ActivityEntity is implemented like this:

struct ActivityEntity: AppEntity {
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "activity")
    typealias DefaultQuery = MobilityActivityQuery
    static var defaultQuery: MobilityActivityQuery = MobilityActivityQuery()

    var id: String
    var name: String
    var icon: String

    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(self.name.localized())", image: .init(systemName: self.icon))
    }
}

struct MobilityActivityQuery: EntityQuery {
    func entities(for identifiers: [String]) async throws -> [ActivityEntity] {
        Activity.all()?.compactMap({ activity in
            identifiers.contains(where: { $0 == activity.name }) ? ActivityEntity(id: activity.name, name: activity.name, icon: activity.icon) : nil
        }) ?? []
    }

    func suggestedEntities() async throws -> [ActivityEntity] {
        Activity.all()?.compactMap({ activity in
            ActivityEntity(id: activity.name, name: activity.name, icon: activity.icon)
        }) ?? []
    }
}

Has anyone an idea what might be causing this and how I can fix this behavior? Thanks in advance

Post not yet marked as solved Up vote post of De Mo Down vote post of De Mo
825 views

Replies

Hi! The same issue happened to me before. The issue happens when we return an emtpy array for the function entities(for identifiers: [String]).

In my case, I forgot to add one missing entity which was additionally added in the suggestedEntities.

This may not be the cause of your issue, but I hope it can help you find some clues. 🙂

  • Oh! And I found another possible cause of this behavior.

    It could also happen if you somehow have inconsistent CoreData data.

    For example, I had one old codebase, when saving changes, instead of saving each changes, it wipes out the sql file underneath and create a new one and save the data from the memory. 🤪

Add a Comment

Did you found a solution? I have a similar problem: I selected a value for my AppEntity "category", then want to execute the shortcut and run into the loop. I diagnosed, that the parameter of my intent is still nil and stays so after selecting a value in the dialog. Also, I have a second parameter "categoryEntry", which depends on the selection of the "category" value. But this also fails, because of the internal nil value of the category :/