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