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