-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-integration.js
More file actions
352 lines (300 loc) · 8.8 KB
/
client-integration.js
File metadata and controls
352 lines (300 loc) · 8.8 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Library Documentation Client
* Fetches curated JS library documentation for LLM code generation
*
* Usage:
* const client = new LibraryDocsClient();
* await client.init(); // Load index at startup
* const docs = await client.getDocs('axios');
*/
class LibraryDocsClient {
constructor(options = {}) {
// Default to GitHub Pages URL, can be overridden
this.baseUrl = options.baseUrl || 'https://yourusername.github.io/awesome-js-libs-for-llms';
this.cache = new Map();
this.index = null;
this.quickRef = null;
}
/**
* Initialize - load library index at app startup
* Call this once when your app starts
*/
async init() {
console.log('Loading library index...');
try {
const response = await fetch(`${this.baseUrl}/index.json`);
if (!response.ok) {
throw new Error(`Failed to load index: ${response.status}`);
}
this.index = await response.json();
console.log(`✅ Loaded ${this.index.libraries.length} libraries`);
// Also load quick reference for prompt usage
const quickRefResponse = await fetch(`${this.baseUrl}/quick-reference.json`);
this.quickRef = await quickRefResponse.json();
return this.index;
} catch (error) {
console.error('Failed to initialize library docs:', error);
throw error;
}
}
/**
* Get list of all libraries (requires init() first)
*/
getLibraryList() {
if (!this.index) {
throw new Error('Client not initialized. Call init() first.');
}
return this.index.libraries;
}
/**
* Get quick reference (minimal info for prompt inclusion)
*/
getQuickReference() {
if (!this.quickRef) {
throw new Error('Client not initialized. Call init() first.');
}
return this.quickRef;
}
/**
* Get libraries by category
*/
getByCategory(category) {
if (!this.quickRef) {
throw new Error('Client not initialized. Call init() first.');
}
return this.quickRef.categories[category] || [];
}
/**
* Search libraries by name or tags
*/
search(query) {
if (!this.index) {
throw new Error('Client not initialized. Call init() first.');
}
const lowerQuery = query.toLowerCase();
return this.index.libraries.filter(lib =>
lib.name.toLowerCase().includes(lowerQuery) ||
lib.id.includes(lowerQuery) ||
lib.description.toLowerCase().includes(lowerQuery) ||
lib.tags.some(tag => tag.includes(lowerQuery))
);
}
/**
* Get library metadata by ID
*/
getLibraryMeta(libraryId) {
if (!this.index) {
throw new Error('Client not initialized. Call init() first.');
}
return this.index.libraries.find(lib => lib.id === libraryId);
}
/**
* Get full documentation for a library (markdown)
* Returns cached version if available
*/
async getDocs(libraryId) {
// Check cache first
if (this.cache.has(libraryId)) {
return this.cache.get(libraryId);
}
const meta = this.getLibraryMeta(libraryId);
if (!meta) {
throw new Error(`Library not found: ${libraryId}`);
}
try {
const response = await fetch(`${this.baseUrl}/${meta.docs.local}`);
if (!response.ok) {
throw new Error(`Failed to load docs: ${response.status}`);
}
const markdown = await response.text();
// Cache it
this.cache.set(libraryId, {
id: libraryId,
name: meta.name,
version: meta.version,
markdown,
meta
});
return this.cache.get(libraryId);
} catch (error) {
console.error(`Failed to load docs for ${libraryId}:`, error);
throw error;
}
}
/**
* Get documentation for multiple libraries at once
*/
async getBulkDocs(libraryIds) {
const results = await Promise.allSettled(
libraryIds.map(id => this.getDocs(id))
);
return results.map((result, index) => ({
id: libraryIds[index],
success: result.status === 'fulfilled',
data: result.status === 'fulfilled' ? result.value : null,
error: result.status === 'rejected' ? result.reason.message : null
}));
}
/**
* Get minimal info formatted for LLM prompt
* Returns a compact table suitable for system prompt
*/
getPromptTable(options = {}) {
if (!this.quickRef) {
throw new Error('Client not initialized. Call init() first.');
}
const { category, limit } = options;
let libs = this.quickRef.libraries;
// Filter by category if specified
if (category) {
libs = this.quickRef.categories[category] || [];
}
// Limit if specified
if (limit) {
libs = libs.slice(0, limit);
}
// Format as markdown table
let table = '| Name | Version | Category | CDN | Description |\n';
table += '|------|---------|----------|-----|-------------|\n';
libs.forEach(lib => {
table += `| ${lib.name} | ${lib.version} | ${lib.category} | [CDN](${lib.cdn}) | ${lib.description} |\n`;
});
return table;
}
/**
* Clear cache (useful if you want to force refresh)
*/
clearCache() {
this.cache.clear();
console.log('Cache cleared');
}
/**
* Get cache statistics
*/
getCacheStats() {
return {
size: this.cache.size,
keys: Array.from(this.cache.keys())
};
}
}
// Export for various module systems
if (typeof module !== 'undefined' && module.exports) {
module.exports = LibraryDocsClient;
}
if (typeof window !== 'undefined') {
window.LibraryDocsClient = LibraryDocsClient;
}
// ===== USAGE EXAMPLES =====
async function exampleUsage() {
// Initialize the client
const client = new LibraryDocsClient({
baseUrl: 'https://yourusername.github.io/awesome-js-libs-for-llms'
});
// Load index at app startup (do this once)
await client.init();
// ===== For LLM System Prompt =====
// Get minimal table for prompt (all libraries)
const promptTable = client.getPromptTable();
// Or just animation libraries
const animationTable = client.getPromptTable({
category: 'animation'
});
// Or top 20 most common
const top20 = client.getPromptTable({
limit: 20
});
// ===== For On-Demand Documentation =====
// When LLM needs full docs
const axiosDocs = await client.getDocs('axios');
console.log(axiosDocs.markdown); // Full markdown documentation
// Search for libraries
const chartLibs = client.search('chart');
console.log(chartLibs); // Returns Chart.js, D3.js, etc.
// Get by category
const sliders = client.getByCategory('slider-carousel');
console.log(sliders); // Returns Swiper, Glide.js, Slick, etc.
// Load multiple docs at once
const docs = await client.getBulkDocs(['axios', 'chart-js', 'gsap']);
docs.forEach(doc => {
if (doc.success) {
console.log(`Loaded: ${doc.data.name}`);
}
});
}
// ===== INTEGRATION WITH LLM TOOLS =====
/**
* Tool definition for Claude/GPT function calling
*/
const libraryDocsTools = [
{
name: "search_libraries",
description: "Search JavaScript libraries by name, category, or use case",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "Search term (name, category, or keyword)"
}
},
required: ["query"]
}
},
{
name: "get_library_docs",
description: "Get detailed documentation for a specific library",
parameters: {
type: "object",
properties: {
library_id: {
type: "string",
description: "Library ID (e.g., 'axios', 'chart-js')"
}
},
required: ["library_id"]
}
},
{
name: "get_libraries_by_category",
description: "Get all libraries in a specific category",
parameters: {
type: "object",
properties: {
category: {
type: "string",
enum: [
"http", "animation", "charts", "ui-framework",
"slider-carousel", "forms", "maps", "media",
"utilities", "3d-graphics", "touch-gestures",
"notifications", "date-time", "drag-drop",
"modals-alerts", "lazy-loading", "syntax-highlighting",
"lightbox-gallery", "scrolling", "validation"
]
}
},
required: ["category"]
}
}
];
/**
* Tool handler functions
*/
const client = new LibraryDocsClient();
async function handleToolCall(toolName, parameters) {
// Make sure client is initialized
if (!client.index) {
await client.init();
}
switch (toolName) {
case "search_libraries":
return client.search(parameters.query);
case "get_library_docs":
const docs = await client.getDocs(parameters.library_id);
return docs.markdown;
case "get_libraries_by_category":
return client.getByCategory(parameters.category);
default:
throw new Error(`Unknown tool: ${toolName}`);
}
}