Code generation method and device, equipment, storage medium and computer program product

By accurately matching the target rule file and the global rule file through the path-aware rule layer to generate a set of context prompt words, the problem of reduced code generation accuracy in a single Monorepo code repository is solved, and efficient and accurate code generation is achieved.

CN123137645APending Publication Date: 2026-11-20BEIJING QIHOOD TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611308793.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-26
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing code generation methods in the Monorepo single-code repository architecture suffer from reduced code generation accuracy, wasted resources, and increased API call costs due to the full loading of irrelevant information.

Method used

By obtaining the path information of the currently edited file, the target rule file of the path-aware rule layer is accurately matched. Combined with the global rule file, a set of contextual prompt words is generated. Only the module specification information related to the currently edited file is loaded, avoiding irrelevant information from interfering with the attention mechanism of the large language model.

Benefits of technology

It improves the accuracy of code generation, reduces resource waste and API call costs, and enhances the efficiency and accuracy of code generation results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123137645A_ABST
    Figure CN123137645A_ABST
Patent Text Reader

Abstract

The application discloses a code generation method and device, equipment, a storage medium and a computer program product, relates to the technical field of artificial intelligence, and the method comprises the following steps: determining a matched target rule file from a path perception rule layer according to file path information of a current editing file, the target rule file being used for carrying a code module-specific coding specification to which the current editing file belongs; acquiring a global rule file from a global knowledge layer, and generating a context prompt word set of the current editing file based on the global rule file and the target rule file in the global knowledge layer; and injecting the context prompt word set into a preset code generation model, so that the preset code generation model generates code based on the context prompt word set. Since the application only loads specification information related to the current editing file instead of full information, interference of irrelevant information on the attention mechanism of the large language model can be avoided, and the accuracy of code generation is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and in particular to code generation methods, apparatus, devices, storage media, and computer program products. Background Technology

[0002] With the continuous improvement of Large Language Model (LLM) capabilities, AI programming assistants (such as GitHub Copilot, Cursor, and Claude Code) have been widely applied in daily software development scenarios. These AI programming assistants can assemble the user's natural language instructions, the content of the currently edited code file, and additional contextual information extracted from the project environment into a prompt, which is then input into the LLM. The model then generates corresponding code or modification suggestions based on the prompt. Therefore, the quality of the code generated by the AI ​​programming assistant is highly dependent on the accuracy, completeness, and relevance of the project context and prompts injected into the model.

[0003] Currently, existing code generation methods typically place a single rules configuration file in the project root directory, centralizing all coding standards, technical constraints, and architectural specifications for the entire project into this file. The AI ​​programming assistant loads this file in its entirety during each interaction, injecting it as additional context into the large language model, which then generates the corresponding code based on the prompts. However, in projects managed using the Monorepo single-code repository architecture, the codebase often contains multiple sub-applications, shared libraries, and business packages. Each module may have independent or even incompatible coding conventions (e.g., application A uses regular CSS, while application B uses CSS Modules within the same repository). In such scenarios, centralizing all specifications in a single configuration file leads to the AI ​​passively loading a large amount of module information irrelevant to the current task during each interaction. This not only wastes token resources and increases API call costs but also interferes with the large language model's attention mechanism due to excessive irrelevant information, thereby reducing the accuracy and usability of code generation. Summary of the Invention

[0004] The main objective of this application is to provide a code generation method, apparatus, device, storage medium, and computer program product, aiming to solve the technical problem that existing code generation methods are prone to reduced accuracy due to the full loading of irrelevant information.

[0005] To achieve the above objectives, this application proposes a code generation method, which includes: Obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, a target rule file matching the currently edited file is determined from the path-aware rule layer. The target rule file is used to carry the exclusive coding specifications of the code module to which the currently edited file belongs. The global rule file is obtained from the global knowledge layer, and a set of context prompt words for the currently edited file is generated based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The set of contextual prompts is injected into a preset code generation model so that the preset code generation model generates code based on the set of contextual prompts.

[0006] In one embodiment, the step of determining the target rule file matching the currently edited file from the path-aware rule layer based on the file path information of the currently edited file includes: Traverse all candidate rule files in the path-aware rule layer and read the path matching pattern declared in the header metadata of each candidate rule file. The path matching pattern is used to describe the range of file paths applicable to the corresponding candidate rule file. Perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns; If a match is successful, the first candidate rule file that matches successfully will be determined as the target rule file that matches the currently edited file.

[0007] In one embodiment, after the step of performing wildcard path matching between the file path information of the currently edited file and each of the path matching patterns, the method further includes: If the matching fails, the permanent effective status identifier of each candidate rule file is obtained. The permanent effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective. Detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state; If it exists, the second candidate rule file is determined as the target rule file that matches the currently edited file.

[0008] In one embodiment, the target project knowledge base further includes a task rule layer; before the step of generating the context prompt word set of the currently edited file based on the global rule file and the target rule file, the method further includes: In response to an explicit task invocation command input by the user, the task rule file corresponding to the explicit task invocation command is obtained from the task rule layer. The task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command. The process of generating the contextual prompt word set for the currently edited file based on the global rule file and the target rule file includes: The contextual prompt word set for the currently edited file is generated based on the global rule file, the target rule file, and the task rule file.

[0009] In one embodiment, the step of obtaining the task rule file corresponding to the explicit task invocation instruction from the task rule layer includes: The explicit task invocation instruction is parsed to identify whether it contains a task reference marker. If included, the task rule file corresponding to the explicit task invocation instruction is obtained from the task rule layer based on the task reference tag.

[0010] In one embodiment, the step of generating the set of contextual prompt words for the currently edited file based on the global rule file, the target rule file, and the task rule file includes: Add the global rule file, the target rule file, and the task rule file to the initial context set respectively; The initial context set is sorted according to a preset hierarchical priority order to generate a sorted context set. The sorted context set is deduplicated to generate the context prompt word set for the currently edited file.

[0011] In one embodiment, before the step of injecting the context prompt word set into a preset code generation model so that the preset code generation model generates code based on the context prompt word set, the method further includes: Extract code generation description information from user-input code generation instructions; Generate code generation prompts based on the context prompt set and the code generation description information; The step of injecting the set of contextual prompts into a preset code generation model, so that the preset code generation model generates code based on the set of contextual prompts, includes: The code generation prompt is injected into a preset code generation model so that the preset code generation model generates code based on the code generation prompt.

[0012] In one embodiment, after the step of injecting the set of context prompts into a preset code generation model so that the preset code generation model generates code based on the set of context prompts, the method further includes: In response to a rule change operation triggered based on the target rule file, the modified target rule file is submitted to the target project knowledge base, and the rule file in the target project knowledge base is stored in the same code repository as the source code of the software project; The commit history of the code repository records the change information of the target rule file and the version information of the corresponding source code. Once the association is complete, a consistency check is performed on the modified target rule file and the associated source code changes; If the verification passes, the modified target rule file and the source code changes will be merged into the main branch of the code repository.

[0013] In one embodiment, after the step of injecting the set of context prompts into a preset code generation model so that the preset code generation model generates code based on the set of context prompts, the method further includes: In response to a rule creation instruction, obtain the rule type and module identifier specified by the rule creation instruction; If the rule type is a module rule, then the module path is determined based on the module identifier; Create a module rule file template corresponding to the module path in a preset directory of the software project's code repository. The module rule file template includes a path matching pattern input field. Receive new rule content input by the user based on the module rule file template; A new rule file is generated based on the newly added rule content, and the new rule file is stored in the preset directory to update the target project knowledge base.

[0014] In one embodiment, the global knowledge layer, the path-aware rule layer, and the task rule layer are configured with differentiated loading conditions. The rule files of the global knowledge layer are loaded unconditionally, the rule files of the path-aware rule layer are loaded based on the matching results of the file path and the path matching conditions, and the rule files of the task rule layer are loaded based on explicit user references.

[0015] Furthermore, to achieve the above objectives, this application also proposes a code generation apparatus, the apparatus comprising: The knowledge base management module is used to obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. The file matching module is used to determine the target rule file that matches the currently edited file from the path-aware rule layer based on the file path information of the currently edited file. The target rule file is used to carry the exclusive coding specification of the code module to which the currently edited file belongs. The prompt word generation module is used to obtain a global rule file from the global knowledge layer, and generate a set of context prompt words for the currently edited file based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The code generation module is used to inject the set of context prompts into a preset code generation model, so that the preset code generation model generates code based on the set of context prompts.

[0016] In one embodiment, the file matching module is further configured to traverse all candidate rule files in the path-aware rule layer and read the path matching mode declared in the header metadata of each candidate rule file, wherein the path matching mode is used to describe the range of file paths applicable to the corresponding candidate rule file. The file matching module is also used to perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns; The file matching module is further configured to, if a match is successful, determine the first candidate rule file that has successfully matched as the target rule file that matches the currently edited file.

[0017] In one embodiment, the file matching module is further configured to, if the matching fails, obtain the resident effective status identifier of each candidate rule file, wherein the resident effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective; The file matching module is also used to detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state; The file matching module is further configured to, if it exists, determine the second candidate rule file as the target rule file that matches the currently edited file.

