@intend-it/core 4.0.0 → 4.0.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.
Files changed (3) hide show
  1. package/README.md +3 -0
  2. package/dist/index.js +85 -73
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
  <h1>@intend-it/core</h1>
3
3
  <p><strong>AI-Powered Code Generator for the Intend Language</strong></p>
4
4
  <p>
5
+ <a href="https://intend.fly.dev">Documentation</a> •
5
6
  <a href="https://www.npmjs.com/package/@intend-it/core"><img src="https://img.shields.io/npm/v/@intend-it/core.svg" alt="npm version"></a>
6
7
  <a href="https://www.npmjs.com/package/@intend-it/core"><img src="https://img.shields.io/npm/dm/@intend-it/core.svg" alt="npm downloads"></a>
7
8
  <a href="https://github.com/DRFR0ST/intend/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@intend-it/core.svg" alt="license"></a>
@@ -14,6 +15,8 @@
14
15
 
15
16
  `@intend-it/core` is the heart of the Intend compiler. It takes parsed AST from `@intend-it/parser` and generates production-ready TypeScript code using AI (Google Gemini or Ollama).
16
17
 
18
+ **[Read the full documentation 📖](https://intend.fly.dev)**
19
+
17
20
  ### ✨ Key Features
18
21
 
19
22
  - **🧠 AI Code Generation** - Transforms natural language steps into actual code
package/dist/index.js CHANGED
@@ -168803,8 +168803,15 @@ class TypeScriptGenerator {
168803
168803
  `;
168804
168804
  code += `if (import.meta.main) {
168805
168805
  `;
168806
- code += ` Promise.resolve(${intent.name}()).catch((err: any) => console.error(err));
168806
+ if (intent.parameters.length === 0) {
168807
+ code += ` Promise.resolve(${intent.name}()).catch((err: any) => console.error(err));
168807
168808
  `;
168809
+ } else {
168810
+ code += ` // CLI Auto-Run disabled: function requires arguments
168811
+ `;
168812
+ code += ` // Promise.resolve(${intent.name}(/* ...args */)).catch((err: any) => console.error(err));
168813
+ `;
168814
+ }
168808
168815
  code += `}
168809
168816
  `;
168810
168817
  }
@@ -168817,7 +168824,8 @@ class TypeScriptGenerator {
168817
168824
  }
168818
168825
  generateImport(imp) {
168819
168826
  const specifiers = imp.specifiers.join(", ");
168820
- return `import { ${specifiers} } from "${imp.source}";`;
168827
+ const source = imp.source.replace(/\.intent$/, "");
168828
+ return `import { ${specifiers} } from "${source}";`;
168821
168829
  }
168822
168830
  generateJsDoc(intent) {
168823
168831
  const lines = ["/**"];
@@ -169179,21 +169187,22 @@ class PromptBuilder {
169179
169187
  };
169180
169188
  }
169181
169189
  buildSystemPrompt() {
169182
- return `You are an expert TypeScript developer specializing in clean, production-ready code.
169183
-
169184
- Your task is to implement function bodies based on natural language specifications.
169185
-
169186
- Rules:
169187
- 1. Generate ONLY the implementation code (function body content)
169188
- 2. Do NOT include the function signature or braces
169189
- 3. Use modern TypeScript syntax and best practices
169190
- 4. Handle errors appropriately (throw Error with descriptive messages)
169191
- 5. Return values must match the specified return type
169192
- 6. Follow the exact steps provided in order
169193
- 7. Respect all invariants (add runtime checks if needed)
169194
- 8. Ensure all postconditions are satisfied
169195
- 9. Use descriptive variable names
169196
- 10. Keep code clean and readable`;
169190
+ return `You are an expert Senior TypeScript Developer specializing in robust, production-grade code.
169191
+
169192
+ Your task is to implement function bodies based on structured natural language specifications (Intend).
169193
+
169194
+ Rules:
169195
+ 1. Generate ONLY the implementation code (function body content).
169196
+ 2. Do NOT include the function signature, exports, or braces surfacing the function.
169197
+ 3. Use strict TypeScript (no 'any' unless absolutely necessary).
169198
+ 4. Handle edge cases and errors defensively (throw descriptive Errors).
169199
+ 5. Return values must strictly match the specified return type.
169200
+ 6. Follow the provided steps EXACTLY in order.
169201
+ 7. Respect all invariants (implement usage checks at the start if needed).
169202
+ 8. Ensure all postconditions (ensures) are satisfiable by your logic.
169203
+ 9. Use clear, semantic variable names.
169204
+ 10. Do not hallucinate imports; use only what is provided.
169205
+ 11. If a variable name in the requirements seems to have a typo (e.g. 'totals' instead of 'total'), fix it to match the defined variable.`;
169197
169206
  }
169198
169207
  buildStepPrompt(context, stepIndex) {
169199
169208
  const step = context.steps[stepIndex];
@@ -169314,6 +169323,9 @@ Generate ONLY the TypeScript code for this step (no explanations, no markdown).`
169314
169323
  lines.push(`The code should:`);
169315
169324
  lines.push(`- Follow all steps in order`);
169316
169325
  lines.push(`- Store results in the specified variable names`);
169326
+ lines.push(`- Make sure that imported functions are called with the correct arguments from the context.`);
169327
+ lines.push(`- Make sure the return value matches the expected return type strictly.`);
169328
+ lines.push(`- If a Step requires a return, ensure it is returned.`);
169317
169329
  lines.push(`- Satisfy all postconditions`);
169318
169330
  lines.push(`- Be production-ready TypeScript`);
169319
169331
  lines.push(`
@@ -169395,6 +169407,7 @@ Verify:`);
169395
169407
  lines.push(`3. All declared variables are used`);
169396
169408
  lines.push(`4. Return value matches required type`);
169397
169409
  lines.push(`5. No infinite loops or unintended recursion`);
169410
+ lines.push(`6. Imports are correct (no .intent extension, only using provided imports)`);
169398
169411
  lines.push(`
169399
169412
  Respond with APPROVED or ISSUES + FIX as specified.`);
169400
169413
  return lines.join(`
@@ -169791,7 +169804,10 @@ class TypeScriptValidator {
169791
169804
  esModuleInterop: true,
169792
169805
  skipLibCheck: true,
169793
169806
  checkJs: false,
169794
- allowJs: false
169807
+ allowJs: false,
169808
+ noImplicitReturns: true,
169809
+ strictNullChecks: true,
169810
+ noImplicitAny: true
169795
169811
  };
169796
169812
  const host = ts.createCompilerHost(compilerOptions);
169797
169813
  const originalReadFile = host.readFile;
@@ -169810,6 +169826,12 @@ class TypeScriptValidator {
169810
169826
  `);
169811
169827
  if (msg.includes("Cannot find global type"))
169812
169828
  return false;
169829
+ if (msg.includes("Cannot find name 'process'"))
169830
+ return false;
169831
+ if (msg.includes("Cannot find name 'console'"))
169832
+ return false;
169833
+ if (msg.includes("Cannot find name 'Buffer'"))
169834
+ return false;
169813
169835
  return true;
169814
169836
  }).map((d) => {
169815
169837
  const line = d.file ? d.file.getLineAndCharacterOfPosition(d.start).line + 1 : 0;
@@ -169839,7 +169861,7 @@ class AICodeGenerator {
169839
169861
  constructor(options) {
169840
169862
  this.promptBuilder = new PromptBuilder;
169841
169863
  this.validator = new TypeScriptValidator;
169842
- this.mode = options.mode || "full";
169864
+ this.mode = "full";
169843
169865
  this.maxAttempts = options.maxAttempts ?? 3;
169844
169866
  this.projectContext = options.projectContext;
169845
169867
  this.debug = options.debug ?? false;
@@ -169889,7 +169911,6 @@ class AICodeGenerator {
169889
169911
  const context = this.promptBuilder.buildContext(intent, file.imports, importedIntents);
169890
169912
  totalSteps += context.steps.length;
169891
169913
  if (this.mode === "step-by-step") {
169892
- console.warn("Step-by-step mode not fully supported for multi-intent files. Falling back to full generation.");
169893
169914
  currentCode = await this.generateFull(currentCode, context, intent.name);
169894
169915
  } else {
169895
169916
  currentCode = await this.generateFull(currentCode, context, intent.name);
@@ -169917,18 +169938,21 @@ class AICodeGenerator {
169917
169938
  throw new Error(`AI generation failed: ${response.error}`);
169918
169939
  }
169919
169940
  const cleanCode = this.sanitizeAIResponse(response.code);
169920
- const updatedCode = this.injectImplementation(fileContent, cleanCode, functionName);
169921
- const syntaxErrors = this.validator.validate(updatedCode);
169941
+ const { code: updatedCode, startLine, endLine } = this.injectImplementation(fileContent, functionName, cleanCode);
169942
+ let syntaxErrors = this.validator.validate(updatedCode);
169943
+ syntaxErrors = syntaxErrors.filter((e) => e.line >= startLine + 1 && e.line <= endLine + 1);
169922
169944
  if (syntaxErrors.length > 0) {
169923
169945
  attempts++;
169924
169946
  if (attempts < limit) {
169925
169947
  const errorMsg = syntaxErrors.slice(0, 5).map((e) => `Line ${e.line}: ${e.message}`).join(`
169926
169948
  `);
169927
- console.log(` ⚠️ Validation failed for ${functionName} (Attempt ${attempts}/${limit}):`);
169949
+ console.log(`
169950
+ ⚠️ Validation failed for ${functionName} (Attempt ${attempts}/${limit}):`);
169928
169951
  console.log(errorMsg.split(`
169929
169952
  `).map((l) => ` ${l}`).join(`
169930
169953
  `));
169931
- console.log(` Retrying with AI auto-correction...`);
169954
+ console.log(` Retrying with AI auto-correction...
169955
+ `);
169932
169956
  prompt += `
169933
169957
 
169934
169958
  Validation Failed (Attempt ${attempts}/${limit}).
@@ -169940,14 +169964,17 @@ ${cleanCode}
169940
169964
  ` + `Errors encountered:
169941
169965
  ${errorMsg}
169942
169966
 
169943
- ` + `CRITICAL INSTRUCTION: Don't just patch the code.
169944
- ` + `1. First, analyze WHY this error occurred (e.g., mismatching types, undefined variable).
169945
- ` + `2. Then, generate the completely fixed function body wrapped in a \`\`\`typescript block.
169967
+ ` + `CRITICAL: You are fixing a critical syntax/compilation error.
169968
+ ` + `1. ANALYZE: Briefly explain why the error happened.
169969
+ ` + `2. FIX: Generate the COMPLETE, FIXED function body wrapped in \`\`\`typescript.
169970
+ ` + `Do not return partial snippets. Return the full function body.
169946
169971
  `;
169947
169972
  } else {
169948
- console.warn(` ❌ Validation failed for ${functionName} after ${limit} attempts.`);
169949
- await this.explainFailure(functionName, updatedCode, syntaxErrors);
169950
- return updatedCode;
169973
+ const errorMsg = syntaxErrors.slice(0, 5).map((e) => `Line ${e.line}: ${e.message}`).join(`
169974
+ `);
169975
+ throw new Error(`Validation failed for ${functionName} after ${limit} attempts.
169976
+ Errors:
169977
+ ${errorMsg}`);
169951
169978
  }
169952
169979
  continue;
169953
169980
  }
@@ -169957,10 +169984,12 @@ ${errorMsg}
169957
169984
  }
169958
169985
  attempts++;
169959
169986
  if (attempts < limit) {
169960
- console.log(` \uD83D\uDD0D Logic review found issues for ${functionName} (Attempt ${attempts}/${limit}):`);
169987
+ console.log(`
169988
+ \uD83D\uDD0D Logic review found issues for ${functionName} (Attempt ${attempts}/${limit}):`);
169961
169989
  console.log(` ${logicIssues.issues.slice(0, 3).join(`
169962
169990
  `)}`);
169963
- console.log(` Retrying with AI auto-correction...`);
169991
+ console.log(` Retrying with AI auto-correction...
169992
+ `);
169964
169993
  prompt += `
169965
169994
 
169966
169995
  Logic Review Failed (Attempt ${attempts}/${limit}).
@@ -169975,17 +170004,23 @@ ${logicIssues.issues.join(`
169975
170004
 
169976
170005
  ` + `Suggested fix: ${logicIssues.fix}
169977
170006
 
169978
- ` + `CRITICAL INSTRUCTION: Fix all logical issues and generate the corrected function body.
170007
+ ` + `CRITICAL: You are fixing a LOGICAL issue.
170008
+ ` + `1. ANALYZE: Briefly explain the logic flaw.
170009
+ ` + `2. FIX: Generate the COMPLETE, FIXED function body wrapped in \`\`\`typescript.
170010
+ ` + `Ensure every step from the requirements is fully implemented.
169979
170011
  `;
169980
170012
  } else {
169981
- console.warn(`Logic verification failed for ${functionName} after ${limit} attempts.`);
169982
- return updatedCode;
170013
+ throw new Error(`Logic verification failed for ${functionName} after ${limit} attempts.
170014
+ Issues:
170015
+ ${logicIssues.issues.join(`
170016
+ `)}`);
169983
170017
  }
169984
170018
  }
