1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * SPDX-FileCopyrightText: 2026 by Srirupa Datta <srirupa dot sps at gmail dot com>
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "searchmockbackend.h"

// Qt includes

#include <QTimer>

namespace Digikam
{

SearchMockBackend::SearchMockBackend(QObject* const parent)
    : SearchLanguageBackend(parent)
{

    addCannedResponse(QLatin1String("photos from Paris in 2023"),
        QLatin1String(R"({
            "constraints": [
                { "field": "place",     "op": "contains", "value": "Paris" },
                { "field": "daterange", "op": "between",  "value": "2023-01-01..2023-12-31" }
            ],
            "clarification": null
        })"));

    addCannedResponse(QLatin1String("landscape photos with red labels"),
        QLatin1String(R"({
            "constraints": [
                { "field": "tag",        "op": "contains", "value": "landscape" },
                { "field": "colorlabel", "op": "eq",       "value": "red" }
            ],
            "clarification": null
        })"));

    addCannedResponse(QLatin1String("best photos from last summer"),
        QLatin1String(R"({
            "constraints": [
                { "field": "daterange", "op": "between", "value": "2025-06-01..2025-08-31" }
            ],
            "clarification": {
                "message": "What does 'best' mean for you?",
                "choices": [ "Pick Label: Accepted", "Rating >= 4" ]
            }
        })"));
}

bool SearchMockBackend::loadModel(const QString& modelPath)<--- Shadow argument
{
    m_path   = modelPath;
    m_loaded = true;

    Q_EMIT signalModelLoaded(true);

    return true;
}

void SearchMockBackend::unloadModel()
{
    m_loaded = false;
}

bool SearchMockBackend::isModelLoaded() const
{
    return m_loaded;
}

QString SearchMockBackend::modelPath() const
{
    return m_path;
}

QString SearchMockBackend::backendName() const
{
    return QLatin1String("mock");
}

void SearchMockBackend::addCannedResponse(const QString& querySubstring,
                                          const QString& jsonOutput)
{
    m_canned.insert(querySubstring, jsonOutput);
}

void SearchMockBackend::slotRunInference(const QString& prompt)
{
    // Deliver asynchronously to mimic real backend behaviour and keep
    // signal/slot ordering identical to the llama.cpp backend.
    QTimer::singleShot(0, this, [this, prompt]()
        {
            for (auto it = m_canned.constBegin() ; it != m_canned.constEnd() ; ++it)
            {
                if (prompt.contains(it.key(), Qt::CaseInsensitive))
                {
                    Q_EMIT signalRawOutputReady(it.value());

                    return;
                }
            }

            // Unknown query: empty but valid schema (nothing understood).
            Q_EMIT signalRawOutputReady(QLatin1String(R"({ "constraints": [], "clarification": null })"));
        });
}

} // namespace Digikam

#include "moc_searchmockbackend.cpp"