[0018] In one embodiment, the target project knowledge base further includes a task rule layer; the file matching module is further configured to, in response to an explicit task invocation command input by the user, obtain a task rule file corresponding to the explicit task invocation command from the task rule layer, wherein the task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command; The prompt word generation module is also used to generate a set of context prompt words for the currently edited file based on the global rule file, the target rule file, and the task rule file.

[0019] In one embodiment, the prompt word generation module is further configured to add the global rule file, the target rule file, and the task rule file to the initial context set, respectively; The prompt word generation module is also used to sort the content of the initial context set according to a preset hierarchical priority order to generate a sorted context set; The prompt word generation module is also used to perform deduplication processing on the sorted context set to generate the context prompt word set of the currently edited file.

[0020] In one embodiment, the knowledge base management module is further configured to, in response to a rule change operation triggered based on the target rule file, submit the modified target rule file to the target project knowledge base, wherein the rule file in the target project knowledge base and the source code of the software project are stored in the same code repository; The knowledge base management module is also used to associate and record the change information of the target rule file with the version information of the corresponding source code in the commit records of the code repository; The knowledge base management module is also used to perform consistency verification between the modified target rule file and the associated source code changes after the association is completed. The knowledge base management module is also used to merge the modified target rule file and the source code changes into the main branch of the code repository if the verification passes.

[0021] In one embodiment, the knowledge base management module is further configured to, in response to a rule creation instruction, obtain the rule type and module identifier specified by the rule creation instruction; The knowledge base management module is also used to determine the module path based on the module identifier if the rule type is a module rule; The knowledge base management module is also used to create a module rule file template corresponding to the module path in a preset directory of the software project's code repository. The module rule file template includes a path matching mode input field. The knowledge base management module is also used to receive new rule content input by the user based on the rule file template of the module; The knowledge base management module is also used to generate a new rule file based on the new rule content, and store the new rule file in the preset directory to update the target project knowledge base.

[0022] In addition, to achieve the above objectives, this application also proposes a code generation device, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described above.

[0023] In addition, to achieve the above objectives, this application also proposes a storage medium, which is a computer-readable storage medium, on which a computer program is stored, and which, when executed by a processor, implements the steps of the code generation method described above.

[0024] In addition, to achieve the above objectives, this application also provides a computer program product, which includes a computer program that, when executed by a processor, implements the steps of the code generation method described above.

[0025] This application provides a code generation method. It discloses obtaining the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, the path-aware rule layer determines a target rule file matching the currently edited file. The target rule file carries the exclusive coding specifications of the code module to which the currently edited file belongs. A global rule file is obtained from the global knowledge layer, and a set of contextual prompts for the currently edited file is generated based on the global rule file and the target rule file. The global rule file carries the general project specifications of the software project. The set of contextual prompts is injected into a preset code generation model, enabling the preset code generation model to generate code based on the set of contextual prompts. Compared to existing methods that use a single rule configuration file to centrally inject all coding specifications of the entire project into a large model so that the large model generates code based on full information, this application uses the path-aware rule layer to accurately match the target rule file based on the path of the currently edited file, loading only the module specification information related to the currently edited file, rather than loading all module information. This avoids irrelevant information interfering with the attention mechanism of the large language model, solving the technical problem that existing code generation methods easily lead to reduced accuracy due to the full loading of irrelevant information. Attached Figure Description

[0026] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0027] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0028] Figure 1 This is a flowchart illustrating the code generation method of this application in Embodiment 1; Figure 2 This is a flowchart illustrating Embodiment 2 of the code generation method of this application; Figure 3 This is a flowchart illustrating the generation process of the contextual prompt word set in the code generation method of this application. Figure 4 This is a flowchart illustrating the code generation method of this application in Embodiment 3; Figure 5 The system architecture diagram provided for the code generation method of this application; Figure 6 This is a schematic diagram of the module structure of the code generation device according to an embodiment of this application; Figure 7 This is a schematic diagram of the device structure of the hardware operating environment involved in the code generation method in this application embodiment.

[0029] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0030] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0031] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0032] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device or code generation device capable of performing the above functions. The following description uses a code generation device (hereinafter referred to as the device) as an example to illustrate this embodiment and the subsequent embodiments.

[0033] Based on this, embodiments of this application provide a code generation method, referring to... Figure 1 , Figure 1 This is a flowchart illustrating the code generation method of this application in Embodiment 1.

[0034] In this embodiment, the code generation method includes steps S10 to S40: Step S10: Obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer.

[0035] Understandably, the currently edited file can be the source code file that the user is currently viewing, modifying, or selecting in the code editing area of ​​the Integrated Development Environment (IDE). It can be a program file written in any programming language, such as JavaScript files (with the .js extension), TypeScript files (with the .ts or .tsx extension), Python files (with the .py extension), Java files (with the .java extension), or C++ files (with the .cpp or .h extension). In everyday software development scenarios, developers can open a software project's code repository through the IDE and browse and edit multiple code files within it; in this case, the currently edited file is the file the developer is working on at a given moment.

[0036] It is also understood that a software project can be a complete software development engineering project, whose source code files, configuration files, and project documents are usually organized and stored in the form of a code repository. In this embodiment, the software project can be a large front-end engineering project, a back-end service project, or a Monorepo (single code repository) project containing multiple sub-applications and shared libraries; this embodiment does not impose any restrictions on this.

[0037] It should be understood that the target project knowledge base can be a structured data collection pre-configured for a software project to store the project knowledge required for AI programming assistance. It can be organized in the form of a file directory structure and stored in a preset location in the software project's code repository. For example, the target project knowledge base can be stored in the ".cursor" folder, ".codebuddy" folder, or ".github" folder under the project's root directory.

[0038] It should also be understood that the target project knowledge base in this embodiment differs from the source code files stored in the code repository corresponding to the software project. It stores descriptive text used to guide the AI ​​programming assistant in generating code that conforms to project specifications, rather than the program code itself. Furthermore, the rule files stored in the target project knowledge base can be organized into multiple levels according to different scopes and triggering conditions, such as a path-aware rule layer and a global knowledge layer.

[0039] It's important to note that the path-aware rule layer can be a logical hierarchy within the target project's knowledge base used to store rule files associated with the paths of specific code modules. Within the path-aware rule layer, each rule file corresponds to a code module (e.g., a sub-application, a shared library, or a business package) and carries the module's proprietary coding standards. In practice, the rule files in the path-aware rule layer are not loaded all at once with each interaction; instead, they are loaded on demand based on the path information of the currently edited file.

[0040] It should also be noted that the global knowledge layer can be a logical level within the target project's knowledge base used to store general rule files applicable to the entire software project. It can carry fundamental specifications that all code modules commonly adhere to, such as the overall project architecture description, global coding constraints, and core technology selection conventions. In practical applications, the rule files of the global knowledge layer are not limited to any specific file path; rather, they unconditionally apply to all source code files in the software project and are unconditionally loaded on each interaction.

[0041] In its implementation, the device first obtains the active file information of the editor window where the cursor is located from the interface of the integrated development environment (IDE), and identifies the file corresponding to this active file information as the currently edited file. Subsequently, the device determines the software project to which the currently edited file belongs, i.e., the software project corresponding to the code repository where the currently edited file is located, and obtains the target project knowledge base pre-configured for that software project. The rule files of the target project knowledge base are stored in a preset directory of the software project's code repository. The device can obtain the content of the target project knowledge base by reading the rule files in the preset directory. For example, the global knowledge layer can be stored in the first preset subdirectory under the software project's root directory (e.g., the ".cursor / global" directory), and the path-aware rule layer can be stored in the second preset subdirectory under the software project's root directory (e.g., the ".cursor / rules" directory).

[0042] Step S20: Determine the target rule file matching the current editing file from the path-aware rule layer based on the file path information of the current editing file. The target rule file is used to carry the exclusive coding specification of the code module to which the current editing file belongs.

[0043] It should be understood that the file path information can be the storage location of the currently edited file in the code repository. It can be an absolute path or a relative path based on the root directory of the code repository. In this embodiment, the file path information can be used to identify the specific location of the currently edited file in the code repository. For example, for the file path information "packages / app-a / src / components / Button.tsx", "packages / app-a / " indicates that the file belongs to the "app-a" sub-application, and "src / components / " indicates the specific directory location of the file within the sub-application.

[0044] It should be noted that the target rule file can be a rule file in the path-aware rule layer that matches the file path information of the currently edited file. It contains the exclusive coding specifications of the code module to which the currently edited file belongs, and is used to provide the code generation model with the coding constraint information unique to that code module.

[0045] It should also be noted that the code module to which the currently edited file belongs can be a functional module or sub-project within the software project's code repository. In large projects using the Monorepo architecture, a code repository typically contains multiple sub-applications, shared libraries, and business packages. Each sub-application or shared library constitutes a code module, and the currently edited file belongs to one of these code modules. For example, the currently edited file with the path "packages / app-a / src / Button.tsx" belongs to the code module "app-a" sub-application.

