| 1653 | } |
| 1654 | |
| 1655 | listTasks(opts: { status?: string; limit?: number; offset?: number; owner?: string; session?: string } = {}): { tasks: Task[]; total: number } { |
| 1656 | const conditions: string[] = []; |
| 1657 | const params: unknown[] = []; |
| 1658 | if (opts.status) { conditions.push("status = ?"); params.push(opts.status); } |
| 1659 | if (opts.session) { conditions.push("session_key = ?"); params.push(opts.session); } |
| 1660 | if (opts.owner) { |
| 1661 | conditions.push("(owner = ? OR (owner = 'public' AND id IN (SELECT task_id FROM local_shared_tasks WHERE original_owner = ?)))"); |
| 1662 | params.push(opts.owner, opts.owner); |
| 1663 | } |
| 1664 | const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : ""; |
| 1665 | |
| 1666 | const countRow = this.db.prepare(`SELECT COUNT(*) as c FROM tasks ${whereClause}`).get(...params) as { c: number }; |
| 1667 | const total = countRow.c; |
| 1668 | |
| 1669 | const limit = opts.limit ?? 50; |
| 1670 | const offset = opts.offset ?? 0; |
| 1671 | const rows = this.db.prepare( |
| 1672 | `SELECT * FROM tasks ${whereClause} ORDER BY started_at DESC LIMIT ? OFFSET ?`, |
| 1673 | ).all(...params, limit, offset) as TaskRow[]; |
| 1674 | |
| 1675 | return { tasks: rows.map(rowToTask), total }; |
| 1676 | } |
| 1677 | |
| 1678 | countChunksByTask(taskId: string): number { |
| 1679 | const row = this.db.prepare("SELECT COUNT(*) as c FROM chunks WHERE task_id = ?").get(taskId) as { c: number }; |