-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-client.ts
More file actions
417 lines (373 loc) · 13.4 KB
/
Copy pathllm-client.ts
File metadata and controls
417 lines (373 loc) · 13.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const OpenAI = require("openai");
const Anthropic = require("@anthropic-ai/sdk");
const { normalizeConfidence } = require("./types");
const { incr } = require("./metrics");
const { logger } = require("./logger");
require("dotenv").config();
/** Sentinel when production denies mock LLM output. */
export const LLM_UNAVAILABLE = Object.freeze({
unavailable: true,
reason: "llm_unavailable",
isMock: false,
});
export class LLMClient {
openai: any;
anthropic: any;
constructor() {
this.openai = null;
this.anthropic = null;
this.initializeClients();
}
/**
* Initialize LLM clients based on available API keys
*/
initializeClients() {
const openaiApiKey = process.env.OPENAI_API_KEY;
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
const timeoutMs = Number(process.env.LLM_REQUEST_TIMEOUT_MS || 120000);
const maxRetries = Math.min(3, Math.max(0, Number(process.env.LLM_MAX_RETRIES ?? 2)));
if (openaiApiKey) {
this.openai = new OpenAI({
apiKey: openaiApiKey,
timeout: timeoutMs,
maxRetries,
});
console.log("✅ OpenAI client initialized");
}
if (anthropicApiKey) {
this.anthropic = new Anthropic({
apiKey: anthropicApiKey,
timeout: timeoutMs,
maxRetries,
});
console.log("✅ Anthropic client initialized");
}
if (!this.openai && !this.anthropic) {
console.warn("⚠️ No LLM API keys found. Using mock responses.");
}
}
/**
* Whether a live LLM provider client is configured.
* @returns {boolean}
*/
hasLiveClients() {
return Boolean(this.openai || this.anthropic);
}
/**
* Mock LLM is allowed in non-production, or when SPECSYNC_ALLOW_MOCK_LLM=true.
* In production with the flag unset/false, mock specs must not be posted.
* @returns {boolean}
*/
isMockLlmAllowed() {
if (process.env.SPECSYNC_ALLOW_MOCK_LLM === "true") {
return true;
}
return process.env.NODE_ENV !== "production";
}
/**
* Generate specification using available LLM
* @param {Object} context - Function analysis context
* @returns {Object} Generated specification, or unavailable sentinel when mock denied
*/
async generateSpec(context: any) {
try {
// Try OpenAI first, then Anthropic, then fallback to mock (if allowed)
if (this.openai) {
const result = await this.generateWithOpenAI(context);
incr("llm_live");
return result;
} else if (this.anthropic) {
const result = await this.generateWithAnthropic(context);
incr("llm_live");
return result;
} else {
return this.mockOrUnavailable(context);
}
} catch (error: unknown) {
incr("llm_failures");
logger.error({ err: error }, "Error generating spec with LLM");
return this.mockOrUnavailable(context);
}
}
/**
* Return a mock spec when allowed; otherwise the unavailable sentinel.
* @param {Object} context - Function analysis context
* @returns {Object}
*/
mockOrUnavailable(context: any) {
if (!this.isMockLlmAllowed()) {
incr("llm_failures");
return { ...LLM_UNAVAILABLE };
}
incr("llm_mock");
return this.generateMockSpec(context);
}
/**
* Generate specification using OpenAI
* @param {Object} context - Function analysis context
* @returns {Object} Generated specification
*/
async generateWithOpenAI(context: any) {
const prompt = this.buildPrompt(context);
const response = await this.openai.chat.completions.create({
model: "gpt-4",
messages: [
{
role: "system",
content:
"You are a formal specification expert. Analyze code functions and generate precise preconditions, postconditions, invariants, and edge cases. Always respond with valid JSON.",
},
{
role: "user",
content: prompt,
},
],
temperature: 0.1,
max_tokens: 2000,
});
const content = response.choices[0].message.content;
return this.parseLLMResponse(content);
}
/**
* Generate specification using Anthropic
* @param {Object} context - Function analysis context
* @returns {Object} Generated specification
*/
async generateWithAnthropic(context: any) {
const prompt = this.buildPrompt(context);
const response = await this.anthropic.messages.create({
model: "claude-3-sonnet-20240229",
max_tokens: 2000,
temperature: 0.1,
messages: [
{
role: "user",
content: prompt,
},
],
});
const content = response.content[0].text;
return this.parseLLMResponse(content);
}
/**
* Build comprehensive prompt for LLM with state-of-the-art enhancements
* @param {Object} context - Function analysis context
* @returns {string} Enhanced LLM prompt
*/
buildPrompt(context: any) {
return `You are an expert software engineer specializing in formal specification and program verification. Analyze the following ${context.language} function and generate a comprehensive formal specification using state-of-the-art techniques.
Function: ${context.functionName}
Language: ${context.language}
Function Body:
\`\`\`${context.language.toLowerCase()}
${context.functionBody}
\`\`\`
Analysis Context:
- Input Types: ${JSON.stringify(context.inputTypes, null, 2)}
- Output Type: ${context.outputType}
- Conditional Guards: ${JSON.stringify(context.conditionalGuards, null, 2)}
- Loops: ${JSON.stringify(context.loops, null, 2)}
- Early Returns: ${JSON.stringify(context.earlyReturns, null, 2)}
- Control Flow: ${context.controlFlow}
- Complexity: ${context.complexity}
- Comments: ${context.comments || "None"}
Please generate a formal specification using the following advanced analysis framework:
1. PRECONDITIONS (Input Validation & State Requirements):
- Type safety requirements
- Range and domain constraints
- Resource availability (memory, network, etc.)
- State invariants that must hold
- Security preconditions (authentication, authorization)
- Performance preconditions (rate limits, timeouts)
2. POSTCONDITIONS (Output Guarantees & Side Effects):
- Return value properties and constraints
- Side effects on global state
- Resource cleanup guarantees
- Performance guarantees (time complexity, space usage)
- Security postconditions (no information leakage)
- Exception handling guarantees
3. INVARIANTS (Data Structure & Business Logic):
- Data structure integrity constraints
- Business rule invariants
- Resource management invariants
- Thread safety guarantees (if applicable)
- Memory safety invariants
- Temporal invariants (ordering, timing)
4. EDGE CASES (Boundary Conditions & Error Scenarios):
- Boundary value conditions
- Error handling scenarios
- Performance edge cases (large inputs, timeouts)
- Security edge cases (malicious inputs, overflow)
- Concurrency edge cases (race conditions)
- Resource exhaustion scenarios
5. ADVANCED ANALYSIS:
- Time complexity analysis
- Space complexity analysis
- Security vulnerability assessment
- Thread safety analysis (if applicable)
- Memory leak potential
- API contract compliance
6. CONFIDENCE: Your confidence level as a number in [0, 1] (e.g. 0.85)
- Consider the complexity of the function
- Consider the clarity of the implementation
- Consider the presence of comments/documentation
- Consider the language-specific patterns used
Respond with ONLY valid JSON in this exact format:
{
"preconditions": ["detailed", "preconditions", "with", "reasoning"],
"postconditions": ["detailed", "postconditions", "with", "guarantees"],
"invariants": ["detailed", "invariants", "with", "context"],
"edgeCases": ["detailed", "edge", "cases", "with", "scenarios"],
"complexity": {
"time": "O(n) or specific analysis",
"space": "O(n) or specific analysis"
},
"security": {
"vulnerabilities": ["list", "of", "potential", "issues"],
"mitigations": ["list", "of", "mitigation", "strategies"]
},
"confidence": 0.85,
"reasoning": "detailed explanation of the analysis including key insights and potential concerns"
}`;
}
/**
* Parse LLM response and extract specification
* @param {string} response - LLM response
* @returns {Object} Parsed specification
*/
parseLLMResponse(response: string) {
try {
// Try to extract JSON from the response
const jsonMatch = response.match(/\{[\s\S]*\}/);
if (jsonMatch) {
const parsed = JSON.parse(jsonMatch[0]);
// Validate required fields
const required = [
"preconditions",
"postconditions",
"invariants",
"edgeCases",
"confidence",
];
const missing = required.filter((field: any) => !parsed[field]);
if (missing.length === 0) {
return {
preconditions: Array.isArray(parsed.preconditions) ? parsed.preconditions : [],
postconditions: Array.isArray(parsed.postconditions) ? parsed.postconditions : [],
invariants: Array.isArray(parsed.invariants) ? parsed.invariants : [],
edgeCases: Array.isArray(parsed.edgeCases) ? parsed.edgeCases : [],
complexity: parsed.complexity || { time: "O(1)", space: "O(1)" },
security: parsed.security || { vulnerabilities: [], mitigations: [] },
confidence: normalizeConfidence(parsed.confidence, 0.5),
reasoning: parsed.reasoning || "Generated by LLM",
isMock: false,
};
}
}
throw new Error("Invalid JSON response");
} catch (error: unknown) {
console.error("Failed to parse LLM response:", error);
console.log("Raw response:", response);
return this.mockOrUnavailable({});
}
}
/**
* Generate mock specification for testing/fallback.
* Always sets isMock: true so callers can badge or gate posting.
* @param {Object} context - Function analysis context
* @returns {Object} Mock specification
*/
generateMockSpec(context: any) {
const { inputTypes = [], conditionalGuards = [], earlyReturns = [], complexity = 1 } = context;
const preconditions = [];
const postconditions = [];
const invariants = [];
const edgeCases = [];
// Generate preconditions based on input types
for (const input of inputTypes) {
if (input.type && input.type !== "any") {
preconditions.push(`${input.name} is of type ${input.type}`);
}
if (input.required !== false) {
preconditions.push(`${input.name} is not null/undefined`);
}
}
// Generate postconditions based on early returns
for (const earlyReturn of earlyReturns) {
if (earlyReturn.value) {
postconditions.push(`Function may return ${earlyReturn.value} under certain conditions`);
}
}
// Generate invariants based on complexity
if (complexity > 5) {
invariants.push("Function maintains internal state consistency");
}
// Generate edge cases based on conditionals
for (const guard of conditionalGuards) {
edgeCases.push(`Handle case where ${guard.condition} is false`);
}
// Add generic edge cases
if (inputTypes.length > 0) {
edgeCases.push("Handle empty/null input values");
edgeCases.push("Handle boundary conditions");
}
return {
preconditions:
preconditions.length > 0 ? preconditions : ["No specific preconditions identified"],
postconditions: postconditions.length > 0 ? postconditions : ["Function completes execution"],
invariants: invariants.length > 0 ? invariants : ["No specific invariants identified"],
edgeCases: edgeCases.length > 0 ? edgeCases : ["Consider null/undefined inputs"],
complexity: {
time: complexity > 3 ? "O(n)" : "O(1)",
space: "O(1)",
},
security: {
vulnerabilities: ["Potential input validation bypass", "Possible information disclosure"],
mitigations: [
"Implement strict input validation",
"Use parameterized queries",
"Validate all inputs",
],
},
confidence: normalizeConfidence(Math.min(0.85, 1 - complexity * 0.05)),
reasoning: `Mock analysis of function with ${inputTypes.length} inputs, ${conditionalGuards.length} conditionals, and complexity ${complexity}. Advanced security and performance analysis included.`,
isMock: true,
};
}
/**
* Test LLM connectivity
* @returns {Object} Test results
*/
async testConnectivity() {
const results = {
openai: false,
anthropic: false,
mock: true,
};
if (this.openai) {
try {
await this.openai.chat.completions.create({
model: "gpt-4",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 10,
});
results.openai = true;
} catch (error: unknown) {
console.error("OpenAI test failed:", error instanceof Error ? (error instanceof Error ? error.message : String(error)) : String(error));
}
}
if (this.anthropic) {
try {
await this.anthropic.messages.create({
model: "claude-3-sonnet-20240229",
max_tokens: 10,
messages: [{ role: "user", content: "Hello" }],
});
results.anthropic = true;
} catch (error: unknown) {
console.error("Anthropic test failed:", error instanceof Error ? (error instanceof Error ? error.message : String(error)) : String(error));
}
}
return results;
}
}