deeplyzer 0.2.0 → 0.6.0
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 +43 -2
- package/analysis/deep/assess/assessAgent.js +24 -0
- package/analysis/deep/env/environmentAgent.js +125 -0
- package/analysis/deep/generate/ganerateAgent.js +30 -0
- package/analysis/deep/orchestratorAgent.js +94 -0
- package/analysis/deep/plan/planAgent.js +26 -0
- package/analysis/static/staticAnalyzer.js +86 -0
- package/engine.js +87 -0
- package/globals.js +9 -0
- package/img/logo.png +0 -0
- package/img/workflow.png +0 -0
- package/index.js +6 -0
- package/llm/llm.js +58 -0
- package/llm/providers/claude.js +65 -0
- package/llm/providers/gemini.js +66 -0
- package/llm/providers/openai.js +91 -0
- package/open-packages.json +11 -0
- package/package.json +8 -3
- package/prompts/deep.assess +85 -0
- package/prompts/deep.env.assess +11 -0
- package/prompts/deep.env.runtime +45 -0
- package/prompts/deep.generate.output +31 -0
- package/prompts/deep.plans +153 -0
- package/prompts/graph.build_nodes +577 -0
- package/prompts/graph.environment +154 -0
- package/prompts/graph.resolve_edges +44 -0
- package/prompts/graph.source_exclusions +47 -0
- package/prompts/static.analyse +58 -0
- package/services/environmentService.js +70 -0
- package/services/graphService.js +292 -0
- package/services/workflowService.js +168 -0
- package/session/sessionManager.js +130 -0
- package/utils/execUtil.js +153 -0
- package/utils/fileUtil.js +239 -0
- package/utils/idUtil.js +17 -0
- package/utils/logger.js +93 -0
- package/utils/promptsUtil.js +29 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# ENVIRONMENT IDENTIFICATION PROMPT
|
|
2
|
+
|
|
3
|
+
You are an environment identification engine responsible for analyzing supplied project files and identifying the execution environments required to run or validate them.
|
|
4
|
+
|
|
5
|
+
## INPUT
|
|
6
|
+
|
|
7
|
+
### FILE DATA
|
|
8
|
+
***FILES***
|
|
9
|
+
|
|
10
|
+
### AVAILABLE WORKFLOW RUNNERS
|
|
11
|
+
***WORKFLOW_RUNNERS***
|
|
12
|
+
|
|
13
|
+
## 1. OBJECTIVE
|
|
14
|
+
|
|
15
|
+
Analyze all supplied files holistically and identify the minimum number of execution environments required.
|
|
16
|
+
|
|
17
|
+
Group related files that belong to the same application. Do not create separate environments unnecessarily.
|
|
18
|
+
|
|
19
|
+
Every identified environment must include the names of all supplied files applicable to that environment.
|
|
20
|
+
|
|
21
|
+
## 2. ENVIRONMENT IDENTIFICATION
|
|
22
|
+
|
|
23
|
+
For each environment, determine:
|
|
24
|
+
|
|
25
|
+
### workflowRunner
|
|
26
|
+
|
|
27
|
+
- Select the most appropriate workflow runner from the supplied runners.
|
|
28
|
+
- Evaluate all available runners before making a selection.
|
|
29
|
+
- Choose the closest suitable runner based on its name, description, and capabilities.
|
|
30
|
+
- Do not require an exact technology or framework match.
|
|
31
|
+
- Prefer specialized runners over general-purpose runners.
|
|
32
|
+
- Use the exact runner `name` supplied.
|
|
33
|
+
- Use `"dynamic"` only when no available runner is reasonably suitable.
|
|
34
|
+
- Never default to `"dynamic"` simply because execution details are incomplete.
|
|
35
|
+
|
|
36
|
+
### runtime
|
|
37
|
+
|
|
38
|
+
- Identify the most appropriate runtime and tooling required to execute the application or task.
|
|
39
|
+
- Infer suitable installable runtimes and tools even when they are not explicitly mentioned in the files.
|
|
40
|
+
- Prefer concrete runtime and package names over generic descriptions.
|
|
41
|
+
- Consider programming languages, frameworks, platforms, and execution requirements.
|
|
42
|
+
- Do not limit runtime selection to the supplied workflow runners.
|
|
43
|
+
- Avoid vague descriptions such as "appropriate tooling" or "execution environment".
|
|
44
|
+
- Include all essential runtime requirements without unnecessary dependencies.
|
|
45
|
+
|
|
46
|
+
### commands
|
|
47
|
+
|
|
48
|
+
- Identify commands required to build, start, execute, or validate the application.
|
|
49
|
+
- Commands must be executable shell commands.
|
|
50
|
+
- Include multiple commands when multiple independent processes are required.
|
|
51
|
+
- Consider execution dependencies and command order.
|
|
52
|
+
- Do not include installation commands; workflow runner installations are handled separately.
|
|
53
|
+
- Infer commands from the supplied files and application architecture.
|
|
54
|
+
|
|
55
|
+
### files
|
|
56
|
+
|
|
57
|
+
- Include all filenames applicable to the identified environment.
|
|
58
|
+
- Use the exact file names supplied in the input.
|
|
59
|
+
- Do not include file paths, directory paths, or modified filenames.
|
|
60
|
+
- Include source files, configuration files, dependency manifests, build files, and other relevant files belonging to the environment.
|
|
61
|
+
- Include files that contribute to the application's execution, configuration, build, or validation.
|
|
62
|
+
- Do not include unrelated files merely because they are located in the same directory.
|
|
63
|
+
- Do not omit files that are relevant to the environment.
|
|
64
|
+
- A file must not be assigned to an unrelated environment.
|
|
65
|
+
- A file may belong to multiple environments only when it genuinely contributes to the execution or configuration of each environment.
|
|
66
|
+
- Do not create an environment solely to accommodate a file.
|
|
67
|
+
- If multiple applications share files, include those shared files in each applicable environment.
|
|
68
|
+
|
|
69
|
+
## 3. DYNAMIC RUNNER RESTRICTION
|
|
70
|
+
|
|
71
|
+
When `workflowRunner` is `"dynamic"`:
|
|
72
|
+
|
|
73
|
+
- `commands` MUST always be an empty array.
|
|
74
|
+
- Do not generate application execution commands.
|
|
75
|
+
- Runtime identification is still required.
|
|
76
|
+
- The `files` array must still contain all files applicable to this environment.
|
|
77
|
+
|
|
78
|
+
Only generate executable commands when a predefined workflow runner is selected.
|
|
79
|
+
|
|
80
|
+
## 4. APPLICATION EXECUTION COMMANDS
|
|
81
|
+
|
|
82
|
+
When a predefined workflow runner is selected:
|
|
83
|
+
|
|
84
|
+
1. Always identify the commands required to build, start, or execute the application.
|
|
85
|
+
2. Infer standard execution commands from the supplied files, project structure, configuration, dependency manifests, and available scripts.
|
|
86
|
+
3. Prefer existing project-defined commands when available.
|
|
87
|
+
4. When explicit commands are unavailable, infer the standard execution procedure based on the identified technology and application architecture.
|
|
88
|
+
5. Include all necessary commands for independently running application components.
|
|
89
|
+
6. Include commands in their required execution order.
|
|
90
|
+
7. Do not include dependency installation commands.
|
|
91
|
+
8. Do not leave `commands` empty when execution commands can reasonably be inferred.
|
|
92
|
+
9. Return an empty array only when execution commands cannot reasonably be determined.
|
|
93
|
+
10. Do not invent application-specific scripts or unsupported execution procedures.
|
|
94
|
+
|
|
95
|
+
## 5. HOLISTIC ANALYSIS
|
|
96
|
+
|
|
97
|
+
1. Analyze all supplied files collectively.
|
|
98
|
+
2. Identify application architecture, technology stack, and execution requirements.
|
|
99
|
+
3. Distinguish source files, configuration files, dependency manifests, build files, and entry points.
|
|
100
|
+
4. Identify related and independent applications.
|
|
101
|
+
5. Group files sharing the same execution lifecycle.
|
|
102
|
+
6. Identify separate frontend, backend, worker, or standalone execution environments when necessary.
|
|
103
|
+
7. Do not create environments for configuration files that do not independently require execution.
|
|
104
|
+
8. Prefer the most suitable available workflow runner.
|
|
105
|
+
9. Infer the most appropriate installable runtime and tooling based on the task.
|
|
106
|
+
10. Do not assume that missing execution commands mean an environment cannot be identified.
|
|
107
|
+
11. Create only the minimum environments necessary.
|
|
108
|
+
12. Ensure the selected runtime and workflow runner are compatible with the identified execution requirements.
|
|
109
|
+
13. Ensure every supplied file relevant to an identified application is included in the appropriate environment's `files` array.
|
|
110
|
+
14. Do not force unrelated files into an environment to achieve complete file coverage.
|
|
111
|
+
15. Ensure the same application is not unnecessarily split into multiple environments.
|
|
112
|
+
|
|
113
|
+
## 6. OUTPUT FORMAT
|
|
114
|
+
|
|
115
|
+
Return strictly valid JSON using exactly this structure:
|
|
116
|
+
|
|
117
|
+
{
|
|
118
|
+
"environments": [
|
|
119
|
+
{
|
|
120
|
+
"workflowRunner": "runner-name-or-dynamic",
|
|
121
|
+
"runtime": "Specific runtime and tooling required",
|
|
122
|
+
"commands": [
|
|
123
|
+
"execution command"
|
|
124
|
+
],
|
|
125
|
+
"files": [
|
|
126
|
+
"filename1",
|
|
127
|
+
"filename2"
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
## 7. FINAL VALIDATION
|
|
134
|
+
|
|
135
|
+
Before responding, verify that:
|
|
136
|
+
|
|
137
|
+
- All supplied files were considered holistically.
|
|
138
|
+
- The minimum required environments were identified.
|
|
139
|
+
- The most suitable available workflow runner was selected.
|
|
140
|
+
- `"dynamic"` was used only when no supplied runner was suitable.
|
|
141
|
+
- Dynamic environments always have an empty `commands` array.
|
|
142
|
+
- Concrete, installable runtimes and tools were identified.
|
|
143
|
+
- Predefined runner environments contain the appropriate application execution commands whenever they can reasonably be inferred.
|
|
144
|
+
- Commands are in the correct execution order.
|
|
145
|
+
- No installation commands are included.
|
|
146
|
+
- No unnecessary environments were created.
|
|
147
|
+
- Every environment has a `files` array.
|
|
148
|
+
- Each `files` array contains the exact filenames applicable to that environment.
|
|
149
|
+
- No relevant supplied files were accidentally omitted.
|
|
150
|
+
- No unrelated files were included.
|
|
151
|
+
- Shared files are included in every environment where they are genuinely applicable.
|
|
152
|
+
- The output is valid JSON and contains only the specified properties.
|
|
153
|
+
|
|
154
|
+
Return only valid JSON. Do not include explanations, markdown, comments, or additional properties.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
You are an expert software dependency graph analyzer.
|
|
2
|
+
|
|
3
|
+
Identify which candidate source node, if any, is referenced by
|
|
4
|
+
the source node.
|
|
5
|
+
|
|
6
|
+
## SOURCE_NODE
|
|
7
|
+
|
|
8
|
+
***SOURCE_NODE***
|
|
9
|
+
|
|
10
|
+
This is the actual source code of the node containing the
|
|
11
|
+
external dependency.
|
|
12
|
+
|
|
13
|
+
## CANDIDATES
|
|
14
|
+
|
|
15
|
+
***CANDIDATES***
|
|
16
|
+
|
|
17
|
+
Each candidate contains:
|
|
18
|
+
- id: Unique graph node ID
|
|
19
|
+
- source: Actual source code corresponding to that node
|
|
20
|
+
|
|
21
|
+
## INSTRUCTIONS
|
|
22
|
+
|
|
23
|
+
1. Analyze the source node to understand its dependencies.
|
|
24
|
+
2. Examine each candidate's actual source code.
|
|
25
|
+
3. Identify whether the source node references or depends on
|
|
26
|
+
any candidate.
|
|
27
|
+
4. Consider programming language semantics, declarations,
|
|
28
|
+
imports, exports, function calls, class instantiation,
|
|
29
|
+
variable references and type references.
|
|
30
|
+
5. Matching names alone are not sufficient evidence.
|
|
31
|
+
6. Ignore comments, string literals and unrelated identifiers.
|
|
32
|
+
7. Do not create relationships with unrelated candidates.
|
|
33
|
+
8. If multiple candidates match, select the most appropriate one.
|
|
34
|
+
9. If no candidate represents a genuine dependency, return null.
|
|
35
|
+
10. Never invent candidate IDs.
|
|
36
|
+
|
|
37
|
+
## OUTPUT
|
|
38
|
+
|
|
39
|
+
Return ONLY valid JSON:
|
|
40
|
+
|
|
41
|
+
{
|
|
42
|
+
"selectedNodeId": "candidate-id-or-null",
|
|
43
|
+
"reason": "Brief explanation"
|
|
44
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
You are an expert Software Architect specializing in repository structure analysis.
|
|
2
|
+
|
|
3
|
+
You will receive ONLY a list of file and folder paths from a software project. You will NOT have access to source code, file contents, or implementation details.
|
|
4
|
+
|
|
5
|
+
Your task is to analyze the repository structure and identify files and folders that can safely be excluded from subsequent deep source analysis.
|
|
6
|
+
|
|
7
|
+
### ANALYSIS GUIDELINES
|
|
8
|
+
|
|
9
|
+
- Use only the supplied file and folder paths.
|
|
10
|
+
- Analyze directory hierarchy, naming conventions, file extensions, and path groupings.
|
|
11
|
+
- Identify third-party dependencies, generated files, compiled output, build artifacts, caches, temporary files, coverage reports, and other irrelevant resources.
|
|
12
|
+
- Identify directories that clearly contain non-source artifacts.
|
|
13
|
+
- Prefer directory-level exclusions when the entire directory is safe to exclude.
|
|
14
|
+
- Use paths relative to the project root.
|
|
15
|
+
- Do not exclude files merely because of their extensions.
|
|
16
|
+
- Do not assume a file is irrelevant based only on its name.
|
|
17
|
+
- Preserve application source code, business logic, configuration, scripts, tests, infrastructure definitions, and documentation that may provide meaningful architectural context.
|
|
18
|
+
- Do not exclude potentially important files when their purpose is ambiguous.
|
|
19
|
+
- Avoid broad exclusions that could unintentionally remove meaningful source code.
|
|
20
|
+
- Exclude only paths that can be confidently identified as unnecessary for subsequent deep source analysis.
|
|
21
|
+
- Do not invent paths that are not present in the supplied file or folder list.
|
|
22
|
+
|
|
23
|
+
***PATHS***
|
|
24
|
+
|
|
25
|
+
### OUTPUT REQUIREMENTS
|
|
26
|
+
|
|
27
|
+
Return ONLY a valid JSON array of strings.
|
|
28
|
+
|
|
29
|
+
Each string must represent a file or folder path to exclude, relative to the project root.
|
|
30
|
+
|
|
31
|
+
Example:
|
|
32
|
+
|
|
33
|
+
{
|
|
34
|
+
"exclusions" : [
|
|
35
|
+
"node_modules/",
|
|
36
|
+
"dist/",
|
|
37
|
+
"coverage/",
|
|
38
|
+
".cache/",
|
|
39
|
+
"package-lock.json"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
Do not include explanations, descriptions, objectives, Markdown, or code fences.
|
|
44
|
+
|
|
45
|
+
If no exclusions can be confidently identified, return an empty array:
|
|
46
|
+
|
|
47
|
+
[]
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
You are an expert software security auditor and code quality analyst.
|
|
2
|
+
|
|
3
|
+
Analyze the following workflow source code for security vulnerabilities,
|
|
4
|
+
bugs, and code quality issues.
|
|
5
|
+
|
|
6
|
+
SOURCE CODE:
|
|
7
|
+
***SOURCE_CODE***
|
|
8
|
+
|
|
9
|
+
ANALYSIS REQUIREMENTS:
|
|
10
|
+
|
|
11
|
+
1. SECURITY
|
|
12
|
+
- Injection vulnerabilities
|
|
13
|
+
- Authentication and authorization weaknesses
|
|
14
|
+
- Improper input validation
|
|
15
|
+
- Sensitive information exposure
|
|
16
|
+
- Insecure data handling
|
|
17
|
+
- Unsafe dependencies or API usage
|
|
18
|
+
- Improper access control
|
|
19
|
+
|
|
20
|
+
2. CODE QUALITY
|
|
21
|
+
- Potential bugs and incorrect logic
|
|
22
|
+
- Improper error handling
|
|
23
|
+
- Resource leaks
|
|
24
|
+
- Performance issues
|
|
25
|
+
- Code duplication
|
|
26
|
+
- Maintainability problems
|
|
27
|
+
- Unnecessary complexity
|
|
28
|
+
|
|
29
|
+
3. ANALYSIS RULES
|
|
30
|
+
- Analyze the combined workflow, not just individual functions.
|
|
31
|
+
- Consider interactions and data flow between nodes.
|
|
32
|
+
- Report only issues supported by the provided source code.
|
|
33
|
+
- Do not invent vulnerabilities or assume missing implementation details.
|
|
34
|
+
- Avoid reporting the same issue multiple times.
|
|
35
|
+
- Do not report speculative issues as confirmed vulnerabilities.
|
|
36
|
+
|
|
37
|
+
OUTPUT:
|
|
38
|
+
|
|
39
|
+
Return a JSON array of findings.
|
|
40
|
+
|
|
41
|
+
Each finding must have:
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
"id": "unique identifier",
|
|
45
|
+
"severity": "critical | high | medium | low",
|
|
46
|
+
"category": "security | quality | bug | performance",
|
|
47
|
+
"title": "Short issue title",
|
|
48
|
+
"description": "Detailed explanation",
|
|
49
|
+
"file": "Source file path",
|
|
50
|
+
"startLine": 1,
|
|
51
|
+
"endLine": 10,
|
|
52
|
+
"code": "Relevant source code",
|
|
53
|
+
"recommendation": "Suggested fix"
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
If no issues are found, return an empty array.
|
|
57
|
+
|
|
58
|
+
Return only valid JSON. Do not include markdown code fences.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const LLM = require('../llm/llm');
|
|
2
|
+
const PromptsUtil = require('../utils/promptsUtil');
|
|
3
|
+
const Logger = require('../utils/logger');
|
|
4
|
+
const globals = require("../globals");
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
|
|
8
|
+
class EnvironmentService {
|
|
9
|
+
static baseDir = path.join(process.cwd(), ".deeplyzer");
|
|
10
|
+
static environmentsFile = path.join(EnvironmentService.baseDir, "environments.json");
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.llm = new LLM(globals.config.llm);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async define() {
|
|
17
|
+
const files = globals.graphs.map(graph => graph.file).filter(Boolean);
|
|
18
|
+
let openPackageDefinitions = this._readOpenPackageDefinitions();
|
|
19
|
+
openPackageDefinitions = JSON.parse(openPackageDefinitions);
|
|
20
|
+
globals.openPackages = openPackageDefinitions;
|
|
21
|
+
|
|
22
|
+
const definitions = openPackageDefinitions.map(item => ({ name: item.name, description: item.description }));
|
|
23
|
+
|
|
24
|
+
let prompt = PromptsUtil.read("graph.environment");
|
|
25
|
+
prompt = prompt.replaceAll("***FILES***", JSON.stringify(files));
|
|
26
|
+
prompt = prompt.replaceAll("***WORKFLOW_RUNNERS***", JSON.stringify(definitions));
|
|
27
|
+
Logger.detailed("Define Environments Prompt: " + prompt);
|
|
28
|
+
|
|
29
|
+
const response = await this.llm.generate({ prompt: prompt, json: true });
|
|
30
|
+
Logger.detailed("Define Environments: " + JSON.stringify(response));
|
|
31
|
+
|
|
32
|
+
this._savEnvironments(response.environments);
|
|
33
|
+
|
|
34
|
+
return response.environments;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_readOpenPackageDefinitions() {
|
|
38
|
+
const filePath = path.join(__dirname, '..', 'open-packages.json');
|
|
39
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
40
|
+
|
|
41
|
+
return content;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
loadEnvironments() {
|
|
45
|
+
if (!fs.existsSync(EnvironmentService.environmentsFile)) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(
|
|
51
|
+
fs.readFileSync(EnvironmentService.environmentsFile, "utf8")
|
|
52
|
+
);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(`Failed to load environments: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
_savEnvironments(environments) {
|
|
59
|
+
fs.mkdirSync(EnvironmentService.baseDir, { recursive: true });
|
|
60
|
+
|
|
61
|
+
fs.writeFileSync(
|
|
62
|
+
EnvironmentService.environmentsFile,
|
|
63
|
+
JSON.stringify(environments, null, 4),
|
|
64
|
+
"utf8"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = EnvironmentService;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
const SessionManager = require('../session/sessionManager');
|
|
2
|
+
const FileUtil = require('../utils/fileUtil');
|
|
3
|
+
const IdUtil = require('../utils/idUtil');
|
|
4
|
+
const LLM = require("../llm/llm");
|
|
5
|
+
const PromptsUtil = require('../utils/promptsUtil');
|
|
6
|
+
const Logger = require('../utils/logger');
|
|
7
|
+
const globals = require("../globals");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
class GraphService {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.fileUtil = new FileUtil();
|
|
13
|
+
this.llm = new LLM(globals.config.llm);
|
|
14
|
+
this.graphs = [];
|
|
15
|
+
|
|
16
|
+
this.batchSize = globals.config.batch_size ?? 10;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async buildGraphs() {
|
|
20
|
+
if(SessionManager.checkStage("build_nodes")) {
|
|
21
|
+
// clear all existing graph files
|
|
22
|
+
this.fileUtil.removeAllFiles("graph");
|
|
23
|
+
|
|
24
|
+
Logger.essential("Finding source exclusions");
|
|
25
|
+
await this._identifyExclusions();
|
|
26
|
+
|
|
27
|
+
Logger.essential("Building nodes is in-progress");
|
|
28
|
+
await this._buildNodes();
|
|
29
|
+
|
|
30
|
+
SessionManager.updateStage("build_nodes");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if(SessionManager.checkStage("connect_edges")) {
|
|
34
|
+
Logger.essential("Connecting edges");
|
|
35
|
+
await this._buildExternalEdges();
|
|
36
|
+
|
|
37
|
+
SessionManager.updateStage("connect_edges");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async _identifyExclusions() {
|
|
42
|
+
const sources = this.fileUtil.readPaths(globals.config.sources, globals.config["source_exclusions"]);
|
|
43
|
+
const folders = [...new Set(sources.map(source => path.dirname(source.path)))];
|
|
44
|
+
|
|
45
|
+
let prompt = PromptsUtil.read("graph.source_exclusions");
|
|
46
|
+
prompt = prompt.replaceAll("***PATHS***", JSON.stringify(folders));
|
|
47
|
+
Logger.detailed(prompt);
|
|
48
|
+
|
|
49
|
+
const response = await this.llm.generate({ prompt: prompt, json: true });
|
|
50
|
+
Logger.detailed(JSON.stringify(response));
|
|
51
|
+
|
|
52
|
+
this.additionalExclusions = response.exclusions;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async loadGraphs() {
|
|
56
|
+
this.graphs = [];
|
|
57
|
+
this.sources = [];
|
|
58
|
+
|
|
59
|
+
const graphFiles = this.fileUtil.readPaths([
|
|
60
|
+
path.join(this.fileUtil.baseDir, "graph")
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
for (const file of graphFiles) {
|
|
64
|
+
if (!file.filename.endsWith(".json")) continue;
|
|
65
|
+
|
|
66
|
+
const data = this.fileUtil.loadJSON(
|
|
67
|
+
path.join("graph", file.filename)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if (!data?.file || !data?.graph) continue;
|
|
71
|
+
|
|
72
|
+
this.graphs.push({
|
|
73
|
+
graphPath: path.join("graph", file.filename),
|
|
74
|
+
file: data.file,
|
|
75
|
+
graph: data.graph
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
this.sources.push(data.file);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return this.graphs;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async _buildNodes() {
|
|
85
|
+
const sourceFiles = globals.config.sources;
|
|
86
|
+
const exclusions = [...new Set([
|
|
87
|
+
...(globals.config["source_exclusions"] || []),
|
|
88
|
+
...(this.additionalExclusions)])
|
|
89
|
+
]; // merge exclusions, duplicates removed
|
|
90
|
+
|
|
91
|
+
this.sources = this.fileUtil.readPaths(sourceFiles, exclusions);
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < this.sources.length; i += this.batchSize) {
|
|
94
|
+
const batch = this.sources.slice(i, i + this.batchSize);
|
|
95
|
+
|
|
96
|
+
const results = await Promise.all(
|
|
97
|
+
batch.map(async (source) => {
|
|
98
|
+
let prompt = PromptsUtil.read("graph.build_nodes");
|
|
99
|
+
|
|
100
|
+
prompt = prompt.replaceAll("***CONTENT***", JSON.stringify(source.content));
|
|
101
|
+
prompt = prompt.replaceAll("ID-PREFIX", IdUtil.generate());
|
|
102
|
+
Logger.detailed(prompt);
|
|
103
|
+
|
|
104
|
+
const graph = await this.llm.generate({ prompt: prompt, json: true });
|
|
105
|
+
Logger.detailed(JSON.stringify(graph));
|
|
106
|
+
|
|
107
|
+
const graphPath = `./graph/${IdUtil.generate()}.json`;
|
|
108
|
+
|
|
109
|
+
source.purpose = graph.purpose;
|
|
110
|
+
const sourceJson = { file: path.basename(source.path), path: source.path, purpose: graph.purpose };
|
|
111
|
+
this.fileUtil.saveJSON(graphPath, { file: sourceJson, graph: { nodes: graph.nodes, edges: graph.edges } });
|
|
112
|
+
|
|
113
|
+
return { graphPath, file: source, graph: { nodes: graph.nodes, edges: graph.edges } };
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
this.graphs.push(...results);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async _buildExternalEdges() {
|
|
122
|
+
for (let i = 0; i < this.graphs.length; i += this.batchSize) {
|
|
123
|
+
const batch = this.graphs.slice(i, i + this.batchSize);
|
|
124
|
+
|
|
125
|
+
await Promise.all(
|
|
126
|
+
batch.map(async (sourceGraph) => {
|
|
127
|
+
const sourceFile = sourceGraph.file.path;
|
|
128
|
+
|
|
129
|
+
for (const node of sourceGraph.graph.nodes) {
|
|
130
|
+
const patterns = node.external_dependants || [];
|
|
131
|
+
|
|
132
|
+
if (!patterns.length) continue;
|
|
133
|
+
|
|
134
|
+
const matches = this._searchSources(patterns, sourceFile);
|
|
135
|
+
|
|
136
|
+
for (const match of matches) {
|
|
137
|
+
const candidates = this._findCandidateNodes(match);
|
|
138
|
+
|
|
139
|
+
if (!candidates.length) continue;
|
|
140
|
+
|
|
141
|
+
const sourceNode = {
|
|
142
|
+
id: node.id,
|
|
143
|
+
source: this._getNodeSource(sourceFile, node)
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const candidateData = candidates.map(candidate => ({
|
|
147
|
+
id: candidate.id,
|
|
148
|
+
source: this._getNodeSource(candidate.file, candidate)
|
|
149
|
+
}));
|
|
150
|
+
|
|
151
|
+
let prompt = PromptsUtil.read("graph.resolve_edges");
|
|
152
|
+
|
|
153
|
+
prompt = prompt.replaceAll(
|
|
154
|
+
"***SOURCE_NODE***",
|
|
155
|
+
JSON.stringify(sourceNode, null, 2)
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
prompt = prompt.replaceAll(
|
|
159
|
+
"***CANDIDATES***",
|
|
160
|
+
JSON.stringify(candidateData, null, 2)
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const response = await this.llm.generate({ prompt: prompt });
|
|
164
|
+
|
|
165
|
+
let result;
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
result = JSON.parse(response.text);
|
|
169
|
+
} catch {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!result?.selectedNodeId) continue;
|
|
174
|
+
|
|
175
|
+
const selected = candidates.find(
|
|
176
|
+
candidate => candidate.id === result.selectedNodeId
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
if (!selected) continue;
|
|
180
|
+
|
|
181
|
+
const exists = sourceGraph.graph.edges.some(
|
|
182
|
+
edge =>
|
|
183
|
+
edge.node1 === node.id &&
|
|
184
|
+
edge.node2 === selected.id
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
if (exists) continue;
|
|
188
|
+
|
|
189
|
+
sourceGraph.graph.edges.push({
|
|
190
|
+
node1: node.id,
|
|
191
|
+
node2: selected.id
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const sourceJson = { file: path.basename(sourceGraph.file.path), path: sourceGraph.file.path, purpose: sourceGraph.file.purpose };
|
|
197
|
+
this.fileUtil.saveJSON(sourceGraph.graphPath, {
|
|
198
|
+
file: sourceJson,
|
|
199
|
+
graph: sourceGraph.graph
|
|
200
|
+
});
|
|
201
|
+
})
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
_getNodeSource(filePath, node) {
|
|
207
|
+
const source = this.sources.find(
|
|
208
|
+
item => item.path === filePath
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
if (!source) return "";
|
|
212
|
+
|
|
213
|
+
return source.content
|
|
214
|
+
.split(/\r?\n/)
|
|
215
|
+
.slice(node.startLine - 1, node.endLine)
|
|
216
|
+
.join("\n");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
_searchSources(patterns, sourceFile) {
|
|
220
|
+
const matches = [];
|
|
221
|
+
|
|
222
|
+
for (const source of this.sources) {
|
|
223
|
+
|
|
224
|
+
// Never search the current source file
|
|
225
|
+
if (source.path === sourceFile) continue;
|
|
226
|
+
|
|
227
|
+
const lines = source.content.split(/\r?\n/);
|
|
228
|
+
|
|
229
|
+
for (let i = 0; i < lines.length; i++) {
|
|
230
|
+
for (const pattern of patterns) {
|
|
231
|
+
const regex = new RegExp(pattern, "g");
|
|
232
|
+
let match;
|
|
233
|
+
|
|
234
|
+
while ((match = regex.exec(lines[i])) !== null) {
|
|
235
|
+
matches.push({
|
|
236
|
+
file: source.path,
|
|
237
|
+
line: i + 1,
|
|
238
|
+
matchedText: match[0],
|
|
239
|
+
sourceLine: lines[i].trim()
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
if (!match[0].length) regex.lastIndex++;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return [
|
|
249
|
+
...new Map(
|
|
250
|
+
matches.map(match => [
|
|
251
|
+
`${match.file}:${match.line}:${match.matchedText}`,
|
|
252
|
+
match
|
|
253
|
+
])
|
|
254
|
+
).values()
|
|
255
|
+
];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
_findCandidateNodes(match) {
|
|
259
|
+
const candidates = [];
|
|
260
|
+
|
|
261
|
+
const source = this.sources.find(
|
|
262
|
+
item => item.path === match.file
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
if (!source) return candidates;
|
|
266
|
+
|
|
267
|
+
const lines = source.content.split(/\r?\n/);
|
|
268
|
+
|
|
269
|
+
for (const sourceGraph of this.graphs) {
|
|
270
|
+
if (sourceGraph.file.path !== match.file) continue;
|
|
271
|
+
for (const node of sourceGraph.graph.nodes) {
|
|
272
|
+
if (
|
|
273
|
+
match.line >= node.startLine &&
|
|
274
|
+
match.line <= node.endLine
|
|
275
|
+
) {
|
|
276
|
+
candidates.push({
|
|
277
|
+
...node,
|
|
278
|
+
file: sourceGraph.file.path,
|
|
279
|
+
nodeText: lines.slice(
|
|
280
|
+
node.startLine - 1,
|
|
281
|
+
node.endLine
|
|
282
|
+
).join("\n")
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return candidates;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
module.exports = GraphService;
|