[0046] Understandably, proprietary coding standards can be coding conventions, technical constraints, or architectural limitations unique to a specific code module. In large software projects, different code modules may employ different technology stacks or coding schemes. For example, sub-application A in the same code repository might use regular CSS for style development, while sub-application B is forced to use the CSS Modules scheme. In this case, "using the CSS Modules scheme" is the proprietary coding standard for sub-application B. It's important to note that proprietary coding standards are only valid for the code files of their respective code modules and do not apply to other code modules.

[0047] In its implementation, the device can read the storage path of the currently edited file through the file system interface to obtain file path information. This file path information is then used as a matching criterion to search for a rule file in the path-aware rule layer that matches the file path information. The path-aware rule layer contains multiple candidate rule files, each with a path matching pattern declared in its header metadata. The device can compare the file path information with the path matching patterns of each candidate rule file one by one, and determine the candidate rule file whose path matching pattern successfully matches the file path information as the target rule file.

[0048] Step S30: Obtain the global rule file from the global knowledge layer, and generate the context prompt word set of the currently edited file based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project.

[0049] It should be noted that the global rule file can be a rule file stored in the global knowledge layer that carries general project specifications applicable to the entire software project, and it can be stored in the root directory of the code repository. In this embodiment, the content of the global rule file is unconditionally loaded during each AI programming interaction, without depending on any path matching conditions or explicit user references.

[0050] Understandably, general project guidelines can be universal standards applicable to all code modules of a software project, including coding constraints, architectural constraints, and technical specifications. In practice, general project guidelines differ from specific coding standards; they are not targeted at specific code modules but have general binding force on the entire software project, such as global requirements like "the use of the 'any' type is prohibited" and "all components must include unit tests."

[0051] It should be understood that the contextual prompt set can be a set of rule content composed of both the global rule file and the target rule file. In this embodiment, the contextual prompt set can serve as the input context for the preset code generation model, providing the preset code generation model with complete project knowledge of the software project in which the currently edited file is located, including general project specifications and module-specific coding specifications.

[0052] In its implementation, the device can access the global knowledge layer in the target project's knowledge base and read pre-stored global rule files from the storage location corresponding to the global knowledge layer. These global rule files can be a single file or multiple files. During each interaction, the device unconditionally loads all global rule files from the global knowledge layer to ensure that all common project specifications are injected into the context prompt word set.

[0053] Step S40: Inject the set of context prompts into the preset code generation model so that the preset code generation model generates code based on the set of context prompts.

[0054] It should be noted that the preset code generation model can be a pre-trained or configured large language model used to generate code based on input prompts. It can be a general-purpose large language model, such as the GPT series models or the Claude model, or a specialized model that has been fine-tuned or specifically configured for code generation tasks. This embodiment does not impose any restrictions on this. In practical applications, the preset code generation model can provide code generation services through an Application Programming Interface (API), with the prompt text as input and the generated program code text as output.

[0055] In practical applications, the device can concatenate the contents of the global rule file and the target rule file in a preset order to generate a complete set of contextual hints. This set includes both general project specifications applicable to the entire software project and the specific coding specifications of the code module to which the currently edited file belongs. The device can then inject this set of contextual hints into a preset code generation model, enabling the model to generate code based on this set. Specifically, the device can send the set of contextual hints as part of the input to the preset code generation model via an application programming interface (API). Upon receiving the set, the preset code generation model uses it as a reference context for code generation, adhering to the constraints of the included general project specifications and specific coding specifications, thus outputting code that meets the project's coding requirements.

[0056] Furthermore, prior to step S40, the procedure also includes: Step S41: Extract code generation description information from the code generation instructions entered by the user.

[0057] Understandably, code generation instructions can be natural language instructions entered by the user to instruct the device to perform code generation tasks. These instructions may include a description of the specific tasks the user expects to perform, such as adding components, modifying functions, fixing defects, or writing unit tests.

[0058] It is also understood that the code generation description information can be core information extracted from the code generation instruction, used to characterize the user's specific code generation needs. This information may include, but is not limited to, the name of the component to be generated, the functional description of the function to be generated, the input and output parameter definitions of the interface to be generated, and the business logic rules involved in the code to be generated. In this embodiment, the code generation description information can remove redundant expressions (such as interjections, formatting characters, etc.) unrelated to the generation task in the code generation instruction, while retaining key descriptions that clearly indicate what kind of code the preset code generation model should generate. For example, for the code generation instruction "Please help me generate a React Button component that receives children and onClick attributes", the code generation description information extracted by the device could be "Generate a React Button component that receives children and onClick attributes".

[0059] In practical implementation, the device can acquire code generation instructions input by the user through the input interface, perform natural language processing on the acquired instructions, identify and extract the core information representing the specific code generation requirements, and then remove interjections, formatting characters, redundant descriptions, and other content not directly related to the code generation task to obtain the final code generation description information. Alternatively, the device can employ a model-based semantic understanding method, calling a natural language understanding model to perform intent recognition and slot filling on the code generation instructions, using the identified intent and slot information as the code generation description information.

[0060] Step S42: Generate code generation prompts based on the context prompt set and the code generation description information.

[0061] It should be understood that the code generation prompt can be a complete prompt text that conforms to the input format requirements of a preset code generation model, composed of a set of context prompts and code generation description information. In this embodiment, the code generation prompt is the final input obtained by fusing the project context information (i.e., the set of context prompts) with the user's specific task requirements (i.e., the code generation description information) before the preset code generation model performs the code generation operation.

[0062] Step S40 includes: Step S43: Inject the code generation prompt into the preset code generation model so that the preset code generation model generates code based on the code generation prompt.

[0063] In practical applications, the device can combine a set of contextual hints and code generation description information according to a preset format, and use the concatenated content as code generation hints. Then, the device can send the code generation hints to a preset code generation model via an application programming interface (API). Upon receiving the hints, the preset code generation model can parse them as complete input, read the general project specifications and proprietary coding standards contained in the contextual hints set, and simultaneously read the user's specific task requirements contained in the code generation description information. Based on the general project specifications, proprietary coding standards, and user's specific task requirements, the model performs code generation inference and outputs code that conforms to the project specifications and meets the user's task requirements.

[0064] In this embodiment, code generation description information is extracted from the user-input code generation command, and this description information is combined with a set of contextual prompts to generate code generation prompts. These prompts are then injected into a preset code generation model. This allows the model to reason based on both project specification constraints and the user's specific task requirements when generating code, resulting in code that conforms to both project coding standards and user functional requirements. Furthermore, since the contextual prompts pre-loaded with module-specific coding standards and globally applicable project standards matching the currently edited file, users do not need to repeatedly describe the project background and coding constraints in each code generation command. This reduces the complexity and risk of omissions in user input commands, improving code generation efficiency and accuracy.

[0065] Furthermore, after step S40, the method further includes: Step S51: In response to the rule change operation triggered based on the target rule file, the changed target rule file is submitted to the target project knowledge base. The rule file in the target project knowledge base is stored in the same code repository as the source code of the software project.

[0066] It should be noted that rule change operations can be user actions that modify, delete, or add content to the target rule file. For example, a user can edit the text content of the target rule file through an integrated development environment or a text editor and then save it; a user can submit rule file changes through command-line tools of a version control system (such as the git add and git commit commands); a user can submit pull requests or merge requests for rule file changes through the web interface of a code hosting platform (such as GitHub or GitLab). This embodiment does not limit these actions.

[0067] Understandably, the modified target rule file can be a target rule file that has been modified through rule change operations, and whose content has changed. It contains the new rule content modified by the user, which reflects the latest requirements of the project's coding standards or technical constraints. For example, if the user changes "component names are prefixed with Admin" to "component names are prefixed with Adm" in the target rule file, the modified rule file is the modified target rule file.

[0068] It should be noted that in this embodiment, all rule files (including global rule files, target rule files, and task rule files) contained in the target project knowledge base are stored together with the source code files of the software project in a code repository managed by the same version control system. This code repository can be a Git repository or a code repository of another version control system. Furthermore, rule files and source code can be stored in different directory structures within the code repository. For example, global rule files can be stored in the root directory of the code repository, rule files for the path-aware rule layer can be stored in the hidden configuration directory of the code repository, and source code can be stored in the source code directory of the code repository. However, rule files and source code can share the same version management system; that is, commits to rule files and commits to source code files are recorded in the same commit log and are managed by the same set of branching and tagging strategies.

[0069] In practice, when a user modifies, deletes, or adds content to the target rule file, the device recognizes this as a rule change operation, retrieves the modified target rule file, and then submits it as a new commit to the target project knowledge base via a version control system commit command. Since the rule files in the target project knowledge base are stored in the same code repository as the software project's source code, the modified target rule file shares the repository's storage space and version management mechanism with the source code after being submitted.

[0070] Step S52: In the commit history of the code repository, associate the change information of the target rule file with the version information of the corresponding source code.

[0071] It should be understood that the commit record of the code repository can be a version record entry generated each time code is committed in the code repository. It may include information such as commit identifier, committer information, commit timestamp, commit description information, and the set of file changes included in this commit. This embodiment does not limit this.

[0072] Understandably, the change information of the target rule file can be information describing the specific changes that have occurred in the target rule file during this rule change operation. This information may include, but is not limited to, the changed file name, the file content or file hash value before the change, the file content or file hash value after the change, the specific line numbers involved in the change and the differences in line content, the executor of the change operation, and the execution time of the change operation.

