@ai-setting/roy-agent-core 1.6.11 → 1.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/env/agent/index.js +2 -2
- package/dist/env/event-source/index.js +3 -3
- package/dist/env/index.js +10 -10
- package/dist/env/prompt/index.js +2 -2
- package/dist/env/task/delegate/index.js +3 -3
- package/dist/env/task/index.js +6 -6
- package/dist/env/task/plugins/index.js +2 -2
- package/dist/env/task/storage/index.js +2 -1
- package/dist/env/task/tools/operation/index.js +1 -1
- package/dist/index.js +11 -11
- package/dist/shared/@ai-setting/{roy-agent-core-m38q2azm.js → roy-agent-core-1bvv6s75.js} +3 -1
- package/dist/shared/@ai-setting/{roy-agent-core-wqe84f84.js → roy-agent-core-3bdpnmtg.js} +29 -0
- package/dist/shared/@ai-setting/{roy-agent-core-5w9a1eqa.js → roy-agent-core-7a3gf3tw.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-9abv4ewh.js +1122 -0
- package/dist/shared/@ai-setting/{roy-agent-core-0cftbg62.js → roy-agent-core-asp5p3zx.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-38kbfarg.js → roy-agent-core-cxdnzn17.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-3rs38pf7.js → roy-agent-core-dntkpwqq.js} +8 -8
- package/dist/shared/@ai-setting/{roy-agent-core-trdpar9f.js → roy-agent-core-ms5dp89r.js} +338 -20
- package/dist/shared/@ai-setting/{roy-agent-core-2ek596yd.js → roy-agent-core-q2bw3cwd.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-y5ymc4f8.js → roy-agent-core-q7rt3x8d.js} +45 -13
- package/dist/shared/@ai-setting/roy-agent-core-rn57psp0.js +396 -0
- package/package.json +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-9xby523m.js +0 -961
- package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +0 -213
|
@@ -1,961 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createLogger,
|
|
3
|
-
init_logger
|
|
4
|
-
} from "./roy-agent-core-vyygk314.js";
|
|
5
|
-
import {
|
|
6
|
-
__esm,
|
|
7
|
-
__export,
|
|
8
|
-
__require,
|
|
9
|
-
__toCommonJS
|
|
10
|
-
} from "./roy-agent-core-fs0mn2jk.js";
|
|
11
|
-
|
|
12
|
-
// src/env/task/storage/migrations/001_add_project_path_context.ts
|
|
13
|
-
var exports_001_add_project_path_context = {};
|
|
14
|
-
__export(exports_001_add_project_path_context, {
|
|
15
|
-
runMigration: () => runMigration
|
|
16
|
-
});
|
|
17
|
-
function runMigration(dbPath) {
|
|
18
|
-
console.log(`[Migration 001] Starting migration for: ${dbPath}`);
|
|
19
|
-
const db = createCompatibleDatabase(dbPath);
|
|
20
|
-
try {
|
|
21
|
-
try {
|
|
22
|
-
db.run(`ALTER TABLE task ADD COLUMN project_path TEXT`);
|
|
23
|
-
console.log(`[Migration 001] Added project_path column`);
|
|
24
|
-
} catch (e) {
|
|
25
|
-
if (e.message?.includes("duplicate column name")) {
|
|
26
|
-
console.log(`[Migration 001] project_path column already exists, skipping`);
|
|
27
|
-
} else {
|
|
28
|
-
throw e;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
db.run(`ALTER TABLE task ADD COLUMN context TEXT`);
|
|
33
|
-
console.log(`[Migration 001] Added context column`);
|
|
34
|
-
} catch (e) {
|
|
35
|
-
if (e.message?.includes("duplicate column name")) {
|
|
36
|
-
console.log(`[Migration 001] context column already exists, skipping`);
|
|
37
|
-
} else {
|
|
38
|
-
throw e;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
console.log(`[Migration 001] Migration completed successfully`);
|
|
42
|
-
} finally {
|
|
43
|
-
db.close();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
var init_001_add_project_path_context = __esm(() => {
|
|
47
|
-
init_sqlite_task_store();
|
|
48
|
-
if (false) {}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// src/env/task/storage/migrations/002_add_task_type.ts
|
|
52
|
-
var exports_002_add_task_type = {};
|
|
53
|
-
__export(exports_002_add_task_type, {
|
|
54
|
-
runMigration: () => runMigration2
|
|
55
|
-
});
|
|
56
|
-
function runMigration2(dbPath) {
|
|
57
|
-
console.log(`[Migration 002] Starting migration for: ${dbPath}`);
|
|
58
|
-
const db = createCompatibleDatabase(dbPath);
|
|
59
|
-
try {
|
|
60
|
-
try {
|
|
61
|
-
db.run(`ALTER TABLE task ADD COLUMN type TEXT NOT NULL DEFAULT 'normal'`);
|
|
62
|
-
console.log(`[Migration 002] Added type column`);
|
|
63
|
-
} catch (e) {
|
|
64
|
-
if (e.message?.includes("duplicate column name")) {
|
|
65
|
-
console.log(`[Migration 002] type column already exists, skipping`);
|
|
66
|
-
} else {
|
|
67
|
-
throw e;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
console.log(`[Migration 002] Migration completed successfully`);
|
|
71
|
-
} finally {
|
|
72
|
-
db.close();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
var init_002_add_task_type = __esm(() => {
|
|
76
|
-
init_sqlite_task_store();
|
|
77
|
-
if (false) {}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// src/env/task/storage/sqlite-task-store.ts
|
|
81
|
-
import { join } from "path";
|
|
82
|
-
function getDatabaseClass() {
|
|
83
|
-
if (typeof Bun !== "undefined") {
|
|
84
|
-
const { Database } = __require("bun:sqlite");
|
|
85
|
-
return Database;
|
|
86
|
-
} else {
|
|
87
|
-
return __require("better-sqlite3");
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function createCompatibleDatabase(path) {
|
|
91
|
-
const Database = getDatabaseClass();
|
|
92
|
-
const db = new Database(path);
|
|
93
|
-
if (typeof db.run !== "function") {
|
|
94
|
-
db.run = function(sql, ...params) {
|
|
95
|
-
if (params.length > 0) {
|
|
96
|
-
return this.prepare(sql).run(...params);
|
|
97
|
-
} else {
|
|
98
|
-
return this.exec(sql);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
return db;
|
|
103
|
-
}
|
|
104
|
-
function getDefaultDataDir() {
|
|
105
|
-
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
106
|
-
return join(home, ".local", "share");
|
|
107
|
-
}
|
|
108
|
-
function getDefaultTaskDbPath() {
|
|
109
|
-
return join(getDefaultDataDir(), "roy-agent", "tasks.db");
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
class SQLiteTaskStore {
|
|
113
|
-
db = null;
|
|
114
|
-
dbPath;
|
|
115
|
-
initialized = false;
|
|
116
|
-
tasksCache = new Map;
|
|
117
|
-
operationsCache = new Map;
|
|
118
|
-
constructor(dbPath) {
|
|
119
|
-
this.dbPath = dbPath || getDefaultTaskDbPath();
|
|
120
|
-
}
|
|
121
|
-
async initialize() {
|
|
122
|
-
if (this.initialized)
|
|
123
|
-
return;
|
|
124
|
-
if (this.dbPath !== ":memory:") {
|
|
125
|
-
const fs = __require("fs");
|
|
126
|
-
const path = __require("path");
|
|
127
|
-
const dir = path.dirname(this.dbPath);
|
|
128
|
-
if (!fs.existsSync(dir)) {
|
|
129
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
this.db = createCompatibleDatabase(this.dbPath);
|
|
133
|
-
this.db.run(`
|
|
134
|
-
CREATE TABLE IF NOT EXISTS task (
|
|
135
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
136
|
-
title TEXT NOT NULL,
|
|
137
|
-
description TEXT DEFAULT '',
|
|
138
|
-
status TEXT DEFAULT 'todo',
|
|
139
|
-
priority TEXT DEFAULT 'medium',
|
|
140
|
-
type TEXT DEFAULT 'normal',
|
|
141
|
-
progress INTEGER DEFAULT 0,
|
|
142
|
-
current_status TEXT DEFAULT '',
|
|
143
|
-
goals_and_expected_deliverables TEXT DEFAULT '',
|
|
144
|
-
parent_task_id INTEGER,
|
|
145
|
-
created_at INTEGER NOT NULL,
|
|
146
|
-
updated_at INTEGER NOT NULL,
|
|
147
|
-
due_date TEXT,
|
|
148
|
-
tags TEXT,
|
|
149
|
-
project_path TEXT NOT NULL DEFAULT 'unknown',
|
|
150
|
-
context TEXT NOT NULL DEFAULT '{}'
|
|
151
|
-
)
|
|
152
|
-
`);
|
|
153
|
-
this.db.run(`
|
|
154
|
-
CREATE TABLE IF NOT EXISTS task_operation (
|
|
155
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
156
|
-
task_id INTEGER NOT NULL,
|
|
157
|
-
session_id TEXT NOT NULL,
|
|
158
|
-
action_type TEXT NOT NULL,
|
|
159
|
-
action_title TEXT NOT NULL,
|
|
160
|
-
action_description TEXT DEFAULT '',
|
|
161
|
-
timestamp INTEGER NOT NULL,
|
|
162
|
-
md_path TEXT,
|
|
163
|
-
FOREIGN KEY (task_id) REFERENCES task(id) ON DELETE CASCADE
|
|
164
|
-
)
|
|
165
|
-
`);
|
|
166
|
-
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_operation_task_id ON task_operation(task_id)");
|
|
167
|
-
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_operation_session_id ON task_operation(session_id)");
|
|
168
|
-
this.db.run(`
|
|
169
|
-
CREATE TABLE IF NOT EXISTS task_tag (
|
|
170
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
171
|
-
name TEXT NOT NULL UNIQUE,
|
|
172
|
-
created_at INTEGER NOT NULL,
|
|
173
|
-
task_count INTEGER DEFAULT 0
|
|
174
|
-
)
|
|
175
|
-
`);
|
|
176
|
-
this.db.run(`
|
|
177
|
-
CREATE TABLE IF NOT EXISTS task_tag_relation (
|
|
178
|
-
task_id INTEGER NOT NULL,
|
|
179
|
-
tag_id INTEGER NOT NULL,
|
|
180
|
-
created_at INTEGER NOT NULL,
|
|
181
|
-
PRIMARY KEY (task_id, tag_id),
|
|
182
|
-
FOREIGN KEY (task_id) REFERENCES task(id) ON DELETE CASCADE,
|
|
183
|
-
FOREIGN KEY (tag_id) REFERENCES task_tag(id) ON DELETE CASCADE
|
|
184
|
-
)
|
|
185
|
-
`);
|
|
186
|
-
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_tag_relation_tag_id ON task_tag_relation(tag_id)");
|
|
187
|
-
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_parent_task_id ON task(parent_task_id)");
|
|
188
|
-
if (this.dbPath !== ":memory:") {
|
|
189
|
-
try {
|
|
190
|
-
const { runMigration: runMigration001 } = (init_001_add_project_path_context(), __toCommonJS(exports_001_add_project_path_context));
|
|
191
|
-
runMigration001(this.dbPath);
|
|
192
|
-
} catch (e) {
|
|
193
|
-
logger.warn(`[Migration 001] Failed to run: ${e.message}`);
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
const { runMigration: runMigration002 } = (init_002_add_task_type(), __toCommonJS(exports_002_add_task_type));
|
|
197
|
-
runMigration002(this.dbPath);
|
|
198
|
-
} catch (e) {
|
|
199
|
-
logger.warn(`[Migration 002] Failed to run: ${e.message}`);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
this.initialized = true;
|
|
203
|
-
logger.info(`TaskStore initialized at ${this.dbPath}`);
|
|
204
|
-
}
|
|
205
|
-
async createTask(options) {
|
|
206
|
-
await this.initialize();
|
|
207
|
-
const now = Date.now();
|
|
208
|
-
const stmt = this.db.prepare(`
|
|
209
|
-
INSERT INTO task (title, description, priority, type, parent_task_id,
|
|
210
|
-
goals_and_expected_deliverables, due_date, tags, status,
|
|
211
|
-
current_status, created_at, updated_at, project_path, context)
|
|
212
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'todo', '', ?, ?, ?, ?)
|
|
213
|
-
`);
|
|
214
|
-
const result = stmt.run(options.title, options.description || "", options.priority || "medium", options.type || "normal", options.parent_task_id || null, options.goals_and_expected_deliverables || "", options.due_date || null, options.tags ? JSON.stringify(options.tags) : null, now, now, options.project_path, options.context);
|
|
215
|
-
const task = await this.getTask(result.lastInsertRowid);
|
|
216
|
-
if (task) {
|
|
217
|
-
this.tasksCache.set(task.id, task);
|
|
218
|
-
return task;
|
|
219
|
-
}
|
|
220
|
-
throw new Error("Failed to create task");
|
|
221
|
-
}
|
|
222
|
-
async getTask(id) {
|
|
223
|
-
await this.initialize();
|
|
224
|
-
if (this.tasksCache.has(id)) {
|
|
225
|
-
return this.tasksCache.get(id);
|
|
226
|
-
}
|
|
227
|
-
const stmt = this.db.prepare("SELECT * FROM task WHERE id = ?");
|
|
228
|
-
const row = stmt.get(id);
|
|
229
|
-
if (!row)
|
|
230
|
-
return;
|
|
231
|
-
const task = this.rowToTask(row);
|
|
232
|
-
this.tasksCache.set(id, task);
|
|
233
|
-
return task;
|
|
234
|
-
}
|
|
235
|
-
async updateTask(id, options) {
|
|
236
|
-
await this.initialize();
|
|
237
|
-
const task = await this.getTask(id);
|
|
238
|
-
if (!task)
|
|
239
|
-
return;
|
|
240
|
-
const updates = [];
|
|
241
|
-
const values = [];
|
|
242
|
-
if (options.title !== undefined) {
|
|
243
|
-
updates.push("title = ?");
|
|
244
|
-
values.push(options.title);
|
|
245
|
-
}
|
|
246
|
-
if (options.description !== undefined) {
|
|
247
|
-
updates.push("description = ?");
|
|
248
|
-
values.push(options.description);
|
|
249
|
-
}
|
|
250
|
-
if (options.status !== undefined) {
|
|
251
|
-
updates.push("status = ?");
|
|
252
|
-
values.push(options.status);
|
|
253
|
-
}
|
|
254
|
-
if (options.priority !== undefined) {
|
|
255
|
-
updates.push("priority = ?");
|
|
256
|
-
values.push(options.priority);
|
|
257
|
-
}
|
|
258
|
-
if (options.type !== undefined) {
|
|
259
|
-
updates.push("type = ?");
|
|
260
|
-
values.push(options.type);
|
|
261
|
-
}
|
|
262
|
-
if (options.progress !== undefined) {
|
|
263
|
-
updates.push("progress = ?");
|
|
264
|
-
values.push(options.progress);
|
|
265
|
-
}
|
|
266
|
-
if (options.current_status !== undefined) {
|
|
267
|
-
updates.push("current_status = ?");
|
|
268
|
-
values.push(options.current_status);
|
|
269
|
-
}
|
|
270
|
-
if (options.goals_and_expected_deliverables !== undefined) {
|
|
271
|
-
updates.push("goals_and_expected_deliverables = ?");
|
|
272
|
-
values.push(options.goals_and_expected_deliverables);
|
|
273
|
-
}
|
|
274
|
-
if (options.due_date !== undefined) {
|
|
275
|
-
updates.push("due_date = ?");
|
|
276
|
-
values.push(options.due_date);
|
|
277
|
-
}
|
|
278
|
-
if (options.tags !== undefined) {
|
|
279
|
-
updates.push("tags = ?");
|
|
280
|
-
values.push(JSON.stringify(options.tags));
|
|
281
|
-
}
|
|
282
|
-
if (options.project_path !== undefined) {
|
|
283
|
-
updates.push("project_path = ?");
|
|
284
|
-
values.push(options.project_path);
|
|
285
|
-
}
|
|
286
|
-
if (options.context !== undefined) {
|
|
287
|
-
updates.push("context = ?");
|
|
288
|
-
values.push(options.context);
|
|
289
|
-
}
|
|
290
|
-
if (options.parent_task_id !== undefined) {
|
|
291
|
-
updates.push("parent_task_id = ?");
|
|
292
|
-
values.push(options.parent_task_id);
|
|
293
|
-
}
|
|
294
|
-
if (updates.length === 0)
|
|
295
|
-
return task;
|
|
296
|
-
updates.push("updated_at = ?");
|
|
297
|
-
values.push(Date.now());
|
|
298
|
-
values.push(id);
|
|
299
|
-
const stmt = this.db.prepare(`UPDATE task SET ${updates.join(", ")} WHERE id = ?`);
|
|
300
|
-
stmt.run(...values);
|
|
301
|
-
this.tasksCache.delete(id);
|
|
302
|
-
return this.getTask(id);
|
|
303
|
-
}
|
|
304
|
-
async deleteTask(id) {
|
|
305
|
-
await this.initialize();
|
|
306
|
-
const stmt = this.db.prepare("DELETE FROM task WHERE id = ?");
|
|
307
|
-
const result = stmt.run(id);
|
|
308
|
-
if (result.changes > 0) {
|
|
309
|
-
this.tasksCache.delete(id);
|
|
310
|
-
this.operationsCache.delete(id);
|
|
311
|
-
}
|
|
312
|
-
return result.changes > 0;
|
|
313
|
-
}
|
|
314
|
-
async listTasks(options) {
|
|
315
|
-
await this.initialize();
|
|
316
|
-
if (options?.depth !== undefined && options.depth >= 0 && options.parent_task_id === undefined) {
|
|
317
|
-
return this.listTasksByDepth(options);
|
|
318
|
-
}
|
|
319
|
-
const whereClauses = ["1=1"];
|
|
320
|
-
const whereParams = [];
|
|
321
|
-
if (options?.status) {
|
|
322
|
-
whereClauses.push("status = ?");
|
|
323
|
-
whereParams.push(options.status);
|
|
324
|
-
}
|
|
325
|
-
if (options?.priority) {
|
|
326
|
-
whereClauses.push("priority = ?");
|
|
327
|
-
whereParams.push(options.priority);
|
|
328
|
-
}
|
|
329
|
-
if (options?.type) {
|
|
330
|
-
whereClauses.push("type = ?");
|
|
331
|
-
whereParams.push(options.type);
|
|
332
|
-
}
|
|
333
|
-
if (options?.parent_task_id !== undefined) {
|
|
334
|
-
whereClauses.push("parent_task_id = ?");
|
|
335
|
-
whereParams.push(options.parent_task_id);
|
|
336
|
-
}
|
|
337
|
-
if (!options?.include_archived && options?.status !== "archived") {
|
|
338
|
-
whereClauses.push("status != 'archived'");
|
|
339
|
-
}
|
|
340
|
-
const whereSql = whereClauses.join(" AND ");
|
|
341
|
-
const countSql = `SELECT COUNT(*) as total FROM task WHERE ${whereSql}`;
|
|
342
|
-
const countRow = this.db.prepare(countSql).get(...whereParams);
|
|
343
|
-
const total = countRow?.total ?? 0;
|
|
344
|
-
let sql = `SELECT * FROM task WHERE ${whereSql}`;
|
|
345
|
-
const params = [...whereParams];
|
|
346
|
-
sql += " ORDER BY updated_at DESC";
|
|
347
|
-
if (options?.limit) {
|
|
348
|
-
sql += " LIMIT ?";
|
|
349
|
-
params.push(options.limit);
|
|
350
|
-
}
|
|
351
|
-
if (options?.offset) {
|
|
352
|
-
sql += " OFFSET ?";
|
|
353
|
-
params.push(options.offset);
|
|
354
|
-
}
|
|
355
|
-
const stmt = this.db.prepare(sql);
|
|
356
|
-
const rows = stmt.all(...params);
|
|
357
|
-
const tasks = rows.map((row) => {
|
|
358
|
-
const task = this.rowToTask(row);
|
|
359
|
-
this.tasksCache.set(task.id, task);
|
|
360
|
-
return task;
|
|
361
|
-
});
|
|
362
|
-
return { tasks, total };
|
|
363
|
-
}
|
|
364
|
-
async listTasksByDepth(options) {
|
|
365
|
-
const depth = options.depth ?? 0;
|
|
366
|
-
const filterClauses = ["1=1"];
|
|
367
|
-
const filterParams = [];
|
|
368
|
-
if (options.status) {
|
|
369
|
-
filterClauses.push("status = ?");
|
|
370
|
-
filterParams.push(options.status);
|
|
371
|
-
}
|
|
372
|
-
if (options.priority) {
|
|
373
|
-
filterClauses.push("priority = ?");
|
|
374
|
-
filterParams.push(options.priority);
|
|
375
|
-
}
|
|
376
|
-
if (options.type) {
|
|
377
|
-
filterClauses.push("type = ?");
|
|
378
|
-
filterParams.push(options.type);
|
|
379
|
-
}
|
|
380
|
-
if (!options?.include_archived && options?.status !== "archived") {
|
|
381
|
-
filterClauses.push("status != 'archived'");
|
|
382
|
-
}
|
|
383
|
-
const filterSql = filterClauses.join(" AND ");
|
|
384
|
-
const collected = [];
|
|
385
|
-
if (depth === 0) {
|
|
386
|
-
const rootSql = `SELECT * FROM task WHERE parent_task_id IS NULL AND ${filterSql} ORDER BY updated_at DESC`;
|
|
387
|
-
const rows = this.db.prepare(rootSql).all(...filterParams);
|
|
388
|
-
for (const row of rows) {
|
|
389
|
-
const t = this.rowToTask(row);
|
|
390
|
-
this.tasksCache.set(t.id, t);
|
|
391
|
-
collected.push(t);
|
|
392
|
-
}
|
|
393
|
-
} else {
|
|
394
|
-
const rootRows = this.db.prepare(`SELECT * FROM task WHERE parent_task_id IS NULL AND ${filterSql} ORDER BY updated_at DESC`).all(...filterParams);
|
|
395
|
-
let currentLevel = rootRows.map((row) => {
|
|
396
|
-
const t = this.rowToTask(row);
|
|
397
|
-
this.tasksCache.set(t.id, t);
|
|
398
|
-
return t;
|
|
399
|
-
});
|
|
400
|
-
collected.push(...currentLevel);
|
|
401
|
-
for (let level = 1;level <= depth; level++) {
|
|
402
|
-
if (currentLevel.length === 0)
|
|
403
|
-
break;
|
|
404
|
-
const ids = currentLevel.map((t) => t.id);
|
|
405
|
-
const placeholders = ids.map(() => "?").join(",");
|
|
406
|
-
const childSql = `SELECT * FROM task WHERE parent_task_id IN (${placeholders}) AND ${filterSql} ORDER BY updated_at DESC`;
|
|
407
|
-
const childRows = this.db.prepare(childSql).all(...ids, ...filterParams);
|
|
408
|
-
const nextLevel = childRows.map((row) => {
|
|
409
|
-
const t = this.rowToTask(row);
|
|
410
|
-
this.tasksCache.set(t.id, t);
|
|
411
|
-
return t;
|
|
412
|
-
});
|
|
413
|
-
collected.push(...nextLevel);
|
|
414
|
-
currentLevel = nextLevel;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
const total = collected.length;
|
|
418
|
-
const offset = options.offset ?? 0;
|
|
419
|
-
const limit = options.limit ?? total;
|
|
420
|
-
const sliced = collected.slice(offset, offset + limit);
|
|
421
|
-
return { tasks: sliced, total };
|
|
422
|
-
}
|
|
423
|
-
async getTaskTree(taskId, depth = -1) {
|
|
424
|
-
await this.initialize();
|
|
425
|
-
const result = [];
|
|
426
|
-
let currentLevel = [taskId];
|
|
427
|
-
let currentDepth = 0;
|
|
428
|
-
while (currentLevel.length > 0) {
|
|
429
|
-
if (depth !== -1 && currentDepth >= depth)
|
|
430
|
-
break;
|
|
431
|
-
const placeholders = currentLevel.map(() => "?").join(",");
|
|
432
|
-
const rows = this.db.prepare(`SELECT * FROM task WHERE parent_task_id IN (${placeholders}) ORDER BY updated_at DESC`).all(...currentLevel);
|
|
433
|
-
const nextLevel = [];
|
|
434
|
-
for (const row of rows) {
|
|
435
|
-
const t = this.rowToTask(row);
|
|
436
|
-
this.tasksCache.set(t.id, t);
|
|
437
|
-
result.push(t);
|
|
438
|
-
nextLevel.push(t.id);
|
|
439
|
-
}
|
|
440
|
-
currentLevel = nextLevel;
|
|
441
|
-
currentDepth++;
|
|
442
|
-
}
|
|
443
|
-
return result;
|
|
444
|
-
}
|
|
445
|
-
async getChildTasks(taskId, options = {}) {
|
|
446
|
-
return this.listTasks({ ...options, parent_task_id: taskId });
|
|
447
|
-
}
|
|
448
|
-
async createOperation(options) {
|
|
449
|
-
await this.initialize();
|
|
450
|
-
const now = Date.now();
|
|
451
|
-
const stmt = this.db.prepare(`
|
|
452
|
-
INSERT INTO task_operation (task_id, session_id, action_type, action_title,
|
|
453
|
-
action_description, timestamp, md_path)
|
|
454
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
455
|
-
`);
|
|
456
|
-
const result = stmt.run(options.taskId, options.sessionId, options.actionType, options.actionTitle, options.actionDescription || "", now, options.mdPath || null);
|
|
457
|
-
this.operationsCache.delete(options.taskId);
|
|
458
|
-
const operation = await this.getOperation(result.lastInsertRowid);
|
|
459
|
-
if (!operation) {
|
|
460
|
-
throw new Error("Failed to create operation");
|
|
461
|
-
}
|
|
462
|
-
return operation;
|
|
463
|
-
}
|
|
464
|
-
async getOperation(id) {
|
|
465
|
-
await this.initialize();
|
|
466
|
-
const stmt = this.db.prepare("SELECT * FROM task_operation WHERE id = ?");
|
|
467
|
-
const row = stmt.get(id);
|
|
468
|
-
if (!row)
|
|
469
|
-
return;
|
|
470
|
-
return this.rowToOperation(row);
|
|
471
|
-
}
|
|
472
|
-
async updateOperation(id, updates) {
|
|
473
|
-
await this.initialize();
|
|
474
|
-
const op = await this.getOperation(id);
|
|
475
|
-
if (!op)
|
|
476
|
-
return;
|
|
477
|
-
const setClauses = [];
|
|
478
|
-
const values = [];
|
|
479
|
-
if (updates.actionTitle !== undefined) {
|
|
480
|
-
setClauses.push("action_title = ?");
|
|
481
|
-
values.push(updates.actionTitle);
|
|
482
|
-
}
|
|
483
|
-
if (updates.actionDescription !== undefined) {
|
|
484
|
-
setClauses.push("action_description = ?");
|
|
485
|
-
values.push(updates.actionDescription);
|
|
486
|
-
}
|
|
487
|
-
if (setClauses.length === 0)
|
|
488
|
-
return op;
|
|
489
|
-
const stmt = this.db.prepare(`UPDATE task_operation SET ${setClauses.join(", ")} WHERE id = ?`);
|
|
490
|
-
stmt.run(...values, id);
|
|
491
|
-
this.operationsCache.delete(op.taskId);
|
|
492
|
-
return this.getOperation(id);
|
|
493
|
-
}
|
|
494
|
-
async deleteOperation(id) {
|
|
495
|
-
await this.initialize();
|
|
496
|
-
const op = await this.getOperation(id);
|
|
497
|
-
if (!op)
|
|
498
|
-
return false;
|
|
499
|
-
const stmt = this.db.prepare("DELETE FROM task_operation WHERE id = ?");
|
|
500
|
-
const result = stmt.run(id);
|
|
501
|
-
if (result.changes > 0) {
|
|
502
|
-
this.operationsCache.delete(op.taskId);
|
|
503
|
-
}
|
|
504
|
-
return result.changes > 0;
|
|
505
|
-
}
|
|
506
|
-
async listOperations(options) {
|
|
507
|
-
await this.initialize();
|
|
508
|
-
const cacheKey = options.taskId;
|
|
509
|
-
if (!this.operationsCache.has(cacheKey)) {
|
|
510
|
-
const stmt = this.db.prepare("SELECT * FROM task_operation WHERE task_id = ? ORDER BY timestamp DESC");
|
|
511
|
-
const rows = stmt.all(options.taskId);
|
|
512
|
-
this.operationsCache.set(cacheKey, rows.map((row) => this.rowToOperation(row)));
|
|
513
|
-
}
|
|
514
|
-
let ops = this.operationsCache.get(cacheKey);
|
|
515
|
-
if (options.actionType) {
|
|
516
|
-
ops = ops.filter((op) => op.actionType === options.actionType);
|
|
517
|
-
}
|
|
518
|
-
const offset = options.offset || 0;
|
|
519
|
-
const limit = options.limit || 20;
|
|
520
|
-
return ops.slice(offset, offset + limit);
|
|
521
|
-
}
|
|
522
|
-
rowToTask(row) {
|
|
523
|
-
return {
|
|
524
|
-
id: row.id,
|
|
525
|
-
title: row.title,
|
|
526
|
-
description: row.description,
|
|
527
|
-
status: row.status,
|
|
528
|
-
priority: row.priority,
|
|
529
|
-
type: row.type || "normal",
|
|
530
|
-
progress: row.progress,
|
|
531
|
-
current_status: row.current_status,
|
|
532
|
-
goals_and_expected_deliverables: row.goals_and_expected_deliverables,
|
|
533
|
-
parent_task_id: row.parent_task_id || undefined,
|
|
534
|
-
createdAt: new Date(row.created_at).toISOString(),
|
|
535
|
-
updatedAt: new Date(row.updated_at).toISOString(),
|
|
536
|
-
due_date: row.due_date || undefined,
|
|
537
|
-
tags: row.tags ? JSON.parse(row.tags) : undefined,
|
|
538
|
-
project_path: row.project_path,
|
|
539
|
-
context: row.context
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
rowToOperation(row) {
|
|
543
|
-
return {
|
|
544
|
-
id: row.id,
|
|
545
|
-
taskId: row.task_id,
|
|
546
|
-
sessionId: row.session_id,
|
|
547
|
-
actionType: row.action_type,
|
|
548
|
-
actionTitle: row.action_title,
|
|
549
|
-
actionDescription: row.action_description,
|
|
550
|
-
timestamp: new Date(row.timestamp).toISOString(),
|
|
551
|
-
mdPath: row.md_path || undefined
|
|
552
|
-
};
|
|
553
|
-
}
|
|
554
|
-
async createTag(name) {
|
|
555
|
-
await this.initialize();
|
|
556
|
-
const now = Date.now();
|
|
557
|
-
const existing = await this.getTagByName(name);
|
|
558
|
-
if (existing) {
|
|
559
|
-
return existing;
|
|
560
|
-
}
|
|
561
|
-
const stmt = this.db.prepare(`
|
|
562
|
-
INSERT INTO task_tag (name, created_at, task_count)
|
|
563
|
-
VALUES (?, ?, 0)
|
|
564
|
-
`);
|
|
565
|
-
const result = stmt.run(name, now);
|
|
566
|
-
return {
|
|
567
|
-
id: result.lastInsertRowid,
|
|
568
|
-
name,
|
|
569
|
-
createdAt: new Date(now).toISOString(),
|
|
570
|
-
taskCount: 0
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
|
-
async getTag(id) {
|
|
574
|
-
await this.initialize();
|
|
575
|
-
const stmt = this.db.prepare("SELECT * FROM task_tag WHERE id = ?");
|
|
576
|
-
const row = stmt.get(id);
|
|
577
|
-
if (!row)
|
|
578
|
-
return;
|
|
579
|
-
return this.rowToTag(row);
|
|
580
|
-
}
|
|
581
|
-
async getTagByName(name) {
|
|
582
|
-
await this.initialize();
|
|
583
|
-
const stmt = this.db.prepare("SELECT * FROM task_tag WHERE name = ?");
|
|
584
|
-
const row = stmt.get(name);
|
|
585
|
-
if (!row)
|
|
586
|
-
return;
|
|
587
|
-
return this.rowToTag(row);
|
|
588
|
-
}
|
|
589
|
-
async listTags(options) {
|
|
590
|
-
await this.initialize();
|
|
591
|
-
let sql = "SELECT * FROM task_tag";
|
|
592
|
-
const params = [];
|
|
593
|
-
const orderBy = options?.orderBy || "count";
|
|
594
|
-
if (orderBy === "count") {
|
|
595
|
-
sql += " ORDER BY task_count DESC";
|
|
596
|
-
} else if (orderBy === "name") {
|
|
597
|
-
sql += " ORDER BY name ASC";
|
|
598
|
-
} else {
|
|
599
|
-
sql += " ORDER BY created_at DESC";
|
|
600
|
-
}
|
|
601
|
-
if (options?.limit) {
|
|
602
|
-
sql += " LIMIT ?";
|
|
603
|
-
params.push(options.limit);
|
|
604
|
-
}
|
|
605
|
-
if (options?.offset) {
|
|
606
|
-
sql += " OFFSET ?";
|
|
607
|
-
params.push(options.offset);
|
|
608
|
-
}
|
|
609
|
-
const stmt = this.db.prepare(sql);
|
|
610
|
-
const rows = stmt.all(...params);
|
|
611
|
-
return rows.map((row) => this.rowToTag(row));
|
|
612
|
-
}
|
|
613
|
-
async searchTags(options) {
|
|
614
|
-
await this.initialize();
|
|
615
|
-
const sql = `
|
|
616
|
-
SELECT * FROM task_tag
|
|
617
|
-
WHERE name LIKE ?
|
|
618
|
-
ORDER BY task_count DESC
|
|
619
|
-
LIMIT ?
|
|
620
|
-
`;
|
|
621
|
-
const stmt = this.db.prepare(sql);
|
|
622
|
-
const rows = stmt.all(`%${options.query}%`, options.limit || 20);
|
|
623
|
-
return rows.map((row) => this.rowToTag(row));
|
|
624
|
-
}
|
|
625
|
-
async deleteTag(id) {
|
|
626
|
-
await this.initialize();
|
|
627
|
-
const stmt = this.db.prepare("DELETE FROM task_tag WHERE id = ?");
|
|
628
|
-
const result = stmt.run(id);
|
|
629
|
-
return result.changes > 0;
|
|
630
|
-
}
|
|
631
|
-
async addTagsToTask(taskId, tagIds) {
|
|
632
|
-
await this.initialize();
|
|
633
|
-
const now = Date.now();
|
|
634
|
-
const stmt = this.db.prepare(`
|
|
635
|
-
INSERT OR IGNORE INTO task_tag_relation (task_id, tag_id, created_at)
|
|
636
|
-
VALUES (?, ?, ?)
|
|
637
|
-
`);
|
|
638
|
-
for (const tagId of tagIds) {
|
|
639
|
-
stmt.run(taskId, tagId, now);
|
|
640
|
-
this.db.run(`
|
|
641
|
-
UPDATE task_tag SET task_count = task_count + 1 WHERE id = ?
|
|
642
|
-
`, tagId);
|
|
643
|
-
}
|
|
644
|
-
await this.syncTaskTagsJson(taskId);
|
|
645
|
-
}
|
|
646
|
-
async removeTagsFromTask(taskId, tagIds) {
|
|
647
|
-
await this.initialize();
|
|
648
|
-
const stmt = this.db.prepare("DELETE FROM task_tag_relation WHERE task_id = ? AND tag_id = ?");
|
|
649
|
-
for (const tagId of tagIds) {
|
|
650
|
-
stmt.run(taskId, tagId);
|
|
651
|
-
this.db.run(`
|
|
652
|
-
UPDATE task_tag SET task_count = MAX(0, task_count - 1) WHERE id = ?
|
|
653
|
-
`, tagId);
|
|
654
|
-
}
|
|
655
|
-
await this.syncTaskTagsJson(taskId);
|
|
656
|
-
}
|
|
657
|
-
async getTagsForTask(taskId) {
|
|
658
|
-
await this.initialize();
|
|
659
|
-
const sql = `
|
|
660
|
-
SELECT t.* FROM task_tag t
|
|
661
|
-
INNER JOIN task_tag_relation r ON t.id = r.tag_id
|
|
662
|
-
WHERE r.task_id = ?
|
|
663
|
-
ORDER BY t.name ASC
|
|
664
|
-
`;
|
|
665
|
-
const stmt = this.db.prepare(sql);
|
|
666
|
-
const rows = stmt.all(taskId);
|
|
667
|
-
return rows.map((row) => this.rowToTag(row));
|
|
668
|
-
}
|
|
669
|
-
async getTaskIdsByTags(tagNames) {
|
|
670
|
-
await this.initialize();
|
|
671
|
-
if (tagNames.length === 0)
|
|
672
|
-
return [];
|
|
673
|
-
const placeholders = tagNames.map(() => "?").join(", ");
|
|
674
|
-
const sql = `
|
|
675
|
-
SELECT DISTINCT r.task_id FROM task_tag_relation r
|
|
676
|
-
INNER JOIN task_tag t ON r.tag_id = t.id
|
|
677
|
-
WHERE t.name IN (${placeholders})
|
|
678
|
-
`;
|
|
679
|
-
const stmt = this.db.prepare(sql);
|
|
680
|
-
const rows = stmt.all(...tagNames);
|
|
681
|
-
return rows.map((row) => row.task_id);
|
|
682
|
-
}
|
|
683
|
-
async updateTaskTags(taskId, tagNames) {
|
|
684
|
-
await this.initialize();
|
|
685
|
-
const currentTags = await this.getTagsForTask(taskId);
|
|
686
|
-
if (currentTags.length > 0) {
|
|
687
|
-
await this.removeTagsFromTask(taskId, currentTags.map((t) => t.id));
|
|
688
|
-
}
|
|
689
|
-
const tagIds = [];
|
|
690
|
-
for (const name of tagNames) {
|
|
691
|
-
let tag = await this.getTagByName(name);
|
|
692
|
-
if (!tag) {
|
|
693
|
-
tag = await this.createTag(name);
|
|
694
|
-
}
|
|
695
|
-
tagIds.push(tag.id);
|
|
696
|
-
}
|
|
697
|
-
if (tagIds.length > 0) {
|
|
698
|
-
await this.addTagsToTask(taskId, tagIds);
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
async searchTasksByKeywords(keywords, options = {}) {
|
|
702
|
-
await this.initialize();
|
|
703
|
-
if (!keywords || keywords.length === 0) {
|
|
704
|
-
return { tasks: [], total: 0 };
|
|
705
|
-
}
|
|
706
|
-
const { limit = 20, offset = 0, excludeTaskId, status, priority, type, depth } = options;
|
|
707
|
-
let allowedTaskIds = null;
|
|
708
|
-
if (depth !== undefined && depth >= 0) {
|
|
709
|
-
allowedTaskIds = await this.collectTaskIdsByDepth(depth);
|
|
710
|
-
}
|
|
711
|
-
const tasks = new Map;
|
|
712
|
-
let taskSql = "SELECT * FROM task WHERE 1=1";
|
|
713
|
-
const taskParams = [];
|
|
714
|
-
if (excludeTaskId) {
|
|
715
|
-
taskSql += " AND id != ?";
|
|
716
|
-
taskParams.push(excludeTaskId);
|
|
717
|
-
}
|
|
718
|
-
if (status) {
|
|
719
|
-
taskSql += " AND status = ?";
|
|
720
|
-
taskParams.push(status);
|
|
721
|
-
}
|
|
722
|
-
if (priority) {
|
|
723
|
-
taskSql += " AND priority = ?";
|
|
724
|
-
taskParams.push(priority);
|
|
725
|
-
}
|
|
726
|
-
if (type) {
|
|
727
|
-
taskSql += " AND type = ?";
|
|
728
|
-
taskParams.push(type);
|
|
729
|
-
}
|
|
730
|
-
if (!options?.include_archived && options?.status !== "archived") {
|
|
731
|
-
taskSql += " AND status != 'archived'";
|
|
732
|
-
}
|
|
733
|
-
const scanLimit = Math.max(limit * 5, 200);
|
|
734
|
-
taskSql += " ORDER BY updated_at DESC LIMIT ?";
|
|
735
|
-
taskParams.push(scanLimit);
|
|
736
|
-
const taskRows = this.db.prepare(taskSql).all(...taskParams);
|
|
737
|
-
for (const row of taskRows) {
|
|
738
|
-
const task = this.rowToTask(row);
|
|
739
|
-
const matchedKeywords = new Set;
|
|
740
|
-
let matchCount = 0;
|
|
741
|
-
if (task.title) {
|
|
742
|
-
const lowerTitle = task.title.toLowerCase();
|
|
743
|
-
for (const keyword of keywords) {
|
|
744
|
-
if (lowerTitle.includes(keyword.toLowerCase())) {
|
|
745
|
-
{
|
|
746
|
-
matchCount++;
|
|
747
|
-
matchedKeywords.add(keyword);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
if (task.tags && Array.isArray(task.tags)) {
|
|
753
|
-
for (const keyword of keywords) {
|
|
754
|
-
const lowerKeyword = keyword.toLowerCase();
|
|
755
|
-
if (task.tags.some((tag) => tag.toLowerCase().includes(lowerKeyword))) {
|
|
756
|
-
{
|
|
757
|
-
matchCount += 2;
|
|
758
|
-
matchedKeywords.add(keyword);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
if (task.description) {
|
|
764
|
-
const lowerDesc = task.description.toLowerCase();
|
|
765
|
-
for (const keyword of keywords) {
|
|
766
|
-
if (lowerDesc.includes(keyword.toLowerCase())) {
|
|
767
|
-
{
|
|
768
|
-
matchCount++;
|
|
769
|
-
matchedKeywords.add(keyword);
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
if (task.goals_and_expected_deliverables) {
|
|
775
|
-
const lowerGoals = task.goals_and_expected_deliverables.toLowerCase();
|
|
776
|
-
for (const keyword of keywords) {
|
|
777
|
-
if (lowerGoals.includes(keyword.toLowerCase())) {
|
|
778
|
-
{
|
|
779
|
-
matchCount++;
|
|
780
|
-
matchedKeywords.add(keyword);
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
if (matchedKeywords.size === keywords.length) {
|
|
786
|
-
tasks.set(task.id, { task, matchCount });
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
const allOps = this.db.prepare("SELECT DISTINCT task_id FROM task_operation WHERE 1=1").all();
|
|
790
|
-
for (const opRow of allOps) {
|
|
791
|
-
const opSql = `
|
|
792
|
-
SELECT * FROM task_operation
|
|
793
|
-
WHERE task_id = ?
|
|
794
|
-
AND (action_title LIKE ? OR action_description LIKE ?)
|
|
795
|
-
LIMIT 10
|
|
796
|
-
`;
|
|
797
|
-
const opMatchedKeywords = new Set;
|
|
798
|
-
for (const keyword of keywords) {
|
|
799
|
-
const pattern = `%${keyword}%`;
|
|
800
|
-
const ops = this.db.prepare(opSql).all(opRow.task_id, pattern, pattern);
|
|
801
|
-
if (ops.length > 0) {
|
|
802
|
-
opMatchedKeywords.add(keyword);
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
if (opMatchedKeywords.size === 0)
|
|
806
|
-
continue;
|
|
807
|
-
const taskRow = this.db.prepare("SELECT * FROM task WHERE id = ?").get(opRow.task_id);
|
|
808
|
-
if (!taskRow)
|
|
809
|
-
continue;
|
|
810
|
-
const task = this.rowToTask(taskRow);
|
|
811
|
-
if (excludeTaskId && task.id === excludeTaskId)
|
|
812
|
-
continue;
|
|
813
|
-
if (status && task.status !== status)
|
|
814
|
-
continue;
|
|
815
|
-
if (priority && task.priority !== priority)
|
|
816
|
-
continue;
|
|
817
|
-
if (type && task.type !== type)
|
|
818
|
-
continue;
|
|
819
|
-
if (!options?.include_archived && task.status === "archived")
|
|
820
|
-
continue;
|
|
821
|
-
const existing = tasks.get(task.id);
|
|
822
|
-
if (existing) {
|
|
823
|
-
existing.matchCount += 2;
|
|
824
|
-
} else {
|
|
825
|
-
const combinedKeywords = new Set(opMatchedKeywords);
|
|
826
|
-
if (task.title) {
|
|
827
|
-
const lowerTitle = task.title.toLowerCase();
|
|
828
|
-
for (const kw of keywords) {
|
|
829
|
-
if (lowerTitle.includes(kw.toLowerCase()))
|
|
830
|
-
combinedKeywords.add(kw);
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
if (task.description) {
|
|
834
|
-
const lowerDesc = task.description.toLowerCase();
|
|
835
|
-
for (const kw of keywords) {
|
|
836
|
-
if (lowerDesc.includes(kw.toLowerCase()))
|
|
837
|
-
combinedKeywords.add(kw);
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
if (task.goals_and_expected_deliverables) {
|
|
841
|
-
const lowerGoals = task.goals_and_expected_deliverables.toLowerCase();
|
|
842
|
-
for (const kw of keywords) {
|
|
843
|
-
if (lowerGoals.includes(kw.toLowerCase()))
|
|
844
|
-
combinedKeywords.add(kw);
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
if (task.tags && Array.isArray(task.tags)) {
|
|
848
|
-
for (const kw of keywords) {
|
|
849
|
-
if (task.tags.some((tag) => tag.toLowerCase().includes(kw.toLowerCase())))
|
|
850
|
-
combinedKeywords.add(kw);
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
if (combinedKeywords.size === keywords.length) {
|
|
854
|
-
tasks.set(task.id, { task, matchCount: 2 });
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
let sorted = Array.from(tasks.values()).sort((a, b) => b.matchCount - a.matchCount);
|
|
859
|
-
if (allowedTaskIds !== null) {
|
|
860
|
-
sorted = sorted.filter((item) => allowedTaskIds.has(item.task.id));
|
|
861
|
-
}
|
|
862
|
-
const total = sorted.length;
|
|
863
|
-
const sliced = sorted.slice(offset, offset + limit);
|
|
864
|
-
const resultTasks = sliced.map((item) => item.task);
|
|
865
|
-
return { tasks: resultTasks, total };
|
|
866
|
-
}
|
|
867
|
-
async collectTaskIdsByDepth(depth) {
|
|
868
|
-
const ids = new Set;
|
|
869
|
-
const rootRows = this.db.prepare("SELECT id FROM task WHERE parent_task_id IS NULL").all();
|
|
870
|
-
let currentLevel = rootRows.map((r) => r.id);
|
|
871
|
-
for (const id of currentLevel)
|
|
872
|
-
ids.add(id);
|
|
873
|
-
if (depth === 0)
|
|
874
|
-
return ids;
|
|
875
|
-
for (let level = 1;level <= depth; level++) {
|
|
876
|
-
if (currentLevel.length === 0)
|
|
877
|
-
break;
|
|
878
|
-
const placeholders = currentLevel.map(() => "?").join(",");
|
|
879
|
-
const rows = this.db.prepare(`SELECT id FROM task WHERE parent_task_id IN (${placeholders})`).all(...currentLevel);
|
|
880
|
-
currentLevel = rows.map((r) => r.id);
|
|
881
|
-
for (const id of currentLevel)
|
|
882
|
-
ids.add(id);
|
|
883
|
-
}
|
|
884
|
-
return ids;
|
|
885
|
-
}
|
|
886
|
-
async findSimilarTasksByKeywords(keywords, options = {}) {
|
|
887
|
-
const mergedOptions = { ...options, limit: options.limit ?? 5 };
|
|
888
|
-
const result = await this.searchTasksByKeywords(keywords, mergedOptions);
|
|
889
|
-
return result.tasks;
|
|
890
|
-
}
|
|
891
|
-
async findSimilarTasks(taskId, limit = 5) {
|
|
892
|
-
await this.initialize();
|
|
893
|
-
const taskTags = await this.getTagsForTask(taskId);
|
|
894
|
-
if (taskTags.length === 0) {
|
|
895
|
-
return [];
|
|
896
|
-
}
|
|
897
|
-
const tagIds = taskTags.map((t) => t.id);
|
|
898
|
-
const placeholders = tagIds.map(() => "?").join(", ");
|
|
899
|
-
const sql = `
|
|
900
|
-
SELECT t.*, COUNT(r2.tag_id) as match_count
|
|
901
|
-
FROM task t
|
|
902
|
-
INNER JOIN task_tag_relation r ON t.id = r.task_id
|
|
903
|
-
INNER JOIN task_tag_relation r2 ON t.id = r2.task_id
|
|
904
|
-
WHERE r.tag_id IN (${placeholders})
|
|
905
|
-
AND t.id != ?
|
|
906
|
-
GROUP BY t.id
|
|
907
|
-
ORDER BY match_count DESC, t.updated_at DESC
|
|
908
|
-
LIMIT ?
|
|
909
|
-
`;
|
|
910
|
-
const stmt = this.db.prepare(sql);
|
|
911
|
-
const rows = stmt.all(...tagIds, taskId, limit);
|
|
912
|
-
return rows.map((row) => this.rowToTask(row));
|
|
913
|
-
}
|
|
914
|
-
async searchTasks(query, limit = 20) {
|
|
915
|
-
await this.initialize();
|
|
916
|
-
const sql = `
|
|
917
|
-
SELECT * FROM task
|
|
918
|
-
WHERE title LIKE ? OR description LIKE ?
|
|
919
|
-
ORDER BY updated_at DESC
|
|
920
|
-
LIMIT ?
|
|
921
|
-
`;
|
|
922
|
-
const stmt = this.db.prepare(sql);
|
|
923
|
-
const rows = stmt.all(`%${query}%`, `%${query}%`, limit);
|
|
924
|
-
return rows.map((row) => this.rowToTask(row));
|
|
925
|
-
}
|
|
926
|
-
rowToTag(row) {
|
|
927
|
-
return {
|
|
928
|
-
id: row.id,
|
|
929
|
-
name: row.name,
|
|
930
|
-
createdAt: new Date(row.created_at).toISOString(),
|
|
931
|
-
taskCount: row.task_count
|
|
932
|
-
};
|
|
933
|
-
}
|
|
934
|
-
async syncTaskTagsJson(taskId) {
|
|
935
|
-
const tags = await this.getTagsForTask(taskId);
|
|
936
|
-
const tagNames = tags.map((t) => t.name);
|
|
937
|
-
const stmt = this.db.prepare("UPDATE task SET tags = ? WHERE id = ?");
|
|
938
|
-
stmt.run(tagNames.length > 0 ? JSON.stringify(tagNames) : null, taskId);
|
|
939
|
-
this.tasksCache.delete(taskId);
|
|
940
|
-
}
|
|
941
|
-
async close() {
|
|
942
|
-
if (this.db) {
|
|
943
|
-
this.db.close();
|
|
944
|
-
this.db = null;
|
|
945
|
-
this.initialized = false;
|
|
946
|
-
}
|
|
947
|
-
this.tasksCache.clear();
|
|
948
|
-
this.operationsCache.clear();
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
var logger;
|
|
952
|
-
var init_sqlite_task_store = __esm(() => {
|
|
953
|
-
init_logger();
|
|
954
|
-
logger = createLogger("task:store");
|
|
955
|
-
});
|
|
956
|
-
|
|
957
|
-
// src/env/task/storage/index.ts
|
|
958
|
-
init_sqlite_task_store();
|
|
959
|
-
init_sqlite_task_store();
|
|
960
|
-
|
|
961
|
-
export { getDefaultDataDir, getDefaultTaskDbPath, SQLiteTaskStore };
|