@ai-setting/roy-agent-core 1.4.13 → 1.4.14
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/packages/core/src/config/index.js +32 -0
- package/dist/packages/core/src/env/agent/index.js +24 -0
- package/dist/packages/core/src/env/commands/index.js +14 -0
- package/dist/packages/core/src/env/debug/formatters/index.js +11 -0
- package/dist/packages/core/src/env/debug/index.js +26 -0
- package/dist/packages/core/src/env/hook/index.js +29 -0
- package/dist/packages/core/src/env/index.js +81 -0
- package/dist/packages/core/src/env/llm/index.js +40 -0
- package/dist/packages/core/src/env/log-trace/index.js +83 -0
- package/dist/packages/core/src/env/mcp/index.js +39 -0
- package/dist/packages/core/src/env/mcp/tool/index.js +14 -0
- package/dist/packages/core/src/env/memory/built-in/index.js +11 -0
- package/dist/packages/core/src/env/memory/index.js +56 -0
- package/dist/packages/core/src/env/memory/plugin/index.js +36 -0
- package/dist/packages/core/src/env/prompt/index.js +20 -0
- package/dist/packages/core/src/env/session/index.js +25 -0
- package/dist/packages/core/src/env/session/storage/index.js +18 -0
- package/dist/packages/core/src/env/skill/index.js +34 -0
- package/dist/packages/core/src/env/skill/tool/index.js +9 -0
- package/dist/packages/core/src/env/task/delegate/index.js +18 -0
- package/dist/packages/core/src/env/task/hooks/index.js +7 -0
- package/dist/packages/core/src/env/task/index.js +30 -0
- package/dist/packages/core/src/env/task/plugins/index.js +23 -0
- package/dist/packages/core/src/env/task/storage/index.js +14 -0
- package/dist/packages/core/src/env/task/tools/index.js +17 -0
- package/dist/packages/core/src/env/task/tools/operation/index.js +15 -0
- package/dist/{shared/chunk-1d4rwms4.js → packages/core/src/env/tool/built-in/index.js} +4 -4
- package/dist/packages/core/src/env/tool/index.js +39 -0
- package/dist/packages/core/src/env/workflow/decorators/index.js +27 -0
- package/dist/packages/core/src/env/workflow/engine/index.js +28 -0
- package/dist/packages/core/src/env/workflow/index.js +132 -0
- package/dist/packages/core/src/env/workflow/nodes/index.js +19 -0
- package/dist/packages/core/src/env/workflow/service/index.js +13 -0
- package/dist/packages/core/src/env/workflow/storage/index.js +27 -0
- package/dist/packages/core/src/env/workflow/tools/index.js +159 -0
- package/dist/packages/core/src/env/workflow/types/index.js +94 -0
- package/dist/packages/core/src/env/workflow/utils/index.js +637 -0
- package/dist/packages/core/src/index.js +398 -0
- package/dist/shared/@ai-setting/roy-agent-core-04fm8177.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-04qgbjbe.js +172 -0
- package/dist/shared/@ai-setting/roy-agent-core-084qqd7t.js +11 -0
- package/dist/shared/@ai-setting/roy-agent-core-0gekht4e.js +1130 -0
- package/dist/shared/@ai-setting/roy-agent-core-0hdry23r.js +419 -0
- package/dist/shared/{chunk-ze20rksg.js → @ai-setting/roy-agent-core-0sgn3de4.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-12x57kf1.js +286 -0
- package/dist/shared/@ai-setting/roy-agent-core-1f3xrrm6.js +393 -0
- package/dist/shared/{chunk-q9j99fsm.js → @ai-setting/roy-agent-core-1k28kg7h.js} +6 -6
- package/dist/shared/{chunk-2b5kbhx3.js → @ai-setting/roy-agent-core-1z1zv5g8.js} +9 -117
- package/dist/shared/@ai-setting/roy-agent-core-2hqxnaf3.js +851 -0
- package/dist/shared/@ai-setting/roy-agent-core-3dfq8awb.js +587 -0
- package/dist/shared/@ai-setting/roy-agent-core-3takar0s.js +93 -0
- package/dist/shared/@ai-setting/roy-agent-core-3tnb2005.js +117 -0
- package/dist/shared/@ai-setting/roy-agent-core-4vmcvkav.js +14 -0
- package/dist/shared/{chunk-25x2pdtp.js → @ai-setting/roy-agent-core-4ws8atva.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-5fbp24se.js +603 -0
- package/dist/shared/@ai-setting/roy-agent-core-5my94ywp.js +66 -0
- package/dist/shared/{chunk-rncy3rtd.js → @ai-setting/roy-agent-core-6j0zcmwk.js} +128 -1122
- package/dist/shared/@ai-setting/roy-agent-core-6w4pmxc7.js +266 -0
- package/dist/shared/{chunk-yqmx37vm.js → @ai-setting/roy-agent-core-7vrk3add.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-8dvbn7tw.js +64 -0
- package/dist/shared/@ai-setting/roy-agent-core-8mbmrwzs.js +171 -0
- package/dist/shared/@ai-setting/roy-agent-core-8wzz66qe.js +620 -0
- package/dist/shared/@ai-setting/roy-agent-core-9ykq91jc.js +762 -0
- package/dist/shared/@ai-setting/roy-agent-core-dde19zke.js +1305 -0
- package/dist/shared/@ai-setting/roy-agent-core-f7g67gce.js +913 -0
- package/dist/shared/@ai-setting/roy-agent-core-fq5mtxsy.js +341 -0
- package/dist/shared/@ai-setting/roy-agent-core-fvd9g6k8.js +1205 -0
- package/dist/shared/@ai-setting/roy-agent-core-gv1hrn3x.js +378 -0
- package/dist/shared/@ai-setting/roy-agent-core-gy0wp5h7.js +213 -0
- package/dist/shared/{chunk-mf5xqbdh.js → @ai-setting/roy-agent-core-hyza1gm7.js} +3 -2
- package/dist/shared/{chunk-1qwabsm0.js → @ai-setting/roy-agent-core-j8zx62zr.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-jb2exr0d.js +442 -0
- package/dist/shared/@ai-setting/roy-agent-core-jv3b7v9w.js +57 -0
- package/dist/shared/@ai-setting/roy-agent-core-k1rxf9ya.js +513 -0
- package/dist/shared/@ai-setting/roy-agent-core-kydc9nwb.js +60 -0
- package/dist/shared/@ai-setting/roy-agent-core-m2x48hw6.js +97 -0
- package/dist/shared/@ai-setting/roy-agent-core-m6y668cc.js +377 -0
- package/dist/shared/{chunk-1aakcfp1.js → @ai-setting/roy-agent-core-nczzf0ms.js} +3 -3
- package/dist/shared/@ai-setting/roy-agent-core-nfj6knp5.js +36 -0
- package/dist/shared/@ai-setting/roy-agent-core-ntrp979d.js +204 -0
- package/dist/shared/@ai-setting/roy-agent-core-pd7g8z5v.js +1387 -0
- package/dist/shared/@ai-setting/roy-agent-core-pzk1syce.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-q50tg9m2.js +862 -0
- package/dist/shared/{chunk-9qzt1v1p.js → @ai-setting/roy-agent-core-qg9tcaph.js} +3 -2
- package/dist/shared/{chunk-91bas8w5.js → @ai-setting/roy-agent-core-qhyerewk.js} +5 -5
- package/dist/shared/{chunk-1pf5mfgd.js → @ai-setting/roy-agent-core-qxybm159.js} +3 -3
- package/dist/shared/{chunk-g6j5n3gv.js → @ai-setting/roy-agent-core-rh9dpkpw.js} +10 -10
- package/dist/shared/{chunk-0q6s9wm6.js → @ai-setting/roy-agent-core-rr9p1g43.js} +7 -51
- package/dist/shared/@ai-setting/roy-agent-core-sbzvpfn7.js +284 -0
- package/dist/shared/@ai-setting/roy-agent-core-t22nqt4d.js +788 -0
- package/dist/shared/{chunk-t1rh6jtm.js → @ai-setting/roy-agent-core-tkr5ynkh.js} +8 -13
- package/dist/shared/@ai-setting/roy-agent-core-v4aabsf0.js +303 -0
- package/dist/shared/@ai-setting/roy-agent-core-w75rafhy.js +202 -0
- package/dist/shared/@ai-setting/roy-agent-core-w76hqkmg.js +584 -0
- package/dist/shared/@ai-setting/roy-agent-core-yfbgwes2.js +408 -0
- package/dist/shared/{chunk-a9qmy3sc.js → @ai-setting/roy-agent-core-yn761yve.js} +7 -4
- package/dist/shared/@ai-setting/roy-agent-core-yrzmn4m1.js +492 -0
- package/dist/shared/@ai-setting/roy-agent-core-yt8wdh2w.js +206 -0
- package/package.json +8 -7
- package/dist/index.d.ts +0 -7825
- package/dist/index.js +0 -16551
- package/dist/shared/chunk-hs7tbmje.js +0 -24
- /package/dist/shared/{chunk-wbkh7wat.js → @ai-setting/roy-agent-core-fs0mn2jk.js} +0 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLogger,
|
|
3
|
+
init_logger
|
|
4
|
+
} from "./roy-agent-core-yn761yve.js";
|
|
5
|
+
import {
|
|
6
|
+
__require
|
|
7
|
+
} from "./roy-agent-core-fs0mn2jk.js";
|
|
8
|
+
|
|
9
|
+
// packages/core/src/env/task/storage/sqlite-task-store.ts
|
|
10
|
+
init_logger();
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
var logger = createLogger("task:store");
|
|
13
|
+
function getDefaultDataDir() {
|
|
14
|
+
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
15
|
+
return join(home, ".local", "share");
|
|
16
|
+
}
|
|
17
|
+
function getDefaultTaskDbPath() {
|
|
18
|
+
return join(getDefaultDataDir(), "roy-agent", "tasks.db");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class SQLiteTaskStore {
|
|
22
|
+
db = null;
|
|
23
|
+
dbPath;
|
|
24
|
+
initialized = false;
|
|
25
|
+
tasksCache = new Map;
|
|
26
|
+
operationsCache = new Map;
|
|
27
|
+
constructor(dbPath) {
|
|
28
|
+
this.dbPath = dbPath || getDefaultTaskDbPath();
|
|
29
|
+
}
|
|
30
|
+
async initialize() {
|
|
31
|
+
if (this.initialized)
|
|
32
|
+
return;
|
|
33
|
+
const { Database } = __require("bun:sqlite");
|
|
34
|
+
if (this.dbPath !== ":memory:") {
|
|
35
|
+
const fs = __require("fs");
|
|
36
|
+
const path = __require("path");
|
|
37
|
+
const dir = path.dirname(this.dbPath);
|
|
38
|
+
if (!fs.existsSync(dir)) {
|
|
39
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.db = new Database(this.dbPath);
|
|
43
|
+
this.db.run(`
|
|
44
|
+
CREATE TABLE IF NOT EXISTS task (
|
|
45
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
46
|
+
title TEXT NOT NULL,
|
|
47
|
+
description TEXT DEFAULT '',
|
|
48
|
+
status TEXT DEFAULT 'todo',
|
|
49
|
+
priority TEXT DEFAULT 'medium',
|
|
50
|
+
progress INTEGER DEFAULT 0,
|
|
51
|
+
current_status TEXT DEFAULT '',
|
|
52
|
+
goals_and_expected_deliverables TEXT DEFAULT '',
|
|
53
|
+
parent_task_id INTEGER,
|
|
54
|
+
created_at INTEGER NOT NULL,
|
|
55
|
+
updated_at INTEGER NOT NULL,
|
|
56
|
+
due_date TEXT,
|
|
57
|
+
tags TEXT
|
|
58
|
+
)
|
|
59
|
+
`);
|
|
60
|
+
this.db.run(`
|
|
61
|
+
CREATE TABLE IF NOT EXISTS task_operation (
|
|
62
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
63
|
+
task_id INTEGER NOT NULL,
|
|
64
|
+
session_id TEXT NOT NULL,
|
|
65
|
+
action_type TEXT NOT NULL,
|
|
66
|
+
action_title TEXT NOT NULL,
|
|
67
|
+
action_description TEXT DEFAULT '',
|
|
68
|
+
timestamp INTEGER NOT NULL,
|
|
69
|
+
md_path TEXT,
|
|
70
|
+
FOREIGN KEY (task_id) REFERENCES task(id) ON DELETE CASCADE
|
|
71
|
+
)
|
|
72
|
+
`);
|
|
73
|
+
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_operation_task_id ON task_operation(task_id)");
|
|
74
|
+
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_operation_session_id ON task_operation(session_id)");
|
|
75
|
+
this.db.run(`
|
|
76
|
+
CREATE TABLE IF NOT EXISTS task_tag (
|
|
77
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
78
|
+
name TEXT NOT NULL UNIQUE,
|
|
79
|
+
created_at INTEGER NOT NULL,
|
|
80
|
+
task_count INTEGER DEFAULT 0
|
|
81
|
+
)
|
|
82
|
+
`);
|
|
83
|
+
this.db.run(`
|
|
84
|
+
CREATE TABLE IF NOT EXISTS task_tag_relation (
|
|
85
|
+
task_id INTEGER NOT NULL,
|
|
86
|
+
tag_id INTEGER NOT NULL,
|
|
87
|
+
created_at INTEGER NOT NULL,
|
|
88
|
+
PRIMARY KEY (task_id, tag_id),
|
|
89
|
+
FOREIGN KEY (task_id) REFERENCES task(id) ON DELETE CASCADE,
|
|
90
|
+
FOREIGN KEY (tag_id) REFERENCES task_tag(id) ON DELETE CASCADE
|
|
91
|
+
)
|
|
92
|
+
`);
|
|
93
|
+
this.db.run("CREATE INDEX IF NOT EXISTS idx_task_tag_relation_tag_id ON task_tag_relation(tag_id)");
|
|
94
|
+
this.initialized = true;
|
|
95
|
+
logger.info(`TaskStore initialized at ${this.dbPath}`);
|
|
96
|
+
}
|
|
97
|
+
async createTask(options) {
|
|
98
|
+
await this.initialize();
|
|
99
|
+
const now = Date.now();
|
|
100
|
+
const stmt = this.db.prepare(`
|
|
101
|
+
INSERT INTO task (title, description, priority, parent_task_id,
|
|
102
|
+
goals_and_expected_deliverables, due_date, tags, status,
|
|
103
|
+
current_status, created_at, updated_at)
|
|
104
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'todo', '', ?, ?)
|
|
105
|
+
`);
|
|
106
|
+
const result = stmt.run(options.title, options.description || "", options.priority || "medium", options.parent_task_id || null, options.goals_and_expected_deliverables || "", options.due_date || null, options.tags ? JSON.stringify(options.tags) : null, now, now);
|
|
107
|
+
const task = await this.getTask(result.lastInsertRowid);
|
|
108
|
+
if (task) {
|
|
109
|
+
this.tasksCache.set(task.id, task);
|
|
110
|
+
return task;
|
|
111
|
+
}
|
|
112
|
+
throw new Error("Failed to create task");
|
|
113
|
+
}
|
|
114
|
+
async getTask(id) {
|
|
115
|
+
await this.initialize();
|
|
116
|
+
if (this.tasksCache.has(id)) {
|
|
117
|
+
return this.tasksCache.get(id);
|
|
118
|
+
}
|
|
119
|
+
const stmt = this.db.prepare("SELECT * FROM task WHERE id = ?");
|
|
120
|
+
const row = stmt.get(id);
|
|
121
|
+
if (!row)
|
|
122
|
+
return;
|
|
123
|
+
const task = this.rowToTask(row);
|
|
124
|
+
this.tasksCache.set(id, task);
|
|
125
|
+
return task;
|
|
126
|
+
}
|
|
127
|
+
async updateTask(id, options) {
|
|
128
|
+
await this.initialize();
|
|
129
|
+
const task = await this.getTask(id);
|
|
130
|
+
if (!task)
|
|
131
|
+
return;
|
|
132
|
+
const updates = [];
|
|
133
|
+
const values = [];
|
|
134
|
+
if (options.title !== undefined) {
|
|
135
|
+
updates.push("title = ?");
|
|
136
|
+
values.push(options.title);
|
|
137
|
+
}
|
|
138
|
+
if (options.description !== undefined) {
|
|
139
|
+
updates.push("description = ?");
|
|
140
|
+
values.push(options.description);
|
|
141
|
+
}
|
|
142
|
+
if (options.status !== undefined) {
|
|
143
|
+
updates.push("status = ?");
|
|
144
|
+
values.push(options.status);
|
|
145
|
+
}
|
|
146
|
+
if (options.priority !== undefined) {
|
|
147
|
+
updates.push("priority = ?");
|
|
148
|
+
values.push(options.priority);
|
|
149
|
+
}
|
|
150
|
+
if (options.progress !== undefined) {
|
|
151
|
+
updates.push("progress = ?");
|
|
152
|
+
values.push(options.progress);
|
|
153
|
+
}
|
|
154
|
+
if (options.current_status !== undefined) {
|
|
155
|
+
updates.push("current_status = ?");
|
|
156
|
+
values.push(options.current_status);
|
|
157
|
+
}
|
|
158
|
+
if (options.goals_and_expected_deliverables !== undefined) {
|
|
159
|
+
updates.push("goals_and_expected_deliverables = ?");
|
|
160
|
+
values.push(options.goals_and_expected_deliverables);
|
|
161
|
+
}
|
|
162
|
+
if (options.due_date !== undefined) {
|
|
163
|
+
updates.push("due_date = ?");
|
|
164
|
+
values.push(options.due_date);
|
|
165
|
+
}
|
|
166
|
+
if (options.tags !== undefined) {
|
|
167
|
+
updates.push("tags = ?");
|
|
168
|
+
values.push(JSON.stringify(options.tags));
|
|
169
|
+
}
|
|
170
|
+
if (updates.length === 0)
|
|
171
|
+
return task;
|
|
172
|
+
updates.push("updated_at = ?");
|
|
173
|
+
values.push(Date.now());
|
|
174
|
+
values.push(id);
|
|
175
|
+
const stmt = this.db.prepare(`UPDATE task SET ${updates.join(", ")} WHERE id = ?`);
|
|
176
|
+
stmt.run(...values);
|
|
177
|
+
this.tasksCache.delete(id);
|
|
178
|
+
return this.getTask(id);
|
|
179
|
+
}
|
|
180
|
+
async deleteTask(id) {
|
|
181
|
+
await this.initialize();
|
|
182
|
+
const stmt = this.db.prepare("DELETE FROM task WHERE id = ?");
|
|
183
|
+
const result = stmt.run(id);
|
|
184
|
+
if (result.changes > 0) {
|
|
185
|
+
this.tasksCache.delete(id);
|
|
186
|
+
this.operationsCache.delete(id);
|
|
187
|
+
}
|
|
188
|
+
return result.changes > 0;
|
|
189
|
+
}
|
|
190
|
+
async listTasks(options) {
|
|
191
|
+
await this.initialize();
|
|
192
|
+
let sql = "SELECT * FROM task WHERE 1=1";
|
|
193
|
+
const params = [];
|
|
194
|
+
if (options?.status) {
|
|
195
|
+
sql += " AND status = ?";
|
|
196
|
+
params.push(options.status);
|
|
197
|
+
}
|
|
198
|
+
if (options?.priority) {
|
|
199
|
+
sql += " AND priority = ?";
|
|
200
|
+
params.push(options.priority);
|
|
201
|
+
}
|
|
202
|
+
if (options?.parent_task_id !== undefined) {
|
|
203
|
+
sql += " AND parent_task_id = ?";
|
|
204
|
+
params.push(options.parent_task_id);
|
|
205
|
+
}
|
|
206
|
+
sql += " ORDER BY updated_at DESC";
|
|
207
|
+
if (options?.limit) {
|
|
208
|
+
sql += " LIMIT ?";
|
|
209
|
+
params.push(options.limit);
|
|
210
|
+
}
|
|
211
|
+
if (options?.offset) {
|
|
212
|
+
sql += " OFFSET ?";
|
|
213
|
+
params.push(options.offset);
|
|
214
|
+
}
|
|
215
|
+
const stmt = this.db.prepare(sql);
|
|
216
|
+
const rows = stmt.all(...params);
|
|
217
|
+
return rows.map((row) => {
|
|
218
|
+
const task = this.rowToTask(row);
|
|
219
|
+
this.tasksCache.set(task.id, task);
|
|
220
|
+
return task;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async createOperation(options) {
|
|
224
|
+
await this.initialize();
|
|
225
|
+
const now = Date.now();
|
|
226
|
+
const stmt = this.db.prepare(`
|
|
227
|
+
INSERT INTO task_operation (task_id, session_id, action_type, action_title,
|
|
228
|
+
action_description, timestamp, md_path)
|
|
229
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
230
|
+
`);
|
|
231
|
+
const result = stmt.run(options.taskId, options.sessionId, options.actionType, options.actionTitle, options.actionDescription || "", now, options.mdPath || null);
|
|
232
|
+
this.operationsCache.delete(options.taskId);
|
|
233
|
+
const operation = await this.getOperation(result.lastInsertRowid);
|
|
234
|
+
if (!operation) {
|
|
235
|
+
throw new Error("Failed to create operation");
|
|
236
|
+
}
|
|
237
|
+
return operation;
|
|
238
|
+
}
|
|
239
|
+
async getOperation(id) {
|
|
240
|
+
await this.initialize();
|
|
241
|
+
const stmt = this.db.prepare("SELECT * FROM task_operation WHERE id = ?");
|
|
242
|
+
const row = stmt.get(id);
|
|
243
|
+
if (!row)
|
|
244
|
+
return;
|
|
245
|
+
return this.rowToOperation(row);
|
|
246
|
+
}
|
|
247
|
+
async updateOperation(id, updates) {
|
|
248
|
+
await this.initialize();
|
|
249
|
+
const op = await this.getOperation(id);
|
|
250
|
+
if (!op)
|
|
251
|
+
return;
|
|
252
|
+
const setClauses = [];
|
|
253
|
+
const values = [];
|
|
254
|
+
if (updates.actionTitle !== undefined) {
|
|
255
|
+
setClauses.push("action_title = ?");
|
|
256
|
+
values.push(updates.actionTitle);
|
|
257
|
+
}
|
|
258
|
+
if (updates.actionDescription !== undefined) {
|
|
259
|
+
setClauses.push("action_description = ?");
|
|
260
|
+
values.push(updates.actionDescription);
|
|
261
|
+
}
|
|
262
|
+
if (setClauses.length === 0)
|
|
263
|
+
return op;
|
|
264
|
+
const stmt = this.db.prepare(`UPDATE task_operation SET ${setClauses.join(", ")} WHERE id = ?`);
|
|
265
|
+
stmt.run(...values, id);
|
|
266
|
+
this.operationsCache.delete(op.taskId);
|
|
267
|
+
return this.getOperation(id);
|
|
268
|
+
}
|
|
269
|
+
async deleteOperation(id) {
|
|
270
|
+
await this.initialize();
|
|
271
|
+
const op = await this.getOperation(id);
|
|
272
|
+
if (!op)
|
|
273
|
+
return false;
|
|
274
|
+
const stmt = this.db.prepare("DELETE FROM task_operation WHERE id = ?");
|
|
275
|
+
const result = stmt.run(id);
|
|
276
|
+
if (result.changes > 0) {
|
|
277
|
+
this.operationsCache.delete(op.taskId);
|
|
278
|
+
}
|
|
279
|
+
return result.changes > 0;
|
|
280
|
+
}
|
|
281
|
+
async listOperations(options) {
|
|
282
|
+
await this.initialize();
|
|
283
|
+
const cacheKey = options.taskId;
|
|
284
|
+
if (!this.operationsCache.has(cacheKey)) {
|
|
285
|
+
const stmt = this.db.prepare("SELECT * FROM task_operation WHERE task_id = ? ORDER BY timestamp DESC");
|
|
286
|
+
const rows = stmt.all(options.taskId);
|
|
287
|
+
this.operationsCache.set(cacheKey, rows.map((row) => this.rowToOperation(row)));
|
|
288
|
+
}
|
|
289
|
+
let ops = this.operationsCache.get(cacheKey);
|
|
290
|
+
if (options.actionType) {
|
|
291
|
+
ops = ops.filter((op) => op.actionType === options.actionType);
|
|
292
|
+
}
|
|
293
|
+
const offset = options.offset || 0;
|
|
294
|
+
const limit = options.limit || 20;
|
|
295
|
+
return ops.slice(offset, offset + limit);
|
|
296
|
+
}
|
|
297
|
+
rowToTask(row) {
|
|
298
|
+
return {
|
|
299
|
+
id: row.id,
|
|
300
|
+
title: row.title,
|
|
301
|
+
description: row.description,
|
|
302
|
+
status: row.status,
|
|
303
|
+
priority: row.priority,
|
|
304
|
+
progress: row.progress,
|
|
305
|
+
current_status: row.current_status,
|
|
306
|
+
goals_and_expected_deliverables: row.goals_and_expected_deliverables,
|
|
307
|
+
parent_task_id: row.parent_task_id || undefined,
|
|
308
|
+
createdAt: new Date(row.created_at).toISOString(),
|
|
309
|
+
updatedAt: new Date(row.updated_at).toISOString(),
|
|
310
|
+
due_date: row.due_date || undefined,
|
|
311
|
+
tags: row.tags ? JSON.parse(row.tags) : undefined
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
rowToOperation(row) {
|
|
315
|
+
return {
|
|
316
|
+
id: row.id,
|
|
317
|
+
taskId: row.task_id,
|
|
318
|
+
sessionId: row.session_id,
|
|
319
|
+
actionType: row.action_type,
|
|
320
|
+
actionTitle: row.action_title,
|
|
321
|
+
actionDescription: row.action_description,
|
|
322
|
+
timestamp: new Date(row.timestamp).toISOString(),
|
|
323
|
+
mdPath: row.md_path || undefined
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
async createTag(name) {
|
|
327
|
+
await this.initialize();
|
|
328
|
+
const now = Date.now();
|
|
329
|
+
const existing = await this.getTagByName(name);
|
|
330
|
+
if (existing) {
|
|
331
|
+
return existing;
|
|
332
|
+
}
|
|
333
|
+
const stmt = this.db.prepare(`
|
|
334
|
+
INSERT INTO task_tag (name, created_at, task_count)
|
|
335
|
+
VALUES (?, ?, 0)
|
|
336
|
+
`);
|
|
337
|
+
const result = stmt.run(name, now);
|
|
338
|
+
return {
|
|
339
|
+
id: result.lastInsertRowid,
|
|
340
|
+
name,
|
|
341
|
+
createdAt: new Date(now).toISOString(),
|
|
342
|
+
taskCount: 0
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
async getTag(id) {
|
|
346
|
+
await this.initialize();
|
|
347
|
+
const stmt = this.db.prepare("SELECT * FROM task_tag WHERE id = ?");
|
|
348
|
+
const row = stmt.get(id);
|
|
349
|
+
if (!row)
|
|
350
|
+
return;
|
|
351
|
+
return this.rowToTag(row);
|
|
352
|
+
}
|
|
353
|
+
async getTagByName(name) {
|
|
354
|
+
await this.initialize();
|
|
355
|
+
const stmt = this.db.prepare("SELECT * FROM task_tag WHERE name = ?");
|
|
356
|
+
const row = stmt.get(name);
|
|
357
|
+
if (!row)
|
|
358
|
+
return;
|
|
359
|
+
return this.rowToTag(row);
|
|
360
|
+
}
|
|
361
|
+
async listTags(options) {
|
|
362
|
+
await this.initialize();
|
|
363
|
+
let sql = "SELECT * FROM task_tag";
|
|
364
|
+
const params = [];
|
|
365
|
+
const orderBy = options?.orderBy || "count";
|
|
366
|
+
if (orderBy === "count") {
|
|
367
|
+
sql += " ORDER BY task_count DESC";
|
|
368
|
+
} else if (orderBy === "name") {
|
|
369
|
+
sql += " ORDER BY name ASC";
|
|
370
|
+
} else {
|
|
371
|
+
sql += " ORDER BY created_at DESC";
|
|
372
|
+
}
|
|
373
|
+
if (options?.limit) {
|
|
374
|
+
sql += " LIMIT ?";
|
|
375
|
+
params.push(options.limit);
|
|
376
|
+
}
|
|
377
|
+
if (options?.offset) {
|
|
378
|
+
sql += " OFFSET ?";
|
|
379
|
+
params.push(options.offset);
|
|
380
|
+
}
|
|
381
|
+
const stmt = this.db.prepare(sql);
|
|
382
|
+
const rows = stmt.all(...params);
|
|
383
|
+
return rows.map((row) => this.rowToTag(row));
|
|
384
|
+
}
|
|
385
|
+
async searchTags(options) {
|
|
386
|
+
await this.initialize();
|
|
387
|
+
const sql = `
|
|
388
|
+
SELECT * FROM task_tag
|
|
389
|
+
WHERE name LIKE ?
|
|
390
|
+
ORDER BY task_count DESC
|
|
391
|
+
LIMIT ?
|
|
392
|
+
`;
|
|
393
|
+
const stmt = this.db.prepare(sql);
|
|
394
|
+
const rows = stmt.all(`%${options.query}%`, options.limit || 20);
|
|
395
|
+
return rows.map((row) => this.rowToTag(row));
|
|
396
|
+
}
|
|
397
|
+
async deleteTag(id) {
|
|
398
|
+
await this.initialize();
|
|
399
|
+
const stmt = this.db.prepare("DELETE FROM task_tag WHERE id = ?");
|
|
400
|
+
const result = stmt.run(id);
|
|
401
|
+
return result.changes > 0;
|
|
402
|
+
}
|
|
403
|
+
async addTagsToTask(taskId, tagIds) {
|
|
404
|
+
await this.initialize();
|
|
405
|
+
const now = Date.now();
|
|
406
|
+
const stmt = this.db.prepare(`
|
|
407
|
+
INSERT OR IGNORE INTO task_tag_relation (task_id, tag_id, created_at)
|
|
408
|
+
VALUES (?, ?, ?)
|
|
409
|
+
`);
|
|
410
|
+
for (const tagId of tagIds) {
|
|
411
|
+
stmt.run(taskId, tagId, now);
|
|
412
|
+
this.db.run(`
|
|
413
|
+
UPDATE task_tag SET task_count = task_count + 1 WHERE id = ?
|
|
414
|
+
`, tagId);
|
|
415
|
+
}
|
|
416
|
+
await this.syncTaskTagsJson(taskId);
|
|
417
|
+
}
|
|
418
|
+
async removeTagsFromTask(taskId, tagIds) {
|
|
419
|
+
await this.initialize();
|
|
420
|
+
const stmt = this.db.prepare("DELETE FROM task_tag_relation WHERE task_id = ? AND tag_id = ?");
|
|
421
|
+
for (const tagId of tagIds) {
|
|
422
|
+
stmt.run(taskId, tagId);
|
|
423
|
+
this.db.run(`
|
|
424
|
+
UPDATE task_tag SET task_count = MAX(0, task_count - 1) WHERE id = ?
|
|
425
|
+
`, tagId);
|
|
426
|
+
}
|
|
427
|
+
await this.syncTaskTagsJson(taskId);
|
|
428
|
+
}
|
|
429
|
+
async getTagsForTask(taskId) {
|
|
430
|
+
await this.initialize();
|
|
431
|
+
const sql = `
|
|
432
|
+
SELECT t.* FROM task_tag t
|
|
433
|
+
INNER JOIN task_tag_relation r ON t.id = r.tag_id
|
|
434
|
+
WHERE r.task_id = ?
|
|
435
|
+
ORDER BY t.name ASC
|
|
436
|
+
`;
|
|
437
|
+
const stmt = this.db.prepare(sql);
|
|
438
|
+
const rows = stmt.all(taskId);
|
|
439
|
+
return rows.map((row) => this.rowToTag(row));
|
|
440
|
+
}
|
|
441
|
+
async getTaskIdsByTags(tagNames) {
|
|
442
|
+
await this.initialize();
|
|
443
|
+
if (tagNames.length === 0)
|
|
444
|
+
return [];
|
|
445
|
+
const placeholders = tagNames.map(() => "?").join(", ");
|
|
446
|
+
const sql = `
|
|
447
|
+
SELECT DISTINCT r.task_id FROM task_tag_relation r
|
|
448
|
+
INNER JOIN task_tag t ON r.tag_id = t.id
|
|
449
|
+
WHERE t.name IN (${placeholders})
|
|
450
|
+
`;
|
|
451
|
+
const stmt = this.db.prepare(sql);
|
|
452
|
+
const rows = stmt.all(...tagNames);
|
|
453
|
+
return rows.map((row) => row.task_id);
|
|
454
|
+
}
|
|
455
|
+
async updateTaskTags(taskId, tagNames) {
|
|
456
|
+
await this.initialize();
|
|
457
|
+
const currentTags = await this.getTagsForTask(taskId);
|
|
458
|
+
if (currentTags.length > 0) {
|
|
459
|
+
await this.removeTagsFromTask(taskId, currentTags.map((t) => t.id));
|
|
460
|
+
}
|
|
461
|
+
const tagIds = [];
|
|
462
|
+
for (const name of tagNames) {
|
|
463
|
+
let tag = await this.getTagByName(name);
|
|
464
|
+
if (!tag) {
|
|
465
|
+
tag = await this.createTag(name);
|
|
466
|
+
}
|
|
467
|
+
tagIds.push(tag.id);
|
|
468
|
+
}
|
|
469
|
+
if (tagIds.length > 0) {
|
|
470
|
+
await this.addTagsToTask(taskId, tagIds);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async searchTasksByKeywords(keywords, options = {}) {
|
|
474
|
+
await this.initialize();
|
|
475
|
+
if (!keywords || keywords.length === 0) {
|
|
476
|
+
return [];
|
|
477
|
+
}
|
|
478
|
+
const { limit = 20, excludeTaskId } = options;
|
|
479
|
+
const tasks = new Map;
|
|
480
|
+
let taskSql = "SELECT * FROM task WHERE 1=1";
|
|
481
|
+
const taskParams = [];
|
|
482
|
+
if (excludeTaskId) {
|
|
483
|
+
taskSql += " AND id != ?";
|
|
484
|
+
taskParams.push(excludeTaskId);
|
|
485
|
+
}
|
|
486
|
+
taskSql += " ORDER BY updated_at DESC LIMIT ?";
|
|
487
|
+
taskParams.push(limit * 2);
|
|
488
|
+
const taskRows = this.db.prepare(taskSql).all(...taskParams);
|
|
489
|
+
for (const row of taskRows) {
|
|
490
|
+
const task = this.rowToTask(row);
|
|
491
|
+
let matchCount = 0;
|
|
492
|
+
if (task.title) {
|
|
493
|
+
const lowerTitle = task.title.toLowerCase();
|
|
494
|
+
for (const keyword of keywords) {
|
|
495
|
+
if (lowerTitle.includes(keyword.toLowerCase())) {
|
|
496
|
+
matchCount++;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (task.tags && Array.isArray(task.tags)) {
|
|
501
|
+
for (const keyword of keywords) {
|
|
502
|
+
const lowerKeyword = keyword.toLowerCase();
|
|
503
|
+
if (task.tags.some((tag) => tag.toLowerCase().includes(lowerKeyword))) {
|
|
504
|
+
matchCount += 2;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (task.description) {
|
|
509
|
+
const lowerDesc = task.description.toLowerCase();
|
|
510
|
+
for (const keyword of keywords) {
|
|
511
|
+
if (lowerDesc.includes(keyword.toLowerCase())) {
|
|
512
|
+
matchCount++;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (task.goals_and_expected_deliverables) {
|
|
517
|
+
const lowerGoals = task.goals_and_expected_deliverables.toLowerCase();
|
|
518
|
+
for (const keyword of keywords) {
|
|
519
|
+
if (lowerGoals.includes(keyword.toLowerCase())) {
|
|
520
|
+
matchCount++;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (matchCount > 0) {
|
|
525
|
+
tasks.set(task.id, { task, matchCount });
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const allOps = this.db.prepare("SELECT DISTINCT task_id FROM task_operation WHERE 1=1").all();
|
|
529
|
+
for (const opRow of allOps) {
|
|
530
|
+
const opSql = `
|
|
531
|
+
SELECT * FROM task_operation
|
|
532
|
+
WHERE task_id = ?
|
|
533
|
+
AND (action_title LIKE ? OR action_description LIKE ?)
|
|
534
|
+
LIMIT 10
|
|
535
|
+
`;
|
|
536
|
+
for (const keyword of keywords) {
|
|
537
|
+
const pattern = `%${keyword}%`;
|
|
538
|
+
const ops = this.db.prepare(opSql).all(opRow.task_id, pattern, pattern);
|
|
539
|
+
if (ops.length > 0) {
|
|
540
|
+
const taskRow = this.db.prepare("SELECT * FROM task WHERE id = ?").get(opRow.task_id);
|
|
541
|
+
if (taskRow) {
|
|
542
|
+
const task = this.rowToTask(taskRow);
|
|
543
|
+
if (excludeTaskId && task.id === excludeTaskId)
|
|
544
|
+
continue;
|
|
545
|
+
const existing = tasks.get(task.id);
|
|
546
|
+
if (existing) {
|
|
547
|
+
existing.matchCount += 2;
|
|
548
|
+
} else {
|
|
549
|
+
tasks.set(task.id, { task, matchCount: 2 });
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const results = Array.from(tasks.values()).sort((a, b) => b.matchCount - a.matchCount).slice(0, limit).map((item) => item.task);
|
|
556
|
+
return results;
|
|
557
|
+
}
|
|
558
|
+
async findSimilarTasksByKeywords(keywords, options = {}) {
|
|
559
|
+
const mergedOptions = { ...options, limit: options.limit ?? 5 };
|
|
560
|
+
return this.searchTasksByKeywords(keywords, mergedOptions);
|
|
561
|
+
}
|
|
562
|
+
async findSimilarTasks(taskId, limit = 5) {
|
|
563
|
+
await this.initialize();
|
|
564
|
+
const taskTags = await this.getTagsForTask(taskId);
|
|
565
|
+
if (taskTags.length === 0) {
|
|
566
|
+
return [];
|
|
567
|
+
}
|
|
568
|
+
const tagIds = taskTags.map((t) => t.id);
|
|
569
|
+
const placeholders = tagIds.map(() => "?").join(", ");
|
|
570
|
+
const sql = `
|
|
571
|
+
SELECT t.*, COUNT(r2.tag_id) as match_count
|
|
572
|
+
FROM task t
|
|
573
|
+
INNER JOIN task_tag_relation r ON t.id = r.task_id
|
|
574
|
+
INNER JOIN task_tag_relation r2 ON t.id = r2.task_id
|
|
575
|
+
WHERE r.tag_id IN (${placeholders})
|
|
576
|
+
AND t.id != ?
|
|
577
|
+
GROUP BY t.id
|
|
578
|
+
ORDER BY match_count DESC, t.updated_at DESC
|
|
579
|
+
LIMIT ?
|
|
580
|
+
`;
|
|
581
|
+
const stmt = this.db.prepare(sql);
|
|
582
|
+
const rows = stmt.all(...tagIds, taskId, limit);
|
|
583
|
+
return rows.map((row) => this.rowToTask(row));
|
|
584
|
+
}
|
|
585
|
+
async searchTasks(query, limit = 20) {
|
|
586
|
+
await this.initialize();
|
|
587
|
+
const sql = `
|
|
588
|
+
SELECT * FROM task
|
|
589
|
+
WHERE title LIKE ? OR description LIKE ?
|
|
590
|
+
ORDER BY updated_at DESC
|
|
591
|
+
LIMIT ?
|
|
592
|
+
`;
|
|
593
|
+
const stmt = this.db.prepare(sql);
|
|
594
|
+
const rows = stmt.all(`%${query}%`, `%${query}%`, limit);
|
|
595
|
+
return rows.map((row) => this.rowToTask(row));
|
|
596
|
+
}
|
|
597
|
+
rowToTag(row) {
|
|
598
|
+
return {
|
|
599
|
+
id: row.id,
|
|
600
|
+
name: row.name,
|
|
601
|
+
createdAt: new Date(row.created_at).toISOString(),
|
|
602
|
+
taskCount: row.task_count
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
async syncTaskTagsJson(taskId) {
|
|
606
|
+
const tags = await this.getTagsForTask(taskId);
|
|
607
|
+
const tagNames = tags.map((t) => t.name);
|
|
608
|
+
const stmt = this.db.prepare("UPDATE task SET tags = ? WHERE id = ?");
|
|
609
|
+
stmt.run(tagNames.length > 0 ? JSON.stringify(tagNames) : null, taskId);
|
|
610
|
+
this.tasksCache.delete(taskId);
|
|
611
|
+
}
|
|
612
|
+
async close() {
|
|
613
|
+
if (this.db) {
|
|
614
|
+
this.db.close();
|
|
615
|
+
this.db = null;
|
|
616
|
+
this.initialized = false;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
export { getDefaultDataDir, getDefaultTaskDbPath, SQLiteTaskStore };
|