[0073] It is also understandable that source code version information can be identification information used to uniquely identify the version status of the software project's source code in the code repository. Specifically, it can be a commit identifier or tag in the code repository. In practical applications, when changes to a target rule file are associated with a certain version of the source code, the device can associate and record the change information of the target rule file with the version information of the source code to clarify under which code version the target rule file takes effect.

[0074] In practical applications, the device can obtain the commit identifier corresponding to the current commit operation and the code version information corresponding to this commit, and associate the change information of the target rule file (including the changed file name, the differences in content before and after the change, the executor of the change, the change time, etc.) with the version information of the source code. A record entry containing the change information of the target rule file and the version information of the source code is generated in the commit record of the code repository, so that every change of the target rule file can be traced back to the specific code version.

[0075] Step S53: Once the association is complete, perform a consistency check between the modified target rule file and the associated source code changes.

[0076] It should be noted that source code changes can be file content changes in the source code of the software project that occur in the same commit, as associated with a rule change operation. Examples include adding or deleting source code files, modifying code content in existing source code files, or modifying configuration information in source code files. In this embodiment, associating source code changes with the changed target rule file through the same commit ensures synchronized evolution between the rule file and the source code.

[0077] In practical implementation, after completing the association and recording of change information and version information, the device can obtain the content of the modified target rule file and the content of the source code file involved in the source code change. Then, it performs a consistency check on the coding specifications or technical constraints described in the modified target rule file and the technical solutions adopted in the source code change. This consistency check can include syntax-level checks (e.g., whether the modified target rule file conforms to the predetermined rule file format specification), semantic-level checks (e.g., whether the module paths or technical component names referenced in the modified target rule file are consistent with the module paths or technical component names actually used in the source code change), and logical-level checks (e.g., whether the coding constraints described in the modified target rule file are compatible with the code implementation method of the source code change).

[0078] Step S54: If the verification passes, merge the modified target rule file and the source code changes into the main branch of the code repository.

[0079] Understandably, the main branch of a code repository can be the base branch used to carry the official release version of the software project, such as the "main," "master," or "trunk" branches in a Git version control system. In the code repository workflow, developers typically start by developing on a feature branch, and after development is completed and approved, the changes on the feature branch are merged into the main branch.

[0080] In practical applications, after the consistency verification is passed, the device can merge the changed target rule file and the files involved in the source code changes into the main branch of the code repository. After the merge is completed, the changed target rule file and the source code changes officially become part of the main branch, and all subsequent code copies pulled from the main branch will contain the changed target rule file.

[0081] In this embodiment, the modified target rule file is submitted to the target project knowledge base stored in the same code repository as the source code. The commit history of the code repository is linked to record the change information of the target rule file and the corresponding source code version information. A consistency check is then performed on the modified target rule file and the associated source code changes. Once the check passes, the modified target rule file and the source code changes are merged into the main branch of the code repository. This ensures that the target rule file and source code evolve synchronously in version management, change review, and merging processes, thereby avoiding inconsistencies and knowledge drift between the rule file and the code implementation. Furthermore, because each change to the rule file is linked to a specific source code version and undergoes consistency verification before merging, the change history of the rule file is traceable and auditable.

[0082] Furthermore, after step S40, the method further includes: Step S61: In response to the rule creation instruction, obtain the rule type and module identifier specified by the rule creation instruction.

[0083] It should be understood that a rule creation instruction can be a user-inputted operation command that instructs the device to create a new rule file. This command can trigger the rule file creation process, enabling the user to add a new rule file to the target project knowledge base. In this embodiment, the rule creation instruction can specifically be a natural language description entered by the user in the dialog interface, such as "create a new rule file for the shared module".

[0084] Understandably, the rule type can be the category to which the new rule file created by the rule creation instruction belongs. It can be used to determine which level in the target project knowledge base the new rule file should be stored in, and what template structure and triggering mechanism the rule file should adopt. In this embodiment, the rule type can include global rules, module rules, and task rules, where the global rule type corresponds to the global knowledge layer, the module rule type corresponds to the path-aware rule layer, and the task rule type corresponds to the task rule layer.

[0085] It is also understood that the module identifier can be identification information used to uniquely identify a code module in a software project. It can be the name of the code module (e.g., "app-a" or "shared"), the relative path of the code module in the code repository (e.g., "app-a" or "shared"), etc. This embodiment does not limit this.

[0086] Step S62: If the rule type is a module rule, then determine the module path based on the module identifier.

[0087] It should be understood that module rules can be rule types corresponding to the path-aware rule layer. In this embodiment, module rules are used to carry the exclusive coding specifications of a specific code module. The corresponding rule file is loaded through file path matching mode, that is, the content of the rule file will only be loaded into the context prompt word set when the file path information of the currently edited file matches the path matching mode of the rule file.

[0088] It should also be understood that the module path can be the storage directory path of the code module corresponding to the module identifier in the code repository. It can be a relative path under the root directory of the code repository, such as "packages / app-a / ".

[0089] Step S63: Create a module rule file template corresponding to the module path in the preset directory of the software project's code repository. The module rule file template includes a path matching mode input field.

[0090] It should be noted that the preset directory can be a specific directory location in the code repository that is pre-specified for storing various rule files. In this embodiment, the global rule file of the global knowledge layer can be stored in the root directory of the code repository, such as " / AGENTS.md"; the module rule file of the path-aware rule layer can be stored in the hidden configuration directory of the code repository, such as " / .cursor / rules / "; and the task rule file of the task rule layer can be stored in the document directory of the code repository, such as " / docs / ai-collab / ".

[0091] It should be noted that the module rule file template can be a file structure framework used to guide users in creating module rule files that conform to the standard format requirements. It can contain the fixed structure and populateable fields necessary for creating new module rule files, such as the fixed format declaration of structured metadata in the file header, path matching pattern input fields, rule content editing areas, etc.

[0092] It should also be noted that the path matching pattern input field can be an input area in the module rule file template used by users to input the file path range information to which the module rules apply. It can be presented in key-value pair format, markup language tag format, or form input box format. In this embodiment, the content of the path matching pattern input field can be filled in the form of a glob wildcard expression. For example, the path matching pattern input field exists in the header of the rule file as a "globs:" field in YAML format. Users can fill in one or more glob path matching patterns after this field, such as "globs: packages / admin-app / ". / ".

[0093] Step S64: Receive the new rule content input by the user based on the module rule file template.

[0094] Understandably, the newly added rule content can be the specific rule content information input by the user based on the module rule file template. Specifically, it can be the content obtained after filling in the fillable fields in the module rule file template, including the content filled in the path matching mode input field, the specific description of the coding specification clause, etc.

[0095] Step S65: Generate a new rule file based on the new rule content, and store the new rule file in the preset directory to update the target project knowledge base.

[0096] It should be understood that a new rule file can be a complete rule file that conforms to a predefined format specification and is generated based on the new rule content. In this embodiment, the new rule file is the final usable rule file generated by the device after adding structured metadata in the file header and performing format validation based on the new rule content.

[0097] In this embodiment, when the rule type is a module rule, after determining the module path based on the module identifier, a module rule file template containing a path matching mode input field is created in a preset directory of the code repository. Then, the new rule content input by the user is received and the new rule file is generated and stored in the preset directory to update the target project knowledge base. This allows users to complete the creation and access of new rules simply by filling in the rule content according to the template instructions. This enables the expansion of rules without modifying the code or restarting the service, thereby reducing the complexity and threshold of rule iteration and project knowledge base maintenance.

[0098] In its implementation, when a user inputs a rule creation command through the input interface, the device can obtain and parse the command, extracting the rule type and module identifier specified by the command. If the rule type is a module rule, the device can use the module identifier as the search criterion to locate the storage directory of the code module corresponding to the module identifier in the software project's code repository, and determine the path of that storage directory as the module path. Subsequently, the device can create a new file in a directory specifically designated for storing path-aware rule layer files in the code repository. This file serves as the module rule file template, containing a path matching mode input field for the user to specify the applicable file path range for the module rule file. After creating the module rule file template, the device can present it to the user, allowing the user to input specific rule content according to their needs and fill in specific coding standard clauses in the rule content editing area to obtain the new rule content. After receiving new rule content, the device organizes it according to a predefined rule file format, generating a complete rule file. The rule file header contains structured metadata, including a description of the rule's purpose, path matching mode (read from the path matching mode input field), and hierarchical information (i.e., the path-aware rule layer). The device then stores the complete new rule file in a preset directory. Once stored, the new rule file becomes part of the path-aware rule layer in the target project's knowledge base. During subsequent code generation, the device can traverse the preset directory to read and match the new rule file.

