forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathruntime-database.ts
More file actions
201 lines (178 loc) · 5.52 KB
/
runtime-database.ts
File metadata and controls
201 lines (178 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { join } from 'node:path';
import type { DatabaseSync } from 'node:sqlite';
import { z } from 'zod';
import type { McpToolContext } from '../tool-registry';
/**
* A simple YAML front matter parser.
*
* This function extracts the YAML block enclosed by `---` at the beginning of a string
* and parses it into a JavaScript object. It is not a full YAML parser and only
* supports simple key-value pairs and string arrays.
*
* @param content The string content to parse.
* @returns A record containing the parsed front matter data.
*/
function parseFrontmatter(content: string): Record<string, unknown> {
const match = content.match(/^---\r?\n(.*?)\r?\n---/s);
if (!match) {
return {};
}
const frontmatter = match[1];
const data: Record<string, unknown> = {};
const lines = frontmatter.split(/\r?\n/);
let currentKey = '';
let isArray = false;
const arrayValues: string[] = [];
for (const line of lines) {
const keyValueMatch = line.match(/^([^:]+):\s*(.*)/);
if (keyValueMatch) {
if (currentKey && isArray) {
data[currentKey] = arrayValues.slice();
arrayValues.length = 0;
}
const [, key, value] = keyValueMatch;
currentKey = key.trim();
isArray = value.trim() === '';
if (!isArray) {
const trimmedValue = value.trim();
if (trimmedValue === 'true') {
data[currentKey] = true;
} else if (trimmedValue === 'false') {
data[currentKey] = false;
} else {
data[currentKey] = trimmedValue;
}
}
} else {
const arrayItemMatch = line.match(/^\s*-\s*(.*)/);
if (arrayItemMatch && currentKey && isArray) {
let value = arrayItemMatch[1].trim();
// Unquote if the value is quoted.
if (
(value.startsWith("'") && value.endsWith("'")) ||
(value.startsWith('"') && value.endsWith('"'))
) {
value = value.slice(1, -1);
}
arrayValues.push(value);
}
}
}
if (currentKey && isArray) {
data[currentKey] = arrayValues;
}
return data;
}
export async function setupRuntimeExamples(
examplesPath: string,
host: McpToolContext['host'],
): Promise<DatabaseSync> {
const { DatabaseSync } = await import('node:sqlite');
const db = new DatabaseSync(':memory:');
// Create a relational table to store the structured example data.
db.exec(`
CREATE TABLE metadata (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL
);
`);
db.exec(`
INSERT INTO metadata (key, value) VALUES
('schema_version', '1'),
('created_at', '${new Date().toISOString()}');
`);
db.exec(`
CREATE TABLE examples (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
summary TEXT NOT NULL,
keywords TEXT,
required_packages TEXT,
related_concepts TEXT,
related_tools TEXT,
experimental INTEGER NOT NULL DEFAULT 0,
content TEXT NOT NULL
);
`);
// Create an FTS5 virtual table to provide full-text search capabilities.
db.exec(`
CREATE VIRTUAL TABLE examples_fts USING fts5(
title,
summary,
keywords,
required_packages,
related_concepts,
related_tools,
content,
content='examples',
content_rowid='id',
tokenize = 'porter ascii'
);
`);
// Create triggers to keep the FTS table synchronized with the examples table.
db.exec(`
CREATE TRIGGER examples_after_insert AFTER INSERT ON examples BEGIN
INSERT INTO examples_fts(rowid, title, summary, keywords, required_packages, related_concepts, related_tools, content)
VALUES (
new.id, new.title, new.summary, new.keywords, new.required_packages, new.related_concepts,
new.related_tools, new.content
);
END;
`);
const insertStatement = db.prepare(
'INSERT INTO examples(' +
'title, summary, keywords, required_packages, related_concepts, related_tools, experimental, content' +
') VALUES(?, ?, ?, ?, ?, ?, ?, ?);',
);
const frontmatterSchema = z.object({
title: z.string(),
summary: z.string(),
keywords: z.array(z.string()).optional(),
required_packages: z.array(z.string()).optional(),
related_concepts: z.array(z.string()).optional(),
related_tools: z.array(z.string()).optional(),
experimental: z.boolean().optional(),
});
db.exec('BEGIN TRANSACTION');
for await (const entry of host.glob('**/*.md', { cwd: examplesPath })) {
if (!entry.isFile()) {
continue;
}
const content = await host.readFile(join(entry.parentPath, entry.name), 'utf-8');
const frontmatter = parseFrontmatter(content);
const validation = frontmatterSchema.safeParse(frontmatter);
if (!validation.success) {
// eslint-disable-next-line no-console
console.warn(`Skipping invalid example file ${entry.name}:`, validation.error.issues);
continue;
}
const {
title,
summary,
keywords,
required_packages,
related_concepts,
related_tools,
experimental,
} = validation.data;
insertStatement.run(
title,
summary,
JSON.stringify(keywords ?? []),
JSON.stringify(required_packages ?? []),
JSON.stringify(related_concepts ?? []),
JSON.stringify(related_tools ?? []),
experimental ? 1 : 0,
content,
);
}
db.exec('END TRANSACTION');
return db;
}