-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_edge_functions.ts
More file actions
127 lines (99 loc) · 4.41 KB
/
analyze_edge_functions.ts
File metadata and controls
127 lines (99 loc) · 4.41 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
import { walk } from "https://deno.land/std@0.168.0/fs/walk.ts";
import { join } from "https://deno.land/std@0.168.0/path/mod.ts";
const FUNCTIONS_DIR = "supabase/functions";
const REGISTRY_PATH = "supabase/functions/_shared/edgeFunctionRegistry.ts";
interface KnowledgeEntity {
name: string;
type: "tool";
description: string;
content: string; // Detailed markdown
confidence_score: number;
metadata: any;
}
// simple regexes
const REGEX_LIST_ACTIONS = /case\s+['"]list_actions['"]\s*:\s*result\s*=\s*({[\s\S]*?});/m;
const REGEX_REQ_JSON_DESTRUCT = /const\s+\{([^}]+)\}\s*=\s*(?:await\s+)?(?:req\.json\(\)|body)/;
const REGEX_BODY_DOT = /body\.([a-zA-Z0-9_]+)/g;
const REGEX_SWITCH_ACTION = /case\s+['"]([^'"]+)['"]:/g;
async function main() {
console.log("🚀 Starting Edge Function Analysis...");
// 1. Parse Registry (Text based to avoid import issues)
const registryContent = await Deno.readTextFile(REGISTRY_PATH);
// Extract objects from the array. This is hacky but sufficient.
// We look for name: '...', description: '...'
const registryMap = new Map<string, { description: string, example: string }>();
const registryRegex = /{\s*name:\s*['"]([^'"]+)['"],\s*description:\s*['"]([^'"]+)['"](?:,\s*example_use:\s*['"]([^'"]+)['"])?/g;
let match;
while ((match = registryRegex.exec(registryContent)) !== null) {
registryMap.set(match[1], {
description: match[2],
example: match[3] || "No example provided"
});
}
console.log(`📋 Found ${registryMap.size} functions in registry.`);
const knowledgeEntities: KnowledgeEntity[] = [];
for (const [name, meta] of registryMap.entries()) {
const indexPath = join(FUNCTIONS_DIR, name, "index.ts");
try {
const code = await Deno.readTextFile(indexPath);
let extraction = "**Usage Analysis:**\n";
let params = new Set<string>();
let actions = new Set<string>();
// Check for list_actions
const listActionsMatch = REGEX_LIST_ACTIONS.exec(code);
if (listActionsMatch) {
extraction += "\n**Formal Actions Definition:**\n```json\n" + listActionsMatch[1] + "\n```\n";
}
// Check for destructured params
const destructMatch = REGEX_REQ_JSON_DESTRUCT.exec(code);
if (destructMatch) {
destructMatch[1].split(',').map(s => s.trim().split(':')[0].trim()).forEach(p => params.add(p));
}
// Check for body.param usage
let bodyMatch;
while ((bodyMatch = REGEX_BODY_DOT.exec(code)) !== null) {
if (bodyMatch[1] !== 'action') params.add(bodyMatch[1]);
}
// Check for actions (switch cases)
let actionMatch;
while ((actionMatch = REGEX_SWITCH_ACTION.exec(code)) !== null) {
actions.add(actionMatch[1]);
}
if (actions.size > 0) {
extraction += `\n**Available Actions:**\n${Array.from(actions).map(a => `- \`${a}\``).join('\n')}\n`;
}
if (params.size > 0) {
extraction += `\n**Detected Parameters:**\n${Array.from(params).map(p => `- \`${p}\``).join('\n')}\n`;
}
const content = `
# ${name}
**Description:** ${meta.description}
**Example Use:**
\`\`\`json
${meta.example}
\`\`\`
${extraction}
**Source Code Analysis:**
Scanned ${code.length} bytes of code to extract usage patterns.
`.trim();
knowledgeEntities.push({
name: name,
type: "tool",
description: meta.description,
content: content,
confidence_score: 0.95,
metadata: {
source: "auto_generated",
registry_example: meta.example,
detected_actions: Array.from(actions),
detected_params: Array.from(params)
}
});
} catch (err) {
console.warn(`⚠️ Could not read ${name}: ${err.message}`);
}
}
console.log(`✅ Generated ${knowledgeEntities.length} knowledge entities.`);
await Deno.writeTextFile("edge_function_knowledge.json", JSON.stringify(knowledgeEntities, null, 2));
}
main();