[0099] This embodiment provides a code generation method. The method discloses obtaining the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, the method determines a target rule file matching the currently edited file from the path-aware rule layer. The target rule file carries the exclusive coding specifications of the code module to which the currently edited file belongs. The method obtains a global rule file from the global knowledge layer and generates a set of contextual prompts for the currently edited file based on the global rule file and the target rule file. The global rule file carries the general project specifications of the software project. The set of contextual prompts is injected into a preset code generation model so that the preset code generation model generates code based on the set of contextual prompts. Compared to existing methods that use a single rule configuration file to centrally inject all coding specifications of the entire project into a large model so that the large model generates code based on all information, this embodiment uses the path-aware rule layer to accurately match the target rule file based on the path of the currently edited file, loading only the module specification information related to the currently edited file, rather than loading all module information. This avoids irrelevant information interfering with the attention mechanism of the large language model, solving the technical problem that existing code generation methods easily lead to reduced accuracy due to loading all irrelevant information.

[0100] Based on the first embodiment of this application, in the second embodiment of this application, the content that is the same as or similar to that in Embodiment 1 above can be referred to the above description, and will not be repeated hereafter. Based on this, please refer to... Figure 2 , Figure 2 This is a flowchart illustrating the code generation method of this application in Embodiment 2.

[0101] In this embodiment, the step of determining the target rule file matching the currently edited file from the path-aware rule layer based on the file path information of the currently edited file includes steps S21 to S23: Step S21: Traverse all candidate rule files in the path-aware rule layer and read the path matching mode declared in the header metadata of each candidate rule file. The path matching mode is used to describe the range of file paths applicable to the corresponding candidate rule file.

[0102] It should be noted that the candidate rule files can be all the rule files available for matching stored in the path-aware rule layer. In this embodiment, the path-aware rule layer stores multiple rule files, each corresponding to a code module. These rule files are all in a candidate state before path matching is performed. The device can traverse each candidate rule file and perform matching judgments one by one to determine which candidate rule files match the file path information of the currently edited file.

[0103] It should be understood that header metadata can be structured data information located in the header area of ​​the candidate rule file, which may include, but is not limited to, fields such as path matching mode and permanent effective status identifier.

[0104] It should also be understood that the path matching pattern can be matching rule information declared in the header metadata of a candidate rule file, describing the range of file paths to which the candidate rule file applies. The range of file paths to which the candidate rule file applies can be the set of code file paths targeted by the coding specification content of the candidate rule file. In practical applications, devices can limit the range of file paths to which a candidate rule file applies through the path matching pattern in the header metadata. For example, if a candidate rule file's path matching pattern is "packages / app-a / / If the candidate rule file is set to "packages / app-a / ", then the applicable file path range is all files under the "packages / app-a / " directory.

[0105] In its implementation, the device can access the storage directory corresponding to the path-aware rule layer, obtain a file list of all candidate rule files stored in that directory, and sequentially traverse each candidate rule file in the file list according to a preset order (e.g., alphabetical order of file names or order of file modification times). For the currently traversed candidate rule file, the device can read the file header area of ​​the candidate rule file, parse the structured metadata from the file header area, and extract the path matching pattern from the structured metadata.

[0106] Step S22: Perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns.

[0107] Understandably, the file path information can be the storage location of the currently edited file in the code repository, specifically a relative path based on the root directory of the code repository.

[0108] It is also understood that wildcard path matching can be a path comparison method based on wildcard expressions, which can be used to determine whether a file path matches a path pattern. In this embodiment, wildcard path matching can use glob wildcard syntax for matching and judgment. In glob wildcard syntax, " "Used to match any number of any characters (excluding path separators)," This function is used to match any number of any characters (including path separators, i.e., matching subdirectories at any level), and returns a boolean value (including whether the match is successful or not), rather than a probabilistic similarity calculation.

[0109] In practical applications, the device can obtain the file path information of the currently edited file and perform wildcard path matching between the file path information and the path matching pattern of each candidate rule file. Specifically, the device can use the file path information as the string to be matched and the path matching pattern as the matching rule, and compare them according to the matching rules of the glob wildcard syntax to determine whether the file path information meets the file path range conditions limited by the path matching pattern.

[0110] Step S23: If the match is successful, the first candidate rule file that is successfully matched is determined as the target rule file that matches the currently edited file.

[0111] It should be understood that the first candidate rule file can be a candidate rule file in the candidate rule file whose path matching pattern successfully matches the file path information. In this embodiment, when it is determined that the path matching pattern of a certain candidate rule file successfully matches the file path information, the device can determine the candidate rule file as the first candidate rule file and determine the first candidate rule file as the target rule file that matches the currently edited file.

[0112] Furthermore, after step S22, the method further includes: Step S241: If the matching fails, obtain the permanent effective status identifier of each candidate rule file. The permanent effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective.

[0113] It should be noted that the persistent effective status identifier can be a flag information contained in the header metadata of the candidate rule file, used to characterize whether the candidate rule file is unconditionally effective. It can be named "alwaysApply" or other names with the same function. In this embodiment, the persistent effective status identifier can be a boolean identifier field used to indicate whether the candidate rule file is forcibly loaded regardless of the file path matching result. Its value includes two states: valid and invalid. When the persistent effective status identifier is in the valid state, the candidate rule file needs to be loaded and included in the context prompt word set, regardless of whether the file path information of the currently edited file successfully matches the path matching pattern of the candidate rule file.

[0114] In the specific implementation, after the device performs wildcard path matching between the file path information of the currently edited file and the path matching patterns of all candidate rule files, if the path matching patterns of all candidate rule files fail to match the file path information (i.e., the matching fails), it can read the header metadata of each candidate rule file one by one and extract the resident effective status identifier of each candidate rule file from the header metadata.

[0115] Step S242: Detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state.

[0116] Understandably, the valid state can be one of the values ​​of the resident valid state identifier that indicates the candidate rule file needs to be unconditionally effective. In this embodiment, when the resident valid state identifier of the candidate rule file is in the valid state, the candidate rule file is not constrained by the path matching result, but is forcibly loaded during context assembly.

[0117] Step S243: If it exists, then the second candidate rule file is determined as the target rule file that matches the currently edited file.

[0118] It should be understood that the second candidate rule file can be a candidate rule file whose resident effectiveness status indicator is in a valid state. In practical applications, the device can detect each candidate rule file based on the value of the resident effectiveness status indicator obtained from each candidate rule file to determine whether the resident effectiveness status indicator of the candidate rule file is in a valid state. When the device detects a second candidate rule file with a valid resident effectiveness status indicator, it can determine this second candidate rule file as the target rule file that matches the currently edited file. Furthermore, if there are multiple candidate rule files with valid resident effectiveness status indicators, all valid candidate rule files can be determined as target rule files.

[0119] In this embodiment, when wildcard path matching fails, the resident effective status identifier of each candidate rule file is obtained, and it is detected whether there is a second candidate rule file with a resident effective status identifier in a valid state. If there is, the second candidate rule file is determined as the target rule file. This ensures that mandatory rules that do not depend on a specific file path and need to take effect on all code files (such as secure coding standards, global prohibitions, etc.) can still be reliably loaded and included in the context prompt word set even if the path matching fails. This ensures the complete injection of hard constraint rules and avoids the rule omission problem caused by the path matching conditions not completely corresponding to the actual file storage location. This is beneficial to improving the accuracy and reliability of subsequent code generation.

[0120] In the specific implementation, refer to Figure 3 , Figure 3 This is a flowchart illustrating the generation process of the contextual prompt word set in the code generation method of this application. For example... Figure 3 As shown, when AI interaction begins, the context scheduler is activated. At this time, the device can synchronously obtain input information such as the current editing file path and user instructions / explicit reference lists through the context scheduler. Subsequently, the device can load all rule files of the global knowledge layer through the context scheduler, including AGENTS.md, CLAUDE.md, etc., and add the contents of these global rule files to the context set. Then, the context scheduler can traverse each rule file in the path-aware rule layer and perform glob wildcard matching between the current editing file path and the path matching pattern declared in the header metadata of the currently traversed rule file to determine whether the current editing file path matches the path matching pattern of the rule file. If the current editing file path matches the path matching pattern of the rule file (i.e., a successful match), the rule file is activated, and the contents of the rule file are added to the context set. If the current editing file path does not match the path matching pattern of the rule file (i.e., a failed match), the persistent effective status flag of the rule file can be further checked to see if it is true (i.e., effective). If the resident effective status flag of the rule file is true, the current rule file is forcibly activated, and its content is added to the context set. If the resident effective status flag is false (i.e., invalid), the current rule file is skipped, and its content is not added to the context set. Next, the context scheduler iterates through each explicit reference in the user instruction / explicit reference list, reads the complete content of the task rule file corresponding to each explicit reference, and adds the content of that task rule file to the context set. At this point, the context set includes rule files from the global rule layer, path-aware rule layer, and task rule layer. Afterward, the context scheduler deduplicates the context set, removing duplicate rule entries, and sorts the content according to a predetermined hierarchical order: global knowledge layer, path-aware rule layer, and task rule layer, generating the final context prompt word set. Finally, the context scheduler combines the context prompt word set with the user-input code generation description information to generate target prompt words, which are then sent to the large language model to generate code or modification suggestions based on the target prompt words.

