feat: implement AI schedule app with multi-device sync

- Qt 6 desktop client with day/month/year views, AI scheduling, and TOML settings\n- Go sync server with SQLite change stream, WebSocket notifications, and Docker deployment\n- Client outbox/LWW sync, offline retry, and dual-client integration tests\n- Linux fcitx5 setup, validation reports, and Windows MinGW preset
This commit is contained in:
Misaki
2026-10-26 13:58:45 +08:00
commit 5be7f7f04b
151 changed files with 38485 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
add_executable(ai_regression ai_regression.cpp)
target_link_libraries(ai_regression PRIVATE schedule_ai Qt6::Core)
set_target_properties(ai_regression PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
+462
View File
@@ -0,0 +1,462 @@
// 手动运行的本地模型回归工具(不属于 CTest)。
// 使用当前 AppSettings 中的 AI 配置,针对临时数据库运行真实模型。
//
// 用法:
// ./ai_regression --limit 5
// ./ai_regression --timeout 180 --out docs/validation/ai-regression-real.md
#include <QCoreApplication>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QFile>
#include <QTextStream>
#include <QTemporaryDir>
#include <QTimer>
#include <cstdio>
#include "ai/AIService.h"
#include "core/DateTimeUtils.h"
#include "services/ScheduleService.h"
#include "storage/SqliteStore.h"
#include "utils/AppSettings.h"
namespace
{
struct Case
{
int id = 0;
QString prompt;
QString expect; // create/update/delete/chat/ripple/split
QString titleNeedle;
int seed = 0; // 0 base tasks, 1 busy blocks for split
};
struct Outcome
{
bool ok = false;
QString detail;
bool timeout = false;
bool failed = false;
int creates = 0;
int updates = 0;
int deletes = 0;
int splitCreates = 0;
bool proposal = false;
QString reply;
QString error;
};
QVector<Case> cases()
{
return {
{1, QStringLiteral("明天下午写周报 2 小时"), QStringLiteral("create"), QStringLiteral("周报"), 0},
{2, QStringLiteral("今天 23:00 整理资料 30 分钟"), QStringLiteral("create"), QStringLiteral("资料"), 0},
{3, QStringLiteral("明天上午 10 点开 1 小时团队会议"), QStringLiteral("create"), QStringLiteral("会议"), 0},
{4, QStringLiteral("后天下午 3 点做一次 45 分钟复盘"), QStringLiteral("create"), QStringLiteral("复盘"), 0},
{5, QStringLiteral("这周找一个小时阅读"), QStringLiteral("create"), QStringLiteral("阅读"), 0},
{6, QStringLiteral("明天早上 8 点开始跑步 30 分钟"), QStringLiteral("create"), QStringLiteral("跑步"), 0},
{7, QStringLiteral("周五之前完成季度总结,预计 3 小时"), QStringLiteral("create"), QStringLiteral("总结"), 0},
{8, QStringLiteral("明天中午 12 点吃饭 1 小时"), QStringLiteral("create"), QStringLiteral("吃饭"), 0},
{9, QStringLiteral("今晚 9 点做 20 分钟拉伸"), QStringLiteral("create"), QStringLiteral("拉伸"), 0},
{10, QStringLiteral("明天下午 4 点准备 1.5 小时的产品演示"), QStringLiteral("create"), QStringLiteral("演示"), 0},
{11, QStringLiteral("明天上午安排两个任务:写周报 1 小时,整理邮件 30 分钟"), QStringLiteral("create"), QStringLiteral("周报"), 0},
{12, QStringLiteral("明天下午做三件事:写文档 2 小时、开评审会 1 小时、回复消息 30 分钟"), QStringLiteral("create"), QStringLiteral("文档"), 0},
{13, QStringLiteral("这周完成代码审查和单元测试,各 2 小时"), QStringLiteral("create"), QStringLiteral("代码"), 0},
{14, QStringLiteral("明天上午 9 点和 10 点各安排一个 30 分钟会议"), QStringLiteral("create"), QStringLiteral("会议"), 0},
{15, QStringLiteral("后天安排健身 1 小时和阅读 1 小时"), QStringLiteral("create"), QStringLiteral("健身"), 0},
{16, QStringLiteral("把写周报改到明天下午 2 点"), QStringLiteral("update"), QStringLiteral("周报"), 1},
{17, QStringLiteral("把团队同步延后 1 小时"), QStringLiteral("update"), QStringLiteral("团队"), 1},
{18, QStringLiteral("把健身改到明天上午 10 点"), QStringLiteral("update"), QStringLiteral("健身"), 1},
{19, QStringLiteral("写周报改成 90 分钟,时间不变"), QStringLiteral("update"), QStringLiteral("周报"), 1},
{20, QStringLiteral("把团队同步改名为项目同步"), QStringLiteral("update"), QStringLiteral("团队"), 1},
{21, QStringLiteral("把写周报改到后天上午 9 点,时长 2 小时"), QStringLiteral("update"), QStringLiteral("周报"), 1},
{22, QStringLiteral("把健身提前到明天 8 点"), QStringLiteral("update"), QStringLiteral("健身"), 1},
{23, QStringLiteral("把写周报和团队同步都挪到下午"), QStringLiteral("update"), QStringLiteral("周报"), 1},
{24, QStringLiteral("把团队同步的标题改成每日站会"), QStringLiteral("update"), QStringLiteral("团队"), 1},
{25, QStringLiteral("把健身的时长改成 45 分钟"), QStringLiteral("update"), QStringLiteral("健身"), 1},
{26, QStringLiteral("删掉团队同步"), QStringLiteral("delete"), QStringLiteral("团队"), 1},
{27, QStringLiteral("删除明天的健身"), QStringLiteral("delete"), QStringLiteral("健身"), 1},
{28, QStringLiteral("取消写周报"), QStringLiteral("delete"), QStringLiteral("周报"), 1},
{29, QStringLiteral("把团队同步删掉,后天不用开会"), QStringLiteral("delete"), QStringLiteral("团队"), 1},
{30, QStringLiteral("删除明天所有会议"), QStringLiteral("delete"), QString(), 1},
{31, QStringLiteral("明天有什么安排?"), QStringLiteral("chat"), QString(), 1},
{32, QStringLiteral("我这周还有多少空闲时间?"), QStringLiteral("chat"), QString(), 1},
{33, QStringLiteral("帮我看看明天下午有没有空"), QStringLiteral("chat"), QString(), 1},
{34, QStringLiteral("现在几点?今天还有什么日程?"), QStringLiteral("chat"), QString(), 1},
{35, QStringLiteral("写周报需要多久?"), QStringLiteral("chat"), QString(), 1},
{36, QStringLiteral("明天下午 3 点插入一个 1 小时的电话会议"), QStringLiteral("ripple"), QStringLiteral("电话"), 1},
{37, QStringLiteral("明天上午 9 点插入 30 分钟站会"), QStringLiteral("ripple"), QStringLiteral("站会"), 1},
{38, QStringLiteral("明天下午 3 点安排写文档 2 小时"), QStringLiteral("ripple"), QStringLiteral("文档"), 1},
{39, QStringLiteral("明天上午 11 点插入一个客户沟通 1 小时"), QStringLiteral("ripple"), QStringLiteral("沟通"), 1},
{40, QStringLiteral("后天下午 3 点插入一个 1 小时复盘"), QStringLiteral("create"), QStringLiteral("复盘"), 1},
{41, QStringLiteral("明天之前找时间做两小时长任务,可以拆开"), QStringLiteral("create"), QStringLiteral("长任务"), 2},
{42, QStringLiteral("明天之前找时间做 3 小时深度工作,可以拆开"), QStringLiteral("create"), QStringLiteral("深度"), 2},
{43, QStringLiteral("明天完成 2 小时报告和 2 小时代码,可以拆分"), QStringLiteral("create"), QStringLiteral("报告"), 2},
{44, QStringLiteral("明天之前做一次 4 小时的项目冲刺,允许拆开"), QStringLiteral("create"), QStringLiteral("冲刺"), 2},
{45, QStringLiteral("明天安排 3 小时的整理工作,可以分两次"), QStringLiteral("create"), QStringLiteral("整理"), 2},
{46, QStringLiteral("昨天下午写周报 1 小时"), QStringLiteral("create"), QStringLiteral("周报"), 0},
{47, QStringLiteral("帮我安排一下"), QStringLiteral("chat"), QString(), 0},
{48, QStringLiteral("明天开会"), QStringLiteral("create"), QStringLiteral("开会"), 0},
{49, QStringLiteral("下周一上午 10 点准备周报,时长 1 小时"), QStringLiteral("create"), QStringLiteral("周报"), 0},
{50, QStringLiteral("这周末安排一次 2 小时的复盘"), QStringLiteral("create"), QStringLiteral("复盘"), 0},
};
}
bool seedDatabase(ScheduleService& service, int mode, QString* error)
{
const QDate today = QDate::currentDate();
const QDate tomorrow = today.addDays(1);
if (mode == 0)
{
return true;
}
if (mode == 2)
{
for (const QDate& day : {today, tomorrow})
{
if (!service.createBlock(QStringLiteral("占用一"),
QDateTime(day, QTime(8, 0), Qt::LocalTime),
QDateTime(day, QTime(12, 0), Qt::LocalTime),
error) ||
!service.createBlock(QStringLiteral("占用二"),
QDateTime(day, QTime(13, 0), Qt::LocalTime),
QDateTime(day, QTime(17, 0), Qt::LocalTime),
error) ||
!service.createBlock(QStringLiteral("占用三"),
QDateTime(day, QTime(18, 0), Qt::LocalTime),
QDateTime(day, QTime(22, 0), Qt::LocalTime),
error))
{
return false;
}
}
return true;
}
return service.createBlock(QStringLiteral("写周报"),
QDateTime(tomorrow, QTime(9, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(10, 0), Qt::LocalTime),
error) &&
service.createBlock(QStringLiteral("团队同步"),
QDateTime(tomorrow, QTime(11, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(12, 0), Qt::LocalTime),
error) &&
service.createBlock(QStringLiteral("健身"),
QDateTime(tomorrow, QTime(15, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(16, 0), Qt::LocalTime),
error) &&
service.createBlock(QStringLiteral("项目会议"),
QDateTime(tomorrow, QTime(13, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(14, 0), Qt::LocalTime),
error);
}
bool containsTitle(const ScheduleProposal& proposal, const QString& needle, bool* split)
{
for (const ProposedScheduleEntry& entry : proposal.entries)
{
if (entry.operation == ProposedOperation::Create && entry.segments.size() > 1)
{
*split = true;
}
const QString haystack = entry.title + QLatin1Char(' ') + entry.oldTitle;
if (needle.isEmpty() || haystack.contains(needle, Qt::CaseInsensitive))
{
return true;
}
}
return false;
}
Outcome evaluate(const Case& testCase, const ScheduleProposal& proposal, bool hasProposal)
{
Outcome outcome;
outcome.proposal = hasProposal;
if (testCase.expect == QStringLiteral("chat"))
{
outcome.ok = !hasProposal;
outcome.detail = hasProposal ? QStringLiteral("unexpected proposal") : QString();
return outcome;
}
if (!hasProposal)
{
outcome.detail = QStringLiteral("no proposal");
return outcome;
}
bool split = false;
bool titleOk = containsTitle(proposal, testCase.titleNeedle, &split);
for (const ProposedScheduleEntry& entry : proposal.entries)
{
if (entry.operation == ProposedOperation::Create)
{
++outcome.creates;
if (entry.segments.size() > 1)
{
++outcome.splitCreates;
}
}
else if (entry.operation == ProposedOperation::Update)
{
++outcome.updates;
}
else if (entry.operation == ProposedOperation::Delete)
{
++outcome.deletes;
}
}
if (testCase.expect == QStringLiteral("create"))
{
outcome.ok = outcome.creates > 0 && titleOk;
}
else if (testCase.expect == QStringLiteral("update"))
{
outcome.ok = outcome.updates > 0 && titleOk;
}
else if (testCase.expect == QStringLiteral("delete"))
{
outcome.ok = outcome.deletes > 0 && titleOk;
}
else if (testCase.expect == QStringLiteral("ripple"))
{
outcome.ok = outcome.creates > 0 && outcome.updates > 0 && titleOk;
}
else if (testCase.expect == QStringLiteral("split"))
{
// 模型可以设置 splittable 让引擎拆分,也可以自行拆成同一任务的多条 create。
outcome.ok = outcome.creates > 0 && titleOk &&
(outcome.splitCreates > 0 || outcome.creates >= 2);
}
if (!outcome.ok)
{
outcome.detail = titleOk ? QStringLiteral("operation mismatch") : QStringLiteral("title mismatch");
}
return outcome;
}
QString opsSummary(const Outcome& outcome)
{
return QStringLiteral("C%1 U%2 D%3 S%4")
.arg(outcome.creates)
.arg(outcome.updates)
.arg(outcome.deletes)
.arg(outcome.splitCreates);
}
} // namespace
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setOrganizationName(QStringLiteral("Misaki"));
QCoreApplication::setApplicationName(QStringLiteral("Schedule"));
int limit = -1;
int startIndex = 0;
int timeoutSeconds = 180;
QString outPath;
for (int i = 1; i < argc; ++i)
{
const QString arg = QString::fromLocal8Bit(argv[i]);
if (arg == QStringLiteral("--limit") && i + 1 < argc)
{
limit = QString::fromLocal8Bit(argv[++i]).toInt();
}
else if (arg == QStringLiteral("--start") && i + 1 < argc)
{
startIndex = std::max(0, QString::fromLocal8Bit(argv[++i]).toInt() - 1);
}
else if (arg == QStringLiteral("--timeout") && i + 1 < argc)
{
timeoutSeconds = QString::fromLocal8Bit(argv[++i]).toInt();
}
else if (arg == QStringLiteral("--out") && i + 1 < argc)
{
outPath = QString::fromLocal8Bit(argv[++i]);
}
}
AppSettings settings;
const AiConfig config = settings.aiConfig();
std::printf("AI regression: base_url=%s model=%s\n",
qPrintable(config.baseUrl),
qPrintable(config.model));
std::printf("seed=base (update/delete/ripple) or busy (split)\n\n");
QVector<Case> allCases = cases();
if (startIndex >= allCases.size())
{
std::fprintf(stderr, "start index out of range\n");
return 1;
}
if (startIndex > 0 || (limit > 0 && limit < allCases.size()))
{
const int count = limit > 0 ? limit : allCases.size() - startIndex;
allCases = allCases.mid(startIndex, count);
}
QFile report(outPath);
QTextStream reportStream;
if (!outPath.isEmpty())
{
if (!report.open(QIODevice::WriteOnly | QIODevice::Text))
{
std::fprintf(stderr, "Cannot open report file: %s\n", qPrintable(outPath));
return 1;
}
reportStream.setDevice(&report);
reportStream << "# Real local model regression\n\n";
reportStream << "- base_url: `" << config.baseUrl << "`\n";
reportStream << "- model: `" << config.model << "`\n";
reportStream << "- timeout: " << timeoutSeconds << "s/case\n\n";
reportStream << "| # | result | ops | seconds | prompt | detail |\n";
reportStream << "|---|---|---|---|---|---|\n";
}
int passed = 0;
int failed = 0;
int timedOut = 0;
int errors = 0;
QVector<double> elapsedSeconds;
for (int index = 0; index < allCases.size(); ++index)
{
const Case& testCase = allCases.at(index);
QTemporaryDir dir;
if (!dir.isValid())
{
std::fprintf(stderr, "Cannot create temp dir\n");
return 1;
}
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
if (!store.open(&error))
{
std::fprintf(stderr, "Cannot open store: %s\n", qPrintable(error));
return 1;
}
ScheduleService service(store, QStringLiteral("regression-device"));
if (!seedDatabase(service, testCase.seed, &error))
{
std::fprintf(stderr, "Cannot seed case %d: %s\n", testCase.id, qPrintable(error));
return 1;
}
AIService ai(service, store, settings);
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
Outcome outcome;
bool done = false;
bool hasProposal = false;
ScheduleProposal proposal;
QObject::connect(&ai, &AIService::streamDelta, [&outcome](const QString& delta) {
outcome.reply += delta;
});
QObject::connect(&ai, &AIService::proposalReady, [&](const ScheduleProposal& value) {
hasProposal = true;
proposal = value;
});
QObject::connect(&ai, &AIService::requestFinished, [&]() {
done = true;
loop.quit();
});
QObject::connect(&ai, &AIService::requestFailed, [&](const QString& message) {
outcome.failed = true;
outcome.error = message;
done = true;
loop.quit();
});
QObject::connect(&timeoutTimer, &QTimer::timeout, [&]() {
outcome.timeout = true;
done = true;
loop.quit();
});
QElapsedTimer timer;
timer.start();
timeoutTimer.start(timeoutSeconds * 1000);
ai.request(testCase.prompt);
if (!done)
{
loop.exec();
}
const double elapsed = timer.elapsed() / 1000.0;
elapsedSeconds.append(elapsed);
timeoutTimer.stop();
if (outcome.timeout)
{
++timedOut;
outcome.detail = QStringLiteral("timeout");
}
else if (outcome.failed)
{
++errors;
outcome.detail = outcome.error;
}
else
{
outcome = [&]() {
Outcome evaluated = evaluate(testCase, proposal, hasProposal);
evaluated.reply = outcome.reply;
evaluated.error = outcome.error;
return evaluated;
}();
}
if (outcome.ok)
{
++passed;
}
else
{
++failed;
}
const QString result = outcome.ok ? QStringLiteral("PASS")
: (outcome.timeout ? QStringLiteral("TIMEOUT")
: (outcome.failed ? QStringLiteral("ERROR")
: QStringLiteral("FAIL")));
std::printf("[%02d] %-7s %5.1fs %s %s%s%s\n",
testCase.id,
qPrintable(result),
elapsed,
qPrintable(opsSummary(outcome)),
qPrintable(testCase.prompt),
outcome.detail.isEmpty() ? "" : " | ",
outcome.detail.isEmpty() ? "" : qPrintable(outcome.detail));
std::fflush(stdout);
if (reportStream.device())
{
reportStream << "| " << testCase.id << " | " << result << " | " << opsSummary(outcome)
<< " | " << QString::number(elapsed, 'f', 1) << " | "
<< testCase.prompt << " | " << outcome.detail << " |\n";
reportStream.flush();
}
}
double total = 0;
for (double value : elapsedSeconds)
{
total += value;
}
const double avg = elapsedSeconds.isEmpty() ? 0 : total / elapsedSeconds.size();
std::printf("\nSummary: passed=%d failed=%d timeout=%d error=%d total=%lld avg=%.2fs\n",
passed,
failed,
timedOut,
errors,
static_cast<long long>(allCases.size()),
avg);
if (reportStream.device())
{
reportStream << "\n## Summary\n\n";
reportStream << "- passed: " << passed << "\n";
reportStream << "- failed: " << failed << "\n";
reportStream << "- timeout: " << timedOut << "\n";
reportStream << "- error: " << errors << "\n";
reportStream << "- average latency: " << QString::number(avg, 'f', 2) << "s\n";
report.flush();
}
return passed == allCases.size() ? 0 : 2;
}