@pcoliveira90/pdd 0.2.6 → 0.3.1
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/LICENSE +20 -20
- package/README.en.md +75 -60
- package/README.md +75 -26
- package/README.pt-BR.md +75 -12
- package/bin/pdd-ai.js +24 -23
- package/bin/pdd-pro.js +7 -7
- package/bin/pdd.js +8 -27
- package/package.json +43 -42
- package/src/ai/analyze-change.js +41 -41
- package/src/ai/engine.js +34 -34
- package/src/ai/run-fix-analysis.js +187 -174
- package/src/cli/doctor-command.js +140 -101
- package/src/cli/doctor-fix.js +51 -51
- package/src/cli/index.js +164 -130
- package/src/cli/init-command.js +270 -270
- package/src/cli/status-command.js +33 -33
- package/src/core/fix-runner.js +134 -135
- package/src/core/ide-detector.js +94 -94
- package/src/core/patch-generator.js +125 -126
- package/src/core/pr-manager.js +21 -21
- package/src/core/project-review-agent.js +301 -301
- package/src/core/remediation-advisor.js +91 -91
- package/src/core/state-manager.js +71 -71
- package/src/core/template-registry.js +446 -320
- package/src/core/template-upgrade.js +68 -68
- package/src/core/validator.js +38 -38
- package/src/core/worktree-guard.js +54 -0
package/src/ai/engine.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
export function getAiProviderConfig(provider = 'openai') {
|
|
2
|
-
const providers = {
|
|
3
|
-
openai: {
|
|
4
|
-
name: 'openai',
|
|
5
|
-
envKey: 'OPENAI_API_KEY',
|
|
6
|
-
baseUrlEnv: 'OPENAI_BASE_URL',
|
|
7
|
-
defaultModel: 'gpt-5'
|
|
8
|
-
},
|
|
9
|
-
claude: {
|
|
10
|
-
name: 'claude',
|
|
11
|
-
envKey: 'ANTHROPIC_API_KEY',
|
|
12
|
-
baseUrlEnv: 'ANTHROPIC_BASE_URL',
|
|
13
|
-
defaultModel: 'claude-sonnet-4-20250514'
|
|
14
|
-
},
|
|
15
|
-
openrouter: {
|
|
16
|
-
name: 'openrouter',
|
|
17
|
-
envKey: 'OPENROUTER_API_KEY',
|
|
18
|
-
baseUrlEnv: 'OPENROUTER_BASE_URL',
|
|
19
|
-
defaultModel: 'openai/gpt-4.1-mini'
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const config = providers[provider];
|
|
24
|
-
|
|
25
|
-
if (!config) {
|
|
26
|
-
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return config;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function listSupportedProviders() {
|
|
33
|
-
return ['openai', 'claude', 'openrouter'];
|
|
34
|
-
}
|
|
1
|
+
export function getAiProviderConfig(provider = 'openai') {
|
|
2
|
+
const providers = {
|
|
3
|
+
openai: {
|
|
4
|
+
name: 'openai',
|
|
5
|
+
envKey: 'OPENAI_API_KEY',
|
|
6
|
+
baseUrlEnv: 'OPENAI_BASE_URL',
|
|
7
|
+
defaultModel: 'gpt-5'
|
|
8
|
+
},
|
|
9
|
+
claude: {
|
|
10
|
+
name: 'claude',
|
|
11
|
+
envKey: 'ANTHROPIC_API_KEY',
|
|
12
|
+
baseUrlEnv: 'ANTHROPIC_BASE_URL',
|
|
13
|
+
defaultModel: 'claude-sonnet-4-20250514'
|
|
14
|
+
},
|
|
15
|
+
openrouter: {
|
|
16
|
+
name: 'openrouter',
|
|
17
|
+
envKey: 'OPENROUTER_API_KEY',
|
|
18
|
+
baseUrlEnv: 'OPENROUTER_BASE_URL',
|
|
19
|
+
defaultModel: 'openai/gpt-4.1-mini'
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const config = providers[provider];
|
|
24
|
+
|
|
25
|
+
if (!config) {
|
|
26
|
+
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return config;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function listSupportedProviders() {
|
|
33
|
+
return ['openai', 'claude', 'openrouter'];
|
|
34
|
+
}
|
|
@@ -1,174 +1,187 @@
|
|
|
1
|
-
import { buildBugfixPrompt } from './analyze-change.js';
|
|
2
|
-
import { getAiProviderConfig } from './engine.js';
|
|
3
|
-
|
|
4
|
-
function extractArgValue(args, name, fallback = null) {
|
|
5
|
-
const prefix = `${name}=`;
|
|
6
|
-
const direct = args.find(arg => arg.startsWith(prefix));
|
|
7
|
-
if (direct) return direct.slice(prefix.length);
|
|
8
|
-
|
|
9
|
-
const index = args.findIndex(arg => arg === name);
|
|
10
|
-
if (index >= 0 && args[index + 1]) return args[index + 1];
|
|
11
|
-
|
|
12
|
-
return fallback;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function getIssueFromArgs(args) {
|
|
16
|
-
const filtered = args.filter(
|
|
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
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
1
|
+
import { buildBugfixPrompt } from './analyze-change.js';
|
|
2
|
+
import { getAiProviderConfig } from './engine.js';
|
|
3
|
+
|
|
4
|
+
function extractArgValue(args, name, fallback = null) {
|
|
5
|
+
const prefix = `${name}=`;
|
|
6
|
+
const direct = args.find(arg => arg.startsWith(prefix));
|
|
7
|
+
if (direct) return direct.slice(prefix.length);
|
|
8
|
+
|
|
9
|
+
const index = args.findIndex(arg => arg === name);
|
|
10
|
+
if (index >= 0 && args[index + 1]) return args[index + 1];
|
|
11
|
+
|
|
12
|
+
return fallback;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getIssueFromArgs(args) {
|
|
16
|
+
const filtered = args.filter(
|
|
17
|
+
arg =>
|
|
18
|
+
!arg.startsWith('--provider') &&
|
|
19
|
+
!arg.startsWith('--model') &&
|
|
20
|
+
!arg.startsWith('--task') &&
|
|
21
|
+
arg !== 'fix' &&
|
|
22
|
+
arg !== '--ai'
|
|
23
|
+
);
|
|
24
|
+
return filtered.join(' ').trim();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function buildStructuredInstruction() {
|
|
28
|
+
return [
|
|
29
|
+
'Return valid JSON only.',
|
|
30
|
+
'Use exactly these keys:',
|
|
31
|
+
'{',
|
|
32
|
+
' "root_cause_hypothesis": string,',
|
|
33
|
+
' "impacted_areas": string[],',
|
|
34
|
+
' "minimal_safe_delta": string,',
|
|
35
|
+
' "regression_risks": string[],',
|
|
36
|
+
' "validation_plan": string[]',
|
|
37
|
+
'}'
|
|
38
|
+
].join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function callOpenAI({ prompt, model, apiKey, baseUrl }) {
|
|
42
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: {
|
|
45
|
+
'Content-Type': 'application/json',
|
|
46
|
+
Authorization: `Bearer ${apiKey}`
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
model,
|
|
50
|
+
temperature: 0.2,
|
|
51
|
+
messages: [
|
|
52
|
+
{ role: 'system', content: buildStructuredInstruction() },
|
|
53
|
+
{ role: 'user', content: prompt }
|
|
54
|
+
]
|
|
55
|
+
})
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const text = await response.text();
|
|
60
|
+
throw new Error(`OpenAI request failed (${response.status}): ${text}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const data = await response.json();
|
|
64
|
+
return data.choices?.[0]?.message?.content ?? '';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function callClaude({ prompt, model, apiKey, baseUrl }) {
|
|
68
|
+
const response = await fetch(`${baseUrl}/messages`, {
|
|
69
|
+
method: 'POST',
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'application/json',
|
|
72
|
+
'x-api-key': apiKey,
|
|
73
|
+
'anthropic-version': '2023-06-01'
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
model,
|
|
77
|
+
max_tokens: 1200,
|
|
78
|
+
temperature: 0.2,
|
|
79
|
+
system: buildStructuredInstruction(),
|
|
80
|
+
messages: [
|
|
81
|
+
{ role: 'user', content: prompt }
|
|
82
|
+
]
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
const text = await response.text();
|
|
88
|
+
throw new Error(`Claude request failed (${response.status}): ${text}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const data = await response.json();
|
|
92
|
+
return data.content?.[0]?.text ?? '';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function callOpenRouter({ prompt, model, apiKey, baseUrl }) {
|
|
96
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: {
|
|
99
|
+
'Content-Type': 'application/json',
|
|
100
|
+
Authorization: `Bearer ${apiKey}`,
|
|
101
|
+
'HTTP-Referer': 'https://github.com/pcoliveira90/pdd',
|
|
102
|
+
'X-Title': 'PDD CLI'
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify({
|
|
105
|
+
model,
|
|
106
|
+
temperature: 0.2,
|
|
107
|
+
messages: [
|
|
108
|
+
{ role: 'system', content: buildStructuredInstruction() },
|
|
109
|
+
{ role: 'user', content: prompt }
|
|
110
|
+
]
|
|
111
|
+
})
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
const text = await response.text();
|
|
116
|
+
throw new Error(`OpenRouter request failed (${response.status}): ${text}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const data = await response.json();
|
|
120
|
+
return data.choices?.[0]?.message?.content ?? '';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function resolveBaseUrl(providerConfig) {
|
|
124
|
+
const envValue = process.env[providerConfig.baseUrlEnv];
|
|
125
|
+
if (envValue) return envValue;
|
|
126
|
+
|
|
127
|
+
if (providerConfig.name === 'openai') return 'https://api.openai.com/v1';
|
|
128
|
+
if (providerConfig.name === 'claude') return 'https://api.anthropic.com/v1';
|
|
129
|
+
if (providerConfig.name === 'openrouter') return 'https://openrouter.ai/api/v1';
|
|
130
|
+
|
|
131
|
+
throw new Error(`Missing base URL for provider: ${providerConfig.name}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function parseJsonSafely(text) {
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(text);
|
|
137
|
+
} catch {
|
|
138
|
+
return {
|
|
139
|
+
raw_output: text
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function runAiFixAnalysis(argv = process.argv.slice(2)) {
|
|
145
|
+
const provider = extractArgValue(argv, '--provider', 'openai');
|
|
146
|
+
const providerConfig = getAiProviderConfig(provider);
|
|
147
|
+
const model = extractArgValue(argv, '--model', providerConfig.defaultModel);
|
|
148
|
+
const task = extractArgValue(argv, '--task', 'analysis');
|
|
149
|
+
const issue = getIssueFromArgs(argv);
|
|
150
|
+
|
|
151
|
+
if (!issue) {
|
|
152
|
+
throw new Error('Missing issue description. Example: pdd-ai --provider=openai --task=analysis "login not saving incomeStatus"');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const apiKey = process.env[providerConfig.envKey];
|
|
156
|
+
if (!apiKey) {
|
|
157
|
+
throw new Error(`Missing API key. Set ${providerConfig.envKey}.`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const baseUrl = resolveBaseUrl(providerConfig);
|
|
161
|
+
const prompt = [
|
|
162
|
+
`Task mode: ${task}`,
|
|
163
|
+
'',
|
|
164
|
+
buildBugfixPrompt({ issue })
|
|
165
|
+
].join('\n');
|
|
166
|
+
|
|
167
|
+
let raw;
|
|
168
|
+
if (provider === 'openai') {
|
|
169
|
+
raw = await callOpenAI({ prompt, model, apiKey, baseUrl });
|
|
170
|
+
} else if (provider === 'claude') {
|
|
171
|
+
raw = await callClaude({ prompt, model, apiKey, baseUrl });
|
|
172
|
+
} else if (provider === 'openrouter') {
|
|
173
|
+
raw = await callOpenRouter({ prompt, model, apiKey, baseUrl });
|
|
174
|
+
} else {
|
|
175
|
+
throw new Error(`Unsupported provider: ${provider}`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const parsed = parseJsonSafely(raw);
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
provider,
|
|
182
|
+
task,
|
|
183
|
+
model,
|
|
184
|
+
issue,
|
|
185
|
+
result: parsed
|
|
186
|
+
};
|
|
187
|
+
}
|