@intend-it/core 2.0.6 → 3.0.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/dist/index.js +157 -35
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -168837,7 +168837,13 @@ class TypeScriptGenerator {
|
|
|
168837
168837
|
}
|
|
168838
168838
|
generateFunctionSignature(intent) {
|
|
168839
168839
|
const params = intent.parameters.map((p) => `${p.name}: ${p.type}`).join(", ");
|
|
168840
|
-
|
|
168840
|
+
let returnType = intent.returnType.base;
|
|
168841
|
+
if (intent.returnType.generic) {
|
|
168842
|
+
returnType += `<${intent.returnType.generic}>`;
|
|
168843
|
+
}
|
|
168844
|
+
if (intent.returnType.isArray) {
|
|
168845
|
+
returnType += "[]";
|
|
168846
|
+
}
|
|
168841
168847
|
const isAsync = returnType.startsWith("Promise");
|
|
168842
168848
|
const asyncKeyword = isAsync ? "async " : "";
|
|
168843
168849
|
return `export ${asyncKeyword}function ${intent.name}(${params}): ${returnType} {`;
|
|
@@ -169110,7 +169116,7 @@ ${request.prompt}`;
|
|
|
169110
169116
|
// src/ai/prompt-builder.ts
|
|
169111
169117
|
class PromptBuilder {
|
|
169112
169118
|
buildContext(intent, fileImports, importedIntents = []) {
|
|
169113
|
-
const returnType =
|
|
169119
|
+
const returnType = this.formatReturnType(intent.returnType);
|
|
169114
169120
|
return {
|
|
169115
169121
|
functionName: intent.name,
|
|
169116
169122
|
parameters: intent.parameters,
|
|
@@ -169122,25 +169128,6 @@ class PromptBuilder {
|
|
|
169122
169128
|
importedIntents
|
|
169123
169129
|
};
|
|
169124
169130
|
}
|
|
169125
|
-
buildFullPrompt(context) {
|
|
169126
|
-
let contextStr = "";
|
|
169127
|
-
if (context.importedIntents && context.importedIntents.length > 0) {
|
|
169128
|
-
contextStr += `
|
|
169129
|
-
|
|
169130
|
-
IMPORTED DEFINITIONS (Strictly adhere to these contracts):
|
|
169131
|
-
`;
|
|
169132
|
-
context.importedIntents.forEach((imp) => {
|
|
169133
|
-
const params = imp.parameters.map((p) => `${p.name}: ${p.type}`).join(", ");
|
|
169134
|
-
contextStr += `- Function ${imp.name}(${params}) -> ${imp.returnType.base}
|
|
169135
|
-
`;
|
|
169136
|
-
if (imp.body.invariants.length > 0) {
|
|
169137
|
-
imp.body.invariants.forEach((inv) => contextStr += ` Invariant: ${inv.constraint}
|
|
169138
|
-
`);
|
|
169139
|
-
}
|
|
169140
|
-
});
|
|
169141
|
-
}
|
|
169142
|
-
return `...` + contextStr;
|
|
169143
|
-
}
|
|
169144
169131
|
buildSystemPrompt() {
|
|
169145
169132
|
return `You are an expert TypeScript developer specializing in clean, production-ready code.
|
|
169146
169133
|
|
|
@@ -169225,7 +169212,7 @@ Generate ONLY the TypeScript code for this step (no explanations, no markdown).`
|
|
|
169225
169212
|
lines.push(`IMPORTED DEFINITIONS (Strictly adhere to these contracts):`);
|
|
169226
169213
|
context.importedIntents.forEach((imp) => {
|
|
169227
169214
|
const params = this.formatParameters(imp.parameters);
|
|
169228
|
-
const returnType =
|
|
169215
|
+
const returnType = this.formatReturnType(imp.returnType);
|
|
169229
169216
|
lines.push(`- Function ${imp.name}(${params}) -> ${returnType}`);
|
|
169230
169217
|
if (imp.body.invariants.length > 0) {
|
|
169231
169218
|
imp.body.invariants.forEach((inv) => lines.push(` Invariant: ${inv.constraint}`));
|
|
@@ -169272,6 +169259,73 @@ Return ONLY the function body code (no function signature, no markdown).`);
|
|
|
169272
169259
|
return "none";
|
|
169273
169260
|
return params.map((p) => `${p.name}: ${p.type}`).join(", ");
|
|
169274
169261
|
}
|
|
169262
|
+
formatReturnType(rt) {
|
|
169263
|
+
let type = rt.base;
|
|
169264
|
+
if (rt.generic) {
|
|
169265
|
+
type += `<${rt.generic}>`;
|
|
169266
|
+
}
|
|
169267
|
+
if (rt.isArray) {
|
|
169268
|
+
type += "[]";
|
|
169269
|
+
}
|
|
169270
|
+
return type;
|
|
169271
|
+
}
|
|
169272
|
+
buildReviewerSystemPrompt() {
|
|
169273
|
+
return `You are a senior code reviewer specializing in detecting logical errors in TypeScript.
|
|
169274
|
+
|
|
169275
|
+
Your task is to review generated function implementations for subtle bugs that pass syntax validation but are logically incorrect.
|
|
169276
|
+
|
|
169277
|
+
Common issues to detect:
|
|
169278
|
+
1. FUNCTION SHADOWING: An inner function with the same name as the outer function, causing the outer to never be called correctly.
|
|
169279
|
+
2. UNUSED VARIABLES: Variables declared but never used or returned.
|
|
169280
|
+
3. MISSING STEP IMPLEMENTATION: Steps mentioned in requirements but not implemented.
|
|
169281
|
+
4. INFINITE LOOPS / RECURSION: Unintended recursive calls or loops with no exit condition.
|
|
169282
|
+
5. TYPE MISMATCHES: Returning wrong type or assigning incompatible types.
|
|
169283
|
+
6. DEAD CODE: Code that can never be reached.
|
|
169284
|
+
|
|
169285
|
+
Response Format:
|
|
169286
|
+
- If the code is logically correct, respond with exactly: APPROVED
|
|
169287
|
+
- If there are issues, respond with:
|
|
169288
|
+
ISSUES:
|
|
169289
|
+
- [Issue 1 description]
|
|
169290
|
+
- [Issue 2 description]
|
|
169291
|
+
...
|
|
169292
|
+
FIX: [Brief description of what needs to change]`;
|
|
169293
|
+
}
|
|
169294
|
+
buildReviewerPrompt(context, code) {
|
|
169295
|
+
const lines = [];
|
|
169296
|
+
lines.push(`Review this TypeScript function implementation for logical errors:
|
|
169297
|
+
`);
|
|
169298
|
+
lines.push(`Function: ${context.functionName}`);
|
|
169299
|
+
lines.push(`Parameters: ${this.formatParameters(context.parameters)}`);
|
|
169300
|
+
lines.push(`Return Type: ${context.returnType}
|
|
169301
|
+
`);
|
|
169302
|
+
lines.push(`Required Steps:`);
|
|
169303
|
+
context.steps.forEach((step, i) => {
|
|
169304
|
+
let line = `${i + 1}. "${step.instruction}"`;
|
|
169305
|
+
if (step.resultVariable) {
|
|
169306
|
+
line += ` => ${step.resultVariable}`;
|
|
169307
|
+
if (step.resultType)
|
|
169308
|
+
line += `: ${step.resultType}`;
|
|
169309
|
+
}
|
|
169310
|
+
lines.push(line);
|
|
169311
|
+
});
|
|
169312
|
+
lines.push(`
|
|
169313
|
+
Generated Code:`);
|
|
169314
|
+
lines.push("```typescript");
|
|
169315
|
+
lines.push(code);
|
|
169316
|
+
lines.push("```");
|
|
169317
|
+
lines.push(`
|
|
169318
|
+
Verify:`);
|
|
169319
|
+
lines.push(`1. All steps are implemented correctly`);
|
|
169320
|
+
lines.push(`2. No function shadowing or naming conflicts`);
|
|
169321
|
+
lines.push(`3. All declared variables are used`);
|
|
169322
|
+
lines.push(`4. Return value matches required type`);
|
|
169323
|
+
lines.push(`5. No infinite loops or unintended recursion`);
|
|
169324
|
+
lines.push(`
|
|
169325
|
+
Respond with APPROVED or ISSUES + FIX as specified.`);
|
|
169326
|
+
return lines.join(`
|
|
169327
|
+
`);
|
|
169328
|
+
}
|
|
169275
169329
|
}
|
|
169276
169330
|
// src/ai/providers/gemini.ts
|
|
169277
169331
|
var DEFAULT_MODEL2 = "gemini-flash-latest";
|
|
@@ -169657,20 +169711,18 @@ class AICodeGenerator {
|
|
|
169657
169711
|
}
|
|
169658
169712
|
const cleanCode = this.sanitizeAIResponse(response.code);
|
|
169659
169713
|
const updatedCode = this.injectImplementation(fileContent, cleanCode, functionName);
|
|
169660
|
-
const
|
|
169661
|
-
if (
|
|
169662
|
-
|
|
169663
|
-
|
|
169664
|
-
|
|
169665
|
-
if (attempts < limit) {
|
|
169666
|
-
const errorMsg = errors.slice(0, 5).map((e) => `Line ${e.line}: ${e.message}`).join(`
|
|
169714
|
+
const syntaxErrors = this.validator.validate(updatedCode);
|
|
169715
|
+
if (syntaxErrors.length > 0) {
|
|
169716
|
+
attempts++;
|
|
169717
|
+
if (attempts < limit) {
|
|
169718
|
+
const errorMsg = syntaxErrors.slice(0, 5).map((e) => `Line ${e.line}: ${e.message}`).join(`
|
|
169667
169719
|
`);
|
|
169668
|
-
|
|
169669
|
-
|
|
169720
|
+
console.log(` ⚠️ Validation failed for ${functionName} (Attempt ${attempts}/${limit}):`);
|
|
169721
|
+
console.log(errorMsg.split(`
|
|
169670
169722
|
`).map((l) => ` ${l}`).join(`
|
|
169671
169723
|
`));
|
|
169672
|
-
|
|
169673
|
-
|
|
169724
|
+
console.log(` Retrying with AI auto-correction...`);
|
|
169725
|
+
prompt += `
|
|
169674
169726
|
|
|
169675
169727
|
Validation Failed (Attempt ${attempts}/${limit}).
|
|
169676
169728
|
` + `The code you generated caused errors:
|
|
@@ -169684,10 +169736,42 @@ ${errorMsg}
|
|
|
169684
169736
|
` + `CRITICAL INSTRUCTION: Don't just patch the code.
|
|
169685
169737
|
` + `1. First, analyze WHY this error occurred (e.g., mismatching types, undefined variable).
|
|
169686
169738
|
` + `2. Then, generate the completely fixed function body wrapped in a \`\`\`typescript block.
|
|
169739
|
+
`;
|
|
169740
|
+
} else {
|
|
169741
|
+
console.warn(` ❌ Validation failed for ${functionName} after ${limit} attempts.`);
|
|
169742
|
+
await this.explainFailure(functionName, updatedCode, syntaxErrors);
|
|
169743
|
+
return updatedCode;
|
|
169744
|
+
}
|
|
169745
|
+
continue;
|
|
169746
|
+
}
|
|
169747
|
+
const logicIssues = await this.verifyLogic(context, cleanCode);
|
|
169748
|
+
if (logicIssues === null) {
|
|
169749
|
+
return updatedCode;
|
|
169750
|
+
}
|
|
169751
|
+
attempts++;
|
|
169752
|
+
if (attempts < limit) {
|
|
169753
|
+
console.log(` \uD83D\uDD0D Logic review found issues for ${functionName} (Attempt ${attempts}/${limit}):`);
|
|
169754
|
+
console.log(` ${logicIssues.issues.slice(0, 3).join(`
|
|
169755
|
+
`)}`);
|
|
169756
|
+
console.log(` Retrying with AI auto-correction...`);
|
|
169757
|
+
prompt += `
|
|
169758
|
+
|
|
169759
|
+
Logic Review Failed (Attempt ${attempts}/${limit}).
|
|
169760
|
+
` + `The code you generated has logical issues:
|
|
169761
|
+
\`\`\`typescript
|
|
169762
|
+
${cleanCode}
|
|
169763
|
+
\`\`\`
|
|
169764
|
+
|
|
169765
|
+
` + `Issues found:
|
|
169766
|
+
${logicIssues.issues.join(`
|
|
169767
|
+
`)}
|
|
169768
|
+
|
|
169769
|
+
` + `Suggested fix: ${logicIssues.fix}
|
|
169770
|
+
|
|
169771
|
+
` + `CRITICAL INSTRUCTION: Fix all logical issues and generate the corrected function body.
|
|
169687
169772
|
`;
|
|
169688
169773
|
} else {
|
|
169689
|
-
console.warn(` ❌
|
|
169690
|
-
await this.explainFailure(functionName, updatedCode, errors);
|
|
169774
|
+
console.warn(` ❌ Logic verification failed for ${functionName} after ${limit} attempts.`);
|
|
169691
169775
|
return updatedCode;
|
|
169692
169776
|
}
|
|
169693
169777
|
}
|
|
@@ -169698,6 +169782,44 @@ ${errorMsg}
|
|
|
169698
169782
|
sanitized = sanitized.replace(/^\s*import\s+.*;?\s*$/gm, "");
|
|
169699
169783
|
return sanitized.trim();
|
|
169700
169784
|
}
|
|
169785
|
+
async verifyLogic(context, code) {
|
|
169786
|
+
try {
|
|
169787
|
+
const systemPrompt = this.promptBuilder.buildReviewerSystemPrompt();
|
|
169788
|
+
const prompt = this.promptBuilder.buildReviewerPrompt(context, code);
|
|
169789
|
+
const response = await this.provider.generateCode({
|
|
169790
|
+
systemPrompt,
|
|
169791
|
+
prompt
|
|
169792
|
+
});
|
|
169793
|
+
if (!response.success) {
|
|
169794
|
+
console.warn(` ⚠️ Logic verification skipped (AI error)`);
|
|
169795
|
+
return null;
|
|
169796
|
+
}
|
|
169797
|
+
const reviewResult = response.code.trim();
|
|
169798
|
+
if (reviewResult.toUpperCase().startsWith("APPROVED")) {
|
|
169799
|
+
return null;
|
|
169800
|
+
}
|
|
169801
|
+
const issuesMatch = reviewResult.match(/ISSUES:\s*([\s\S]*?)(?:FIX:|$)/i);
|
|
169802
|
+
const fixMatch = reviewResult.match(/FIX:\s*([\s\S]*?)$/i);
|
|
169803
|
+
if (issuesMatch) {
|
|
169804
|
+
const issueLines = issuesMatch[1].split(`
|
|
169805
|
+
`).map((l) => l.trim()).filter((l) => l.startsWith("-") || l.startsWith("*") || l.match(/^\d+\./)).map((l) => l.replace(/^[-*]\s*/, "").replace(/^\d+\.\s*/, ""));
|
|
169806
|
+
const fix = fixMatch ? fixMatch[1].trim() : "Review and fix the issues listed above.";
|
|
169807
|
+
if (issueLines.length > 0) {
|
|
169808
|
+
return { issues: issueLines, fix };
|
|
169809
|
+
}
|
|
169810
|
+
}
|
|
169811
|
+
if (reviewResult.toLowerCase().includes("issue") || reviewResult.toLowerCase().includes("problem")) {
|
|
169812
|
+
return {
|
|
169813
|
+
issues: ["Reviewer found potential issues (see details)"],
|
|
169814
|
+
fix: reviewResult.slice(0, 200)
|
|
169815
|
+
};
|
|
169816
|
+
}
|
|
169817
|
+
return null;
|
|
169818
|
+
} catch (err) {
|
|
169819
|
+
console.warn(` ⚠️ Logic verification skipped (error)`);
|
|
169820
|
+
return null;
|
|
169821
|
+
}
|
|
169822
|
+
}
|
|
169701
169823
|
async explainFailure(functionName, code, errors) {
|
|
169702
169824
|
const errorMsg = errors.map((e) => `Line ${e.line}: ${e.message}`).join(`
|
|
169703
169825
|
`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intend-it/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Core compiler and AI integration for the Intend programming language",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
],
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@intend-it/parser": "^1.
|
|
38
|
+
"@intend-it/parser": "^1.2.0",
|
|
39
39
|
"@google/generative-ai": "^0.21.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
@@ -43,6 +43,6 @@
|
|
|
43
43
|
"typescript": "^5.0.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@intend-it/parser": ">=1.
|
|
46
|
+
"@intend-it/parser": ">=1.2.0"
|
|
47
47
|
}
|
|
48
48
|
}
|