[0121] In this embodiment, by traversing all candidate rule files in the path-aware rule layer and reading the path matching patterns declared in the header metadata of each candidate rule file, wildcard path matching is performed between the file path information of the currently edited file and each path matching pattern. When a match is successful, the first candidate rule file that matches successfully is determined as the target rule file. This allows for the accurate location of the rule file corresponding to the code module to which the currently edited file belongs from multiple candidate rule files based on the actual path of the currently edited file. Wildcard path matching is a deterministic matching logic, with stable and reproducible matching results. Compared to existing probabilistic retrieval methods that rely on semantic similarity calculation, it ensures that the hard constraint rules of the current module are reliably loaded, avoiding specification omissions caused by the instability of probabilistic recall. Furthermore, since the corresponding rule is loaded only when a path match is successful, without loading all candidate rule files, the accuracy of context assembly is ensured.

[0122] Based on the first and / or second embodiments of this application, in the third embodiment of this application, the content that is the same as or similar to the above embodiments can be referred to the above description, and will not be repeated hereafter. Based on this, please refer to... Figure 4 , Figure 4 This is a flowchart illustrating the code generation method of this application in Embodiment 3.

[0123] In this embodiment, the target project knowledge base further includes a task rule layer; before step S30, step S31 is also included: Step S31: In response to the explicit task invocation command input by the user, obtain the task rule file corresponding to the explicit task invocation command from the task rule layer. The task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command.

[0124] It should be noted that explicit task invocation commands can be user-inputted operation commands used to actively invoke specific task rules in the task rule layer. They have a clear invocation intent and are used to instruct the device to load specific task rule files from the task rule layer.

[0125] It should be noted that the task rule layer can be a logical hierarchy within the target project knowledge base used to store task rule files associated with specific development scenarios. For example, the task rule layer can be used to store operation flow templates for specific types of development tasks (such as adding a page, refactoring a module, adding unit tests, etc.). In this embodiment, the loading conditions of the task rule layer differ from those of the global knowledge layer and the path-aware rule layer. The rule files of the global knowledge layer are loaded unconditionally, the rule files of the path-aware rule layer are loaded based on file path matching, and the rule files of the task rule layer are loaded based on explicit user references.

[0126] It should also be noted that the task rule file can be a rule file stored in the task rule layer that corresponds to the explicit task invocation instruction. In this embodiment, the task rule file stores standardized task operation procedures for a specific development scenario, which are used to guide the preset code generation model to generate code according to predetermined process steps during code generation.

[0127] Understandably, scenario-based task specifications can be standardized task execution processes and operational constraints for specific development scenarios. They can describe standardized process information such as the order of operation steps that developers should follow, the list of files that need to be modified, and the list of configuration items that need to be updated in a specific development scenario.

[0128] In practical applications, the device can monitor user input commands through the input interface in real time. When an explicit task invocation command is detected, it parses the command and identifies the task reference marker contained within it. Then, based on the parsed task reference marker, the device searches for the corresponding task rule file in the task rule layer and identifies the found file as the task rule file corresponding to the explicit task invocation command.

[0129] Further, the step of obtaining the task rule file corresponding to the explicit task invocation instruction from the task rule layer includes: Step S311: Perform character parsing on the explicit task invocation instruction to identify whether the explicit task invocation instruction contains a task reference marker.

[0130] It should be noted that the task reference marker can be a specific character sequence used in explicit task invocation instructions to identify the reference relationship of task rule files. It can consist of two parts: a preset prefix character and a task identifier name. The preset prefix character is used to indicate to the device that the following characters are task reference markers, such as "@sop:", "@task:", " / sop" and other pre-agreed character sequences. The task identifier name is used to uniquely identify a task rule file, such as "new-page", "add-module" and other names used to identify specific task rule files.

[0131] Step S312: If included, then obtain the task rule file corresponding to the explicit task invocation instruction from the task rule layer according to the task reference tag.

[0132] In practical applications, after receiving the explicit task invocation command input by the user, the device can scan the text content of the explicit task invocation command character by character according to preset character parsing rules, detecting whether there is a character sequence in the text content that matches the preset prefix character. If the device detects a character sequence in the text content that matches the preset prefix character, it can identify this character sequence as the starting part of the task reference mark, and extract the subsequent continuous character sequence (the continuous character sequence is usually terminated by a space, newline, or preset end character) starting from the end position of the preset prefix character. The extracted continuous character sequence is determined as the task identifier name, and then the combination of the preset prefix character and the task identifier name is determined as the task reference mark. Then, the device can extract the task identifier name from the task reference mark, and use the task identifier name as the search basis to search for the task rule file corresponding to the task identifier name in the task rule layer. Finally, the found task rule file is determined as the task rule file corresponding to the explicit task invocation command. In addition, if no character sequence matching the preset prefix character is detected in the text content of the explicit task invocation command, it is determined that the explicit task invocation command does not contain a task reference mark.

[0133] In this embodiment, by parsing the explicit task invocation command, it is identified whether the explicit task invocation command contains a task reference mark. If it does, the corresponding task rule file is obtained from the task rule layer according to the task reference mark. This allows users to trigger the loading of a specific task rule file simply by embedding a concise reference mark in the natural language command, thereby reducing the complexity of the user's operation.

[0134] Accordingly, step S30 includes: Step S32: Generate a set of contextual prompts for the currently edited file based on the global rule file, the target rule file, and the task rule file.

[0135] In this embodiment, the device can further incorporate task rule files into the context assembly scope based on the global rule file and the target rule file. Specifically, the device can combine and assemble the global rule file, the target rule file, and the task rule file in a predetermined hierarchical order to generate a set of context prompt words for the currently edited file. The generated set of context prompt words includes general project specifications applicable to the entire software project, the specific coding specifications of the code module to which the currently edited file belongs, and the scenario-based task specifications triggered by explicit task invocation instructions.

[0136] Further, step S32 includes: Step S321: Add the global rule file, the target rule file, and the task rule file to the initial context set respectively.

[0137] Understandably, the initial context set can be a collection data structure used to temporarily store the contents of the loaded rule files. It can be a dynamically expandable collection, allowing the device to add the contents of the loaded rule files one by one to the initial context set during the process of traversing and loading rule files at each level.

[0138] Step S322: Sort the initial context set according to the preset hierarchical priority order to generate a sorted context set.

[0139] It should be understood that the preset hierarchical priority order can be a pre-defined rule used to specify the order in which the global knowledge layer, path-aware rule layer, and task rule layer are arranged in the context prompt word set; correspondingly, the sorted context set can be a set obtained by sorting the contents of the initial context set according to the preset hierarchical priority order. In this embodiment, the preset hierarchical priority order can be set according to the importance and scope of application of each level of rules, for example, it can be set that the global knowledge layer takes precedence over the path-aware rule layer, and the path-aware rule layer takes precedence over the task rule layer. In practical applications, the device can sort the rule files of different levels stored in the initial context set according to the preset hierarchical priority order to generate a sorted context set, so as to ensure that the contents of the rule files of different levels are arranged in a specific order, thereby forming a clear information hierarchy structure when input into the preset code generation model.

[0140] Step S323: Perform deduplication on the sorted context set to generate the context prompt word set of the currently edited file.

[0141] In practical applications, the device can detect whether there are duplicate rule entries in the sorted context set (e.g., the same rule clause is recorded repeatedly in rule files at different levels), and delete the duplicate rule entries when duplicates are detected, retaining only one copy. The device can then determine the deduplicated set as the context prompt set for the currently edited file. Because each rule entry appears only once in the context prompt set generated after deduplication, redundant information input and resource waste caused by rule duplication can be avoided.

[0142] In this embodiment, the global rule file, target rule file, and task rule file are added to the initial context set, and the initial context set is sorted according to a preset hierarchical priority order to generate a sorted context set. Then, the sorted context set is deduplicated to generate the context prompt word set for the currently edited file. This ensures that the rule content at each level in the final generated context prompt word set is arranged according to a clear priority hierarchy, thereby improving the model's ability to perceive rule hierarchy and the accuracy of code generation. Simultaneously, deduplication ensures that there are no redundant or duplicate rule entries in the context prompt word set, thus avoiding the waste of token resources and model attention distraction caused by repeated rule input, thereby improving the information density and input efficiency of context injection.

