Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/copilotCLITodoWorker.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { existsSync } from 'node:fs';6import { DatabaseSync } from 'node:sqlite';7import { parentPort } from 'worker_threads';8import { RcpResponseHandler, RpcRequest, RpcResponse } from '../../../../util/node/worker';910export interface TodoItem {11id: string;12title: string;13description: string;14status: 'pending' | 'in_progress' | 'done' | 'blocked';15}1617export interface TodoSqlWorkerApi {18queryTodos(dbPath: string): TodoItem[];19}2021const responseHandler = new RcpResponseHandler();2223parentPort!.on('message', (msg: RpcRequest | RpcResponse) => {24if ('fn' in msg) {25try {26const result = handleRequest(msg.fn, msg.args);27parentPort!.postMessage({ id: msg.id, res: result } satisfies RpcResponse);28} catch (err) {29parentPort!.postMessage({ id: msg.id, err } satisfies RpcResponse);30}31} else {32responseHandler.handleResponse(msg);33}34});3536function handleRequest(fn: string, args: unknown[]): unknown {37switch (fn) {38case 'queryTodos':39return queryTodos(args[0] as string);40default:41throw new Error(`Unknown function: ${fn}`);42}43}4445function queryTodos(dbPath: string): TodoItem[] {46if (!existsSync(dbPath)) {47return [];48}49let db: DatabaseSync | undefined;50try {51db = new DatabaseSync(dbPath, { open: true });52db.exec('PRAGMA busy_timeout = 2000');53// Check if the todos table exists54const tableCheck = db.prepare(55'SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'todos\''56);57const tables = tableCheck.all() as Record<string, unknown>[];58if (tables.length === 0) {59return [];60}6162const stmt = db.prepare('SELECT id, title, description, status FROM todos ORDER BY created_at ASC');63const rows = stmt.all() as Record<string, unknown>[];64return rows.map(row => ({65id: String(row.id ?? ''),66title: String(row.title ?? ''),67description: String(row.description ?? ''),68status: String(row.status ?? 'pending') as TodoItem['status'],69}));70} finally {71db?.close();72}73}747576