@flisk/analyze-tracking 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/bin/cli.js +30 -2
- package/package.json +12 -8
- package/src/analyze/go/astTraversal.js +121 -0
- package/src/analyze/go/constants.js +20 -0
- package/src/analyze/go/eventDeduplicator.js +47 -0
- package/src/analyze/go/eventExtractor.js +156 -0
- package/src/analyze/go/goAstParser/constants.js +39 -0
- package/src/analyze/go/goAstParser/expressionParser.js +281 -0
- package/src/analyze/go/goAstParser/index.js +52 -0
- package/src/analyze/go/goAstParser/statementParser.js +387 -0
- package/src/analyze/go/goAstParser/tokenizer.js +196 -0
- package/src/analyze/go/goAstParser/typeParser.js +202 -0
- package/src/analyze/go/goAstParser/utils.js +99 -0
- package/src/analyze/go/index.js +55 -0
- package/src/analyze/go/propertyExtractor.js +670 -0
- package/src/analyze/go/trackingDetector.js +71 -0
- package/src/analyze/go/trackingExtractor.js +54 -0
- package/src/analyze/go/typeContext.js +88 -0
- package/src/analyze/go/utils.js +215 -0
- package/src/analyze/index.js +11 -6
- package/src/analyze/javascript/constants.js +115 -0
- package/src/analyze/javascript/detectors/analytics-source.js +119 -0
- package/src/analyze/javascript/detectors/index.js +10 -0
- package/src/analyze/javascript/extractors/event-extractor.js +179 -0
- package/src/analyze/javascript/extractors/index.js +13 -0
- package/src/analyze/javascript/extractors/property-extractor.js +172 -0
- package/src/analyze/javascript/index.js +38 -0
- package/src/analyze/javascript/parser.js +126 -0
- package/src/analyze/javascript/utils/function-finder.js +123 -0
- package/src/analyze/python/index.js +111 -0
- package/src/analyze/python/pythonTrackingAnalyzer.py +814 -0
- package/src/analyze/ruby/detectors.js +46 -0
- package/src/analyze/ruby/extractors.js +258 -0
- package/src/analyze/ruby/index.js +51 -0
- package/src/analyze/ruby/traversal.js +123 -0
- package/src/analyze/ruby/types.js +30 -0
- package/src/analyze/ruby/visitor.js +66 -0
- package/src/analyze/typescript/constants.js +109 -0
- package/src/analyze/typescript/detectors/analytics-source.js +125 -0
- package/src/analyze/typescript/detectors/index.js +10 -0
- package/src/analyze/typescript/extractors/event-extractor.js +269 -0
- package/src/analyze/typescript/extractors/index.js +14 -0
- package/src/analyze/typescript/extractors/property-extractor.js +427 -0
- package/src/analyze/typescript/index.js +48 -0
- package/src/analyze/typescript/parser.js +131 -0
- package/src/analyze/typescript/utils/function-finder.js +139 -0
- package/src/analyze/typescript/utils/type-resolver.js +208 -0
- package/src/generateDescriptions/index.js +81 -0
- package/src/generateDescriptions/llmUtils.js +33 -0
- package/src/generateDescriptions/promptUtils.js +62 -0
- package/src/generateDescriptions/schemaUtils.js +61 -0
- package/src/index.js +13 -4
- package/src/{fileProcessor.js → utils/fileProcessor.js} +5 -0
- package/src/{repoDetails.js → utils/repoDetails.js} +5 -0
- package/src/utils/yamlGenerator.js +47 -0
- package/src/analyze/analyzeGoFile.js +0 -1164
- package/src/analyze/analyzeJsFile.js +0 -87
- package/src/analyze/analyzePythonFile.js +0 -42
- package/src/analyze/analyzeRubyFile.js +0 -419
- package/src/analyze/analyzeTsFile.js +0 -192
- package/src/analyze/go2json.js +0 -1069
- package/src/analyze/helpers.js +0 -656
- package/src/analyze/pythonTrackingAnalyzer.py +0 -541
- package/src/generateDescriptions.js +0 -196
- package/src/yamlGenerator.js +0 -23
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const { z } = require('zod');
|
|
4
|
-
const { PromptTemplate } = require('@langchain/core/prompts');
|
|
5
|
-
|
|
6
|
-
function createPrompt(eventName, properties, implementations, codebaseDir) {
|
|
7
|
-
let prompt = `Event Name: "${eventName}"\n\n`;
|
|
8
|
-
prompt += `Properties:\n`;
|
|
9
|
-
|
|
10
|
-
function appendPropertiesToPrompt(properties, indent = '') {
|
|
11
|
-
for (const propName in properties) {
|
|
12
|
-
const prop = properties[propName];
|
|
13
|
-
prompt += `${indent}- "${propName}" (type: ${prop.type})\n`;
|
|
14
|
-
if (prop.properties) {
|
|
15
|
-
prompt += `${indent} Sub-properties:\n`;
|
|
16
|
-
appendPropertiesToPrompt(prop.properties, indent + ' ');
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
appendPropertiesToPrompt(properties);
|
|
22
|
-
|
|
23
|
-
// Add implementations with code snippets
|
|
24
|
-
prompt += `\nImplementations:\n`;
|
|
25
|
-
for (const impl of implementations) {
|
|
26
|
-
const codeSnippet = getCodeSnippet(path.join(codebaseDir, impl.path), impl.line);
|
|
27
|
-
prompt += `- Path: "${impl.path}", Line: ${impl.line}, Function: "${impl.function}", Destination: "${impl.destination}"\n`;
|
|
28
|
-
prompt += `Code Snippet:\n`;
|
|
29
|
-
prompt += '```\n';
|
|
30
|
-
prompt += codeSnippet + '\n';
|
|
31
|
-
prompt += '```\n';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return prompt;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getCodeSnippet(filePath, lineNumber, contextLines = 5) {
|
|
38
|
-
// Extract a code snippet from the file around the specified line
|
|
39
|
-
try {
|
|
40
|
-
const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
41
|
-
const lines = fileContent.split('\n');
|
|
42
|
-
const startLine = Math.max(0, lineNumber - contextLines - 1);
|
|
43
|
-
const endLine = Math.min(lines.length, lineNumber + contextLines);
|
|
44
|
-
|
|
45
|
-
const snippetLines = lines.slice(startLine, endLine);
|
|
46
|
-
return snippetLines.join('\n');
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.error(`Failed to read file ${filePath}:`, e);
|
|
49
|
-
return '';
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function createEventDescriptionSchema(properties) {
|
|
54
|
-
function buildPropertySchema(prop) {
|
|
55
|
-
if (prop.properties) {
|
|
56
|
-
const subPropertiesSchema = {};
|
|
57
|
-
for (const subPropName in prop.properties) {
|
|
58
|
-
subPropertiesSchema[subPropName] = buildPropertySchema(prop.properties[subPropName]);
|
|
59
|
-
}
|
|
60
|
-
return z.object({
|
|
61
|
-
description: z
|
|
62
|
-
.string()
|
|
63
|
-
.describe('A maximum of 10 words describing the property and what it means'),
|
|
64
|
-
properties: z.object(subPropertiesSchema),
|
|
65
|
-
});
|
|
66
|
-
} else {
|
|
67
|
-
return z.object({
|
|
68
|
-
description: z
|
|
69
|
-
.string()
|
|
70
|
-
.describe('A maximum of 10 words describing the property and what it means'),
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Define the schema for properties
|
|
76
|
-
const propertiesSchema = {};
|
|
77
|
-
for (const propName in properties) {
|
|
78
|
-
propertiesSchema[propName] = buildPropertySchema(properties[propName]);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Define the schema for implementations
|
|
82
|
-
const implementationsSchema = z.array(
|
|
83
|
-
z.object({
|
|
84
|
-
description: z
|
|
85
|
-
.string()
|
|
86
|
-
.describe('A maximum of 10 words describing how this event is triggered without using the word "triggered"'),
|
|
87
|
-
path: z.string(),
|
|
88
|
-
line: z.number(),
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
// Construct the full schema
|
|
93
|
-
const eventDescriptionSchema = z.object({
|
|
94
|
-
eventDescription: z
|
|
95
|
-
.string()
|
|
96
|
-
.describe('A maximum of 10 words describing the event and what it tracks without using the word "tracks"'),
|
|
97
|
-
properties: z.object(propertiesSchema),
|
|
98
|
-
implementations: implementationsSchema,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return eventDescriptionSchema;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async function sendPromptToLLM(prompt, schema, model) {
|
|
105
|
-
try {
|
|
106
|
-
const promptTemplate = new PromptTemplate({
|
|
107
|
-
template: `You are an expert at structured data extraction. Generate detailed descriptions for the following analytics event, its properties, and implementations.\n{input}`,
|
|
108
|
-
inputVariables: ['input'],
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const formattedPrompt = await promptTemplate.format({
|
|
112
|
-
input: prompt,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
const structuredModel = model.withStructuredOutput(schema);
|
|
116
|
-
const response = await structuredModel.invoke(formattedPrompt);
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
descriptions: response,
|
|
120
|
-
};
|
|
121
|
-
} catch (error) {
|
|
122
|
-
console.error('Error during LLM response parsing:', error);
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function generateEventDescription(eventName, event, codebaseDir, model) {
|
|
128
|
-
const properties = event.properties || {};
|
|
129
|
-
const implementations = event.implementations || [];
|
|
130
|
-
|
|
131
|
-
// Create prompt for the LLM
|
|
132
|
-
const prompt = createPrompt(eventName, properties, implementations, codebaseDir);
|
|
133
|
-
|
|
134
|
-
// Define the output schema using Zod
|
|
135
|
-
const eventDescriptionSchema = createEventDescriptionSchema(properties);
|
|
136
|
-
|
|
137
|
-
// Send prompt to the LLM and get the structured response
|
|
138
|
-
const { descriptions } = await sendPromptToLLM(prompt, eventDescriptionSchema, model);
|
|
139
|
-
|
|
140
|
-
return { eventName, descriptions };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function generateDescriptions(events, codebaseDir, model) {
|
|
144
|
-
const eventPromises = Object.entries(events).map(([eventName, event]) =>
|
|
145
|
-
generateEventDescription(eventName, event, codebaseDir, model)
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
console.log(`Running ${eventPromises.length} prompts in parallel...`);
|
|
149
|
-
|
|
150
|
-
const results = await Promise.all(eventPromises);
|
|
151
|
-
|
|
152
|
-
// Process results and update the events object
|
|
153
|
-
results.forEach(({ eventName, descriptions }) => {
|
|
154
|
-
if (descriptions) {
|
|
155
|
-
const event = events[eventName];
|
|
156
|
-
event.description = descriptions.eventDescription;
|
|
157
|
-
|
|
158
|
-
// Update property descriptions recursively
|
|
159
|
-
function updatePropertyDescriptions(eventProperties, descriptionProperties) {
|
|
160
|
-
for (const propName in descriptionProperties) {
|
|
161
|
-
if (eventProperties[propName]) {
|
|
162
|
-
eventProperties[propName].description = descriptionProperties[propName].description;
|
|
163
|
-
if (eventProperties[propName].properties && descriptionProperties[propName].properties) {
|
|
164
|
-
updatePropertyDescriptions(
|
|
165
|
-
eventProperties[propName].properties,
|
|
166
|
-
descriptionProperties[propName].properties
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
updatePropertyDescriptions(event.properties, descriptions.properties);
|
|
174
|
-
|
|
175
|
-
// Update implementations with descriptions
|
|
176
|
-
for (let i = 0; i < descriptions.implementations.length; i++) {
|
|
177
|
-
if (event.implementations[i]) {
|
|
178
|
-
if (
|
|
179
|
-
event.implementations[i].path === descriptions.implementations[i].path &&
|
|
180
|
-
event.implementations[i].line === descriptions.implementations[i].line
|
|
181
|
-
) {
|
|
182
|
-
event.implementations[i].description = descriptions.implementations[i].description;
|
|
183
|
-
} else {
|
|
184
|
-
console.error(`Returned implementation description does not match path or line for event: ${eventName}`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
} else {
|
|
189
|
-
console.error(`Failed to get description for event: ${eventName}`);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
return events;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
module.exports = { generateDescriptions };
|
package/src/yamlGenerator.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const yaml = require('js-yaml');
|
|
3
|
-
|
|
4
|
-
const VERSION = 1
|
|
5
|
-
const SCHEMA_URL = "https://raw.githubusercontent.com/fliskdata/analyze-tracking/main/schema.json";
|
|
6
|
-
|
|
7
|
-
function generateYamlSchema(events, repository, outputPath) {
|
|
8
|
-
const schema = {
|
|
9
|
-
version: VERSION,
|
|
10
|
-
source: repository,
|
|
11
|
-
events,
|
|
12
|
-
};
|
|
13
|
-
const options = {
|
|
14
|
-
noRefs: true,
|
|
15
|
-
lineWidth: -1,
|
|
16
|
-
};
|
|
17
|
-
const yamlOutput = yaml.dump(schema, options);
|
|
18
|
-
const yamlFile = `# yaml-language-server: $schema=${SCHEMA_URL}\n${yamlOutput}`;
|
|
19
|
-
fs.writeFileSync(outputPath, yamlFile, 'utf8');
|
|
20
|
-
console.log(`Tracking schema YAML file generated: ${outputPath}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = { generateYamlSchema };
|