[0143] In the specific implementation, refer to Figure 5 , Figure 5 A system architecture diagram provided for the code generation method of this application. (e.g.) Figure 5As shown, the code repository in this application pre-establishes a three-tiered rule file storage system, including a global knowledge layer, a path-aware rule layer, and a task rule layer. The global knowledge layer stores rule files such as AGENTS.md and CLAUDE.md. These rule files carry general project specifications applicable to the entire software project and employ a persistent injection mode, meaning that regardless of the path of the currently edited file, the rule files in the global knowledge layer are unconditionally loaded during each interaction. The path-aware rule layer is stored in the .cursor / rules / directory of the code repository. The rule files in this layer carry the exclusive coding specifications for each code module and employ a glob on-demand trigger mode, meaning that the rule files in the path-aware rule layer are only loaded when the file path information of the currently edited file successfully matches the path matching pattern of the rule files. The task rule layer is stored in the docs / ai-collab / directory of the code repository. The rule files in this layer carry scenario-based task specifications and employ an explicit reference trigger mode, meaning that the rule files in the task rule layer are only loaded when the user instruction contains an explicit reference marker. Subsequently, the context scheduler can be activated at the start of AI interaction, synchronously acquiring input information, including the current editing file path and user commands / explicit references. During context assembly, the context scheduler can first perform a mandatory loading operation, i.e., loading all rule files in the global knowledge layer and adding the contents of all rule files in the global knowledge layer to the context set. Then, the context scheduler can perform a path matching operation. Specifically, the context scheduler can traverse all rule files in the path-aware rule layer, performing a glob wildcard match between the current editing file path and the path matching pattern declared in the header metadata of each rule file. If the match is successful, the rule file is activated, and its contents are added to the context set; if the match fails, it checks whether the resident effective status flag of the rule file is true. If the resident effective status flag is true, the rule file is forcibly activated, and its contents are added to the context set; if the resident effective status flag is false, the current rule file is skipped, and its contents are not added to the context set. Then, the context scheduler can perform explicit referencing operations. Specifically, if the user instruction contains an explicit referencing marker, it can locate and load the corresponding task rule file based on the explicit referencing marker, and add the contents of the task rule file to the context set. Afterward, the context scheduler can deduplicate the context set, removing duplicate rule entries, and assemble the deduplicated content into a minimal context set. This minimal context set is then combined with the user instruction to form a complete target prompt, which is sent to the large language model. The large language model then generates code or modification suggestions that conform to project coding standards and technical constraints based on the target prompt.Furthermore, when the source code in the code repository changes, the target rule file and the source code changes can be updated synchronously in the same pull request. Then, consistency verification is performed through the code review process, and after the verification is confirmed to be correct, the target rule file and the source code changes are merged into the code repository.

[0144] In this embodiment, by obtaining the task rule file corresponding to the explicit task invocation instruction from the task rule layer, and using the task rule file together with the global rule file and the target rule file to generate the context prompt word set of the currently edited file, users can inject standardized operation processes into the context by actively referencing when they need to perform specific types of development tasks. This ensures that the code generated by the subsequent preset code generation model follows the standardized operation process, guaranteeing that the same type of development task adopts consistent process specifications at different execution times and among different developers, avoiding process deviations and omissions caused by individual differences, and thus improving the reliability of code generation.

[0145] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the code generation method of this application. Any simple modifications based on this technical concept are within the protection scope of this application.

[0146] This application also provides a code generation apparatus, please refer to... Figure 6 The code generation device includes: The knowledge base management module 10 is used to obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. The file matching module 20 is used to determine a target rule file that matches the current edited file from the path awareness rule layer based on the file path information of the current edited file. The target rule file is used to carry the exclusive coding specification of the code module to which the current edited file belongs. The prompt word generation module 30 is used to obtain a global rule file from the global knowledge layer, and generate a set of context prompt words for the currently edited file based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The code generation module 40 is used to inject the set of context prompt words into a preset code generation model, so that the preset code generation model generates code based on the set of context prompt words.

[0147] The code generation apparatus provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem that existing code generation methods are prone to reduced accuracy due to the full loading of irrelevant information. Compared with the prior art, the beneficial effects of the code generation apparatus provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features in the code generation apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0148] This application provides a code generation device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the code generation method in Embodiment 1 above.

[0149] The following is for reference. Figure 7 The diagram illustrates a structural schematic of a code generation device suitable for implementing embodiments of this application. The code generation device in embodiments of this application may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Description), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 7 The code generation device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0150] like Figure 7As shown, the code generation device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory 1002 or a program loaded from a storage device 1003 into a random access memory 1004. The random access memory 1004 also stores various programs and data required for the operation of the code generation device. The processing unit 1001, the read-only memory 1002, and the random access memory 1004 are interconnected via a bus 1005. An input / output interface 1006 is also connected to the bus. Typically, the following systems can be connected to the input / output interface 1006: input devices 1007 including, for example, a touchscreen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage devices 1003 including, for example, magnetic tape, hard disk, etc.; and communication devices 1009. Communication device 1009 allows the code generation device to communicate wirelessly or wiredly with other devices to exchange data. Although the figure shows code generation devices with various systems, it should be understood that implementation or possession of all the systems shown is not required. More or fewer systems may be implemented alternatively.

[0151] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from read-only memory 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0152] The code generation device provided in this application, employing the code generation method described in the above embodiments, can solve the technical problem of code generation. Compared with the prior art, the beneficial effects of the code generation device provided in this application are the same as those of the code generation method described in the above embodiments, and other technical features of the code generation device are the same as those disclosed in the method of the previous embodiment, and will not be repeated here.

[0153] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.

[0154] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0155] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the code generation method in the above embodiments.

[0156] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.

[0157] The aforementioned computer-readable storage medium may be included in the code generation device; or it may exist independently and not be assembled into the code generation device.

[0158] The aforementioned computer-readable storage medium carries one or more programs. When these programs are executed by a code generation device, the code generation device: obtains the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, it determines a target rule file matching the currently edited file from the path-aware rule layer. The target rule file is used to carry the exclusive coding specifications of the code module to which the currently edited file belongs. It obtains a global rule file from the global knowledge layer and generates a set of context prompt words for the currently edited file based on the global rule file and the target rule file in the global knowledge layer. The global rule file is used to carry the general project specifications of the software project. It injects the set of context prompt words into a preset code generation model so that the preset code generation model generates code based on the set of context prompt words.

[0159] Computer program code for performing the operations of this application can be written in one or more programming languages ​​or a combination thereof. These programming languages ​​include object-oriented programming languages—such as Python, Java, Smalltalk, and C++—and conventional procedural programming languages—such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0160] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0161] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.

[0162] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the above-described code generation method. This solves the technical problem that existing code generation methods are prone to reduced accuracy due to the full loading of irrelevant information. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the code generation method provided in the above embodiments, and will not be repeated here.

[0163] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the code generation method described above.

[0164] The computer program product provided in this application solves the technical problem that existing code generation methods are prone to reduced accuracy due to the full loading of irrelevant information. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as those of the code generation method provided in the above embodiments, and will not be repeated here.

[0165] The above description is only a part of the embodiments of this application and does not limit the scope of this application. All equivalent structural transformations made under the technical concept of this application and using the content of this application specification and drawings, or direct / indirect applications in other related technical fields, are included within the protection scope of this application.

[0166] This invention discloses A1. A code generation method, the method comprising: Obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, a target rule file matching the currently edited file is determined from the path-aware rule layer. The target rule file is used to carry the exclusive coding specifications of the code module to which the currently edited file belongs. The global rule file is obtained from the global knowledge layer, and a set of context prompt words for the currently edited file is generated based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The set of contextual prompts is injected into a preset code generation model so that the preset code generation model generates code based on the set of contextual prompts.

[0167] A2. The method as described in A1, wherein the step of determining the target rule file matching the currently edited file from the path-aware rule layer based on the file path information of the currently edited file includes: Traverse all candidate rule files in the path-aware rule layer and read the path matching pattern declared in the header metadata of each candidate rule file. The path matching pattern is used to describe the range of file paths applicable to the corresponding candidate rule file. Perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns; If a match is successful, the first candidate rule file that matches successfully will be determined as the target rule file that matches the currently edited file.

[0168] A3. As described in A2, after the step of performing wildcard path matching between the file path information of the currently edited file and each of the path matching patterns, the method further includes: If the matching fails, the permanent effective status identifier of each candidate rule file is obtained. The permanent effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective. Detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state; If it exists, the second candidate rule file is determined as the target rule file that matches the currently edited file.

[0169] A4. As described in A1, the target project knowledge base further includes a task rule layer; before the step of generating the context prompt word set of the currently edited file based on the global rule file and the target rule file, the method further includes: In response to an explicit task invocation command input by the user, the task rule file corresponding to the explicit task invocation command is obtained from the task rule layer. The task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command. The process of generating the contextual prompt word set for the currently edited file based on the global rule file and the target rule file includes: The contextual prompt word set for the currently edited file is generated based on the global rule file, the target rule file, and the task rule file.

[0170] A5. As described in A4, the step of obtaining the task rule file corresponding to the explicit task invocation instruction from the task rule layer includes: The explicit task invocation instruction is parsed to identify whether it contains a task reference marker. If included, the task rule file corresponding to the explicit task invocation instruction is obtained from the task rule layer based on the task reference tag.

[0171] A6. The method as described in A4, wherein the step of generating the set of contextual prompt words for the currently edited file based on the global rule file, the target rule file, and the task rule file includes: Add the global rule file, the target rule file, and the task rule file to the initial context set respectively; The initial context set is sorted according to a preset hierarchical priority order to generate a sorted context set. The sorted context set is deduplicated to generate the context prompt word set for the currently edited file.

[0172] A7. The method as described in A1, prior to the step of injecting the context prompt word set into the preset code generation model so that the preset code generation model generates code based on the context prompt word set, further comprising: Extract code generation description information from user-input code generation instructions; Generate code generation prompts based on the context prompt set and the code generation description information; The step of injecting the set of contextual prompts into a preset code generation model, so that the preset code generation model generates code based on the set of contextual prompts, includes: The code generation prompt is injected into a preset code generation model so that the preset code generation model generates code based on the code generation prompt.