169985
170019
  return fileContent;
169986
170020
  }
169987
170021
  sanitizeAIResponse(code) {
169988
170022
  let sanitized = code.replace(/```typescript\s*/g, "").replace(/```\s*/g, "");
170023
+ sanitized = sanitized.replace(/^(Here is|Sure|I have|Certainly).+$/gim, "");
169989
170024
  sanitized = sanitized.replace(/^\s*import\s+.*;?\s*$/gm, "");
169990
170025
  return sanitized.trim();
169991
170026
  }
@@ -170029,40 +170064,9 @@ ${logicIssues.issues.join(`
170029
170064
  }
170030
170065
  }
170031
170066
  async explainFailure(functionName, code, errors) {
170032
- const errorMsg = errors.map((e) => `Line ${e.line}: ${e.message}`).join(`
170033
- `);
170034
- const prompt = `
170035
- The following TypeScript implementation for '${functionName}' failed validation after multiple self-correction attempts.
170036
-
170037
- Code:
170038
- ${code}
170039
-
170040
- Errors:
170041
- ${errorMsg}
170042
-
170043
- Please explain to the user why this code is failing and what they might need to fix in their .intend file or configuration.
170044
- Focus on the root cause. Keep it concise.
170045
- `;
170046
- try {
170047
- console.log(` \uD83E\uDD16 Analyzing failure...`);
170048
- const response = await this.provider.generateCode({
170049
- systemPrompt: "You are a helpful coding assistant explaining compile errors.",
170050
- prompt,
170051
- debug: this.debug
170052
- });
170053
- if (response.success) {
170054
- console.log(`
170055
- ==================================================`);
170056
- console.log("\uD83E\uDD16 AI DIAGNOSTICS:");
170057
- console.log(response.code);
170058
- console.log(`==================================================
170059
- `);
170060
- }
170061
- } catch (err) {
170062
- console.warn("Failed to generate diagnostics.");
170063
- }
170067
+ return;
170064
170068
  }
170065
- injectImplementation(fileContent, implementation, functionName) {
170069
+ injectImplementation(fileContent, functionName, implementation) {
170066
170070
  const lines = fileContent.split(`
170067
170071
  `);
170068
170072
  let startLine = -1;
@@ -170090,8 +170094,12 @@ Focus on the root cause. Keep it concise.
170090
170094
  }
170091
170095
  }
170092
170096
  if (!foundStart || endLine === -1) {
170093
- console.error(`Could not find function body for ${functionName}. Regex: ${regex}`);
170094
- return fileContent;
170097
+ throw new Error(`Fatal: Could not filter function body for injection.
170098
+ Function: ${functionName}
170099
+ Regex: ${regex}
170100
+ Start found: ${foundStart}, End found: ${endLine !== -1}
170101
+
170102
+ Possible cause: Formatting mismatch between template and injector.`);
170095
170103
  }
170096
170104
  const pre = lines.slice(0, startLine + 1).join(`
170097
170105
  `);
@@ -170104,9 +170112,13 @@ Focus on the root cause. Keep it concise.
170104
170112
  return "";
170105
170113
  }).join(`
170106
170114
  `);
170107
- return pre + `
170108
- ` + indentedImpl + `
170109
- ` + post;
170115
+ return {
170116
+ code: `${pre}
170117
+ ${indentedImpl}
170118
+ ${post}`,
170119
+ startLine: startLine + 1,
170120
+ endLine: endLine + 1
170121
+ };
170110
170122
  }
170111
170123
  async testConnection() {
170112
170124
  return await this.provider.testConnection();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intend-it/core",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
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.3.0",
38
+ "@intend-it/parser": "^1.3.1",
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.3.0"
46
+ "@intend-it/parser": ">=1.3.1"
47
47
  }
48
48
  }