-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
130 lines (107 loc) · 3.14 KB
/
handler.js
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
const LRU = require('lru-cache');
const fs = require('fs');
const path = require('path');
// Load processed prompts
const promptsData = JSON.parse(fs.readFileSync(path.join(__dirname, 'prompts.json'), 'utf8'));
class PromptGenius {
constructor() {
// LRU cache for performance optimization
this.promptCache = new LRU({
max: 500,
ttl: 1000 * 60 * 60 // 1 hour cache
});
// Indexed prompts for quick search
this.promptIndex = this.buildPromptIndex(promptsData.prompts);
}
// Build searchable index of prompts
buildPromptIndex(prompts) {
const index = {
byCategory: {},
byTag: {},
byName: {}
};
prompts.forEach(prompt => {
// Index by category
if (!index.byCategory[prompt.category]) {
index.byCategory[prompt.category] = [];
}
index.byCategory[prompt.category].push(prompt);
// Index by tags
prompt.metadata.tags.forEach(tag => {
if (!index.byTag[tag]) {
index.byTag[tag] = [];
}
index.byTag[tag].push(prompt);
});
// Index by name
index.byName[prompt.id] = prompt;
});
return index;
}
// Context-aware prompt retrieval
getContextualPrompts(context = {}) {
const { category, tags, complexity } = context;
let results = promptsData.prompts;
if (category) {
results = results.filter(p => p.category === category);
}
if (tags && tags.length) {
results = results.filter(p =>
tags.some(tag => p.metadata.tags.includes(tag))
);
}
if (complexity) {
results = results.filter(p => p.metadata.complexity === complexity);
}
return results;
}
// Dynamic prompt generation with placeholder replacement
generatePrompt(promptId, input = '', additionalContext = {}) {
const prompt = this.promptIndex.byName[promptId];
if (!prompt) {
throw new Error(`Prompt ${promptId} not found`);
}
// Replace input placeholder
let processedPrompt = prompt.prompt.replace('{input}', input);
// Add additional context if provided
if (additionalContext.context) {
processedPrompt += `\n\nAdditional Context: ${additionalContext.context}`;
}
return {
id: prompt.id,
name: prompt.name,
category: prompt.category,
processedPrompt
};
}
// Search prompts
searchPrompts(query) {
const lowercaseQuery = query.toLowerCase();
return promptsData.prompts.filter(prompt =>
prompt.name.toLowerCase().includes(lowercaseQuery) ||
prompt.metadata.tags.some(tag => tag.includes(lowercaseQuery))
);
}
}
module.exports = {
runtime: {
handler: function({
action,
prompt_id,
user_input,
context
}) {
const promptGenius = new PromptGenius();
switch(action) {
case 'generate':
return promptGenius.generatePrompt(prompt_id, user_input, context);
case 'search':
return promptGenius.searchPrompts(user_input);
case 'contextual':
return promptGenius.getContextualPrompts(context);
default:
throw new Error('Invalid action');
}
}
}
};