[0173] A8. The method as described in any one of A1 to A7, further comprising, after the step of injecting the context prompt word set into a preset code generation model so that the preset code generation model generates code based on the context prompt word set: In response to a rule change operation triggered based on the target rule file, the modified target rule file is submitted to the target project knowledge base, and the rule file in the target project knowledge base is stored in the same code repository as the source code of the software project; The commit history of the code repository records the change information of the target rule file and the version information of the corresponding source code. Once the association is complete, a consistency check is performed on the modified target rule file and the associated source code changes; If the verification passes, the modified target rule file and the source code changes will be merged into the main branch of the code repository.

[0174] A9. The method as described in any one of A1 to A7, further comprising, after the step of injecting the context prompt word set into a preset code generation model so that the preset code generation model generates code based on the context prompt word set: In response to a rule creation instruction, obtain the rule type and module identifier specified by the rule creation instruction; If the rule type is a module rule, then the module path is determined based on the module identifier; Create a module rule file template corresponding to the module path in a preset directory of the software project's code repository. The module rule file template includes a path matching pattern input field. Receive new rule content input by the user based on the module rule file template; A new rule file is generated based on the newly added rule content, and the new rule file is stored in the preset directory to update the target project knowledge base.

[0175] A10. As described in A4, the global knowledge layer, the path-aware rule layer, and the task rule layer are configured with differentiated loading conditions, wherein the rule files of the global knowledge layer are loaded unconditionally, the rule files of the path-aware rule layer are loaded based on the matching result of the file path and the path matching condition, and the rule files of the task rule layer are loaded based on explicit user references.

[0176] The present invention also discloses B11. A code generation apparatus, the apparatus comprising: The knowledge base management module is used to obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. The file matching module is used to determine the target rule file that matches the currently edited file from the path-aware rule layer based on the file path information of the currently edited file. The target rule file is used to carry the exclusive coding specification of the code module to which the currently edited file belongs. The prompt word generation module is used to obtain a global rule file from the global knowledge layer, and generate a set of context prompt words for the currently edited file based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The code generation module is used to inject the set of context prompts into a preset code generation model, so that the preset code generation model generates code based on the set of context prompts.

[0177] B12. The apparatus as described in B11, wherein the file matching module is further configured to traverse all candidate rule files in the path-aware rule layer and read the path matching mode declared in the header metadata of each candidate rule file, wherein the path matching mode is used to describe the range of file paths applicable to the corresponding candidate rule file; The file matching module is also used to perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns; The file matching module is further configured to, if a match is successful, determine the first candidate rule file that has successfully matched as the target rule file that matches the currently edited file.

[0178] B13. The apparatus as described in B12, wherein the file matching module is further configured to, if the matching fails, obtain a resident effective status identifier for each of the candidate rule files, wherein the resident effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective; The file matching module is also used to detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state; The file matching module is further configured to, if it exists, determine the second candidate rule file as the target rule file that matches the currently edited file.

[0179] B14. The apparatus as described in B11, wherein the target project knowledge base further includes a task rule layer; the file matching module is further configured to, in response to an explicit task invocation command input by a user, obtain a task rule file corresponding to the explicit task invocation command from the task rule layer, wherein the task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command; The prompt word generation module is also used to generate a set of context prompt words for the currently edited file based on the global rule file, the target rule file, and the task rule file.

[0180] B15. The apparatus as described in B14, wherein the prompt word generation module is further configured to add the global rule file, the target rule file and the task rule file to the initial context set respectively; The prompt word generation module is also used to sort the content of the initial context set according to a preset hierarchical priority order to generate a sorted context set; The prompt word generation module is also used to perform deduplication processing on the sorted context set to generate the context prompt word set of the currently edited file.

[0181] B16. The apparatus of any one of B11 to B15, wherein the knowledge base management module is further configured to, in response to a rule change operation triggered based on the target rule file, submit the modified target rule file to the target project knowledge base, wherein the rule file in the target project knowledge base and the source code of the software project are stored in the same code repository; The knowledge base management module is also used to associate and record the change information of the target rule file with the version information of the corresponding source code in the commit records of the code repository; The knowledge base management module is also used to perform consistency verification between the modified target rule file and the associated source code changes after the association is completed. The knowledge base management module is also used to merge the modified target rule file and the source code changes into the main branch of the code repository if the verification passes.

[0182] B17. The apparatus of any one of B11 to B15, wherein the knowledge base management module is further configured to, in response to a rule creation instruction, obtain the rule type and module identifier specified by the rule creation instruction; The knowledge base management module is also used to determine the module path based on the module identifier if the rule type is a module rule; The knowledge base management module is also used to create a module rule file template corresponding to the module path in a preset directory of the software project's code repository. The module rule file template includes a path matching mode input field. The knowledge base management module is also used to receive new rule content input by the user based on the rule file template of the module; The knowledge base management module is also used to generate a new rule file based on the new rule content, and store the new rule file in the preset directory to update the target project knowledge base.

[0183] The present invention also discloses C18. A code generation device, the device comprising: a memory, a processor, and a code generation program stored on the memory and executable on the processor, the code generation program being configured to implement the steps of the code generation method as described above.

[0184] The present invention also discloses D19. A storage medium storing a code generation program, wherein the code generation program, when executed by a processor, implements the steps of the code generation method described above.

[0185] The present invention also discloses E20. A computer program product comprising a computer program that, when executed by a processor, implements the steps of the code generation method described above.

Claims

1. A code generation method, characterized in that, The method includes: Obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. Based on the file path information of the currently edited file, a target rule file matching the currently edited file is determined from the path-aware rule layer. The target rule file is used to carry the exclusive coding specifications of the code module to which the currently edited file belongs. The global rule file is obtained from the global knowledge layer, and a set of context prompt words for the currently edited file is generated based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The set of contextual prompts is injected into a preset code generation model so that the preset code generation model generates code based on the set of contextual prompts.

2. The method as described in claim 1, characterized in that, The step of determining the target rule file matching the currently edited file from the path-aware rule layer based on the file path information of the currently edited file includes: Traverse all candidate rule files in the path-aware rule layer and read the path matching pattern declared in the header metadata of each candidate rule file. The path matching pattern is used to describe the range of file paths applicable to the corresponding candidate rule file. Perform wildcard path matching between the file path information of the currently edited file and each of the path matching patterns; If a match is successful, the first candidate rule file that matches successfully will be determined as the target rule file that matches the currently edited file.

3. The method as described in claim 2, characterized in that, After the step of performing wildcard path matching between the file path information of the currently edited file and each of the path matching patterns, the method further includes: If the matching fails, the permanent effective status identifier of each candidate rule file is obtained. The permanent effective status identifier is used to indicate whether the corresponding candidate rule file is unconditionally effective. Detect whether there is a second candidate rule file in each of the candidate rule files where the resident effective status identifier is in a valid state; If it exists, the second candidate rule file is determined as the target rule file that matches the currently edited file.

4. The method as described in claim 1, characterized in that, The target project knowledge base also includes a task rule layer; before the step of generating the context prompt word set of the currently edited file based on the global rule file and the target rule file, the following is also included: In response to an explicit task invocation command input by the user, the task rule file corresponding to the explicit task invocation command is obtained from the task rule layer. The task rule file is used to carry the scenario-based task specifications triggered by the explicit task invocation command. The process of generating the contextual prompt word set for the currently edited file based on the global rule file and the target rule file includes: The contextual prompt word set for the currently edited file is generated based on the global rule file, the target rule file, and the task rule file.

5. The method as described in claim 4, characterized in that, The step of obtaining the task rule file corresponding to the explicit task invocation instruction from the task rule layer includes: The explicit task invocation instruction is parsed to identify whether it contains a task reference marker. If included, the task rule file corresponding to the explicit task invocation instruction is obtained from the task rule layer based on the task reference tag.

6. The method as described in claim 4, characterized in that, The step of generating the set of contextual prompt words for the currently edited file based on the global rule file, the target rule file, and the task rule file includes: Add the global rule file, the target rule file, and the task rule file to the initial context set respectively; The initial context set is sorted according to a preset hierarchical priority order to generate a sorted context set. The sorted context set is deduplicated to generate the context prompt word set for the currently edited file.

7. A code generation device, characterized in that, The device includes: The knowledge base management module is used to obtain the target project knowledge base corresponding to the software project to which the currently edited file belongs. The target project knowledge base includes a path-aware rule layer and a global knowledge layer. The file matching module is used to determine the target rule file that matches the current edited file from the path awareness rule layer based on the file path information of the current edited file. The target rule file is used to carry the exclusive coding specification of the code module to which the current edited file belongs. The prompt word generation module is used to obtain a global rule file from the global knowledge layer, and generate a set of context prompt words for the currently edited file based on the global rule file and the target rule file. The global rule file is used to carry the general project specifications of the software project. The code generation module is used to inject the set of context prompts into a preset code generation model, so that the preset code generation model generates code based on the set of context prompts.

8. A code generation device, characterized in that, The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the code generation method as described in any one of claims 1 to 6.

9. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, it implements the steps of the code generation method as described in any one of claims 1 to 6.

10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the steps of the code generation method as described in any one of claims 1 to 6.