A repository-level code translation intelligent agent method and device based on code graph structure
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-03
- Publication Date
- 2026-08-11
AI Technical Summary
[0007]为了解决现有技术存在的目标仓库结构不合理且可维护性较差;难以在翻译过程中准确获取和传递复杂依赖关系,易产生接口不一致或依赖缺失问题;在翻译完成后的校验与修复阶段,修复粒度较粗,难以形成高效和稳定的自动修复流程的技术问题,本发明实施例提供了一种基于代码图结构的仓库级代码翻译智能体方法及装置
本发明实施例通过对源Python语言代码仓库整体功能及代码组件角色的系统建模,能够在仓库层面建立源语言代码组件与目标语言工程结构之间的合理对应关系,突破了现有技术中固定映射或简单一对一映射的限制,使源Python语言文件内不同代码组件能够根据其功能定位被映射至不同的目标语言文件,从而显著提升目标代码仓库的结构合理性、可读性和可维护性。
Smart Images

Figure CN122331909B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of multi-agent technology, and in particular to a warehouse-level code translation agent method and apparatus based on a code graph structure. Background Technology
[0002] As software systems continue to grow in size and complexity, the need for cross-programming language software migration is becoming increasingly prominent. Code translation technology, by automatically converting source language programs into target language programs, has significant application value in legacy system refactoring and multi-language platform adaptation. Compared to function-level or file-level translation, repository-level code translation targets the entire software repository, requiring the simultaneous handling of multiple file structures, cross-module dependencies, and target project organization methods, significantly increasing its difficulty.
[0003] Existing code translation technologies mainly include rule-based code conversion methods and code generation and translation methods based on large language models. Rule-based methods typically map source language code to target language code through syntax tree transformation or template matching. However, their structure is fixed and their adaptability is poor, making them unsuitable for complex engineering scenarios. In recent years, with the development of large language models, some existing technologies have begun to utilize these models for automatic code translation, achieving some success in function-level or file-level conversion tasks.
[0004] In repository-level code translation scenarios, existing methods typically use source language files or functions as the basic processing unit. They generate corresponding target language files one by one by traversing the source repository file structure, or complete the overall migration under pre-defined directory mapping rules. Their working principle largely relies on a partial understanding of the code content of a single file, supplemented by simple call relationship analysis. However, these methods generally assume a relatively fixed one-to-one mapping relationship between source and target language code. This makes it difficult to handle complex situations where different code blocks within the same source language file play different functional roles and need to be mapped to multiple target language files, resulting in an unreasonable structure and division of responsibilities in the generated target repository.
[0005] Furthermore, existing technologies are insufficient in collecting and utilizing complex dependency information. Although some methods introduce static analysis techniques such as call graphs or dependency graphs, they mostly remain at the structural connection level and are unable to fully characterize the contextual dependencies across files and modules. Especially when dealing with type constraints, implicit calls, or project-level dependencies, the problem of missing contextual information during translation can easily occur, thus affecting the integrity and consistency of the target code.
[0006] In the verification and repair phase after translation, existing technologies typically use the method of inputting the entire target code repository and compilation error information into the model for repair. This repair process lacks fine-grained analysis of the specific error location, cause, and scope of impact, resulting in coarse-grained repair. It is difficult to form a stable and efficient automatic repair mechanism, and the overall process still relies on repeated manual intervention, which limits its engineering practicality. Summary of the Invention
[0007] To address the technical problems of existing technologies, such as unreasonable target repository structures and poor maintainability; difficulty in accurately acquiring and transmitting complex dependencies during translation, leading to interface inconsistencies or missing dependencies; and coarse-grained repair during the post-translation verification and repair phases, making it difficult to form an efficient and stable automated repair process, this invention provides a repository-level code translation intelligent agent method and apparatus based on a code graph structure. The technical solution is as follows: On the one hand, a repository-level code translation agent method based on a code graph structure is provided. This method is implemented by a repository-level code translation agent device based on a code graph structure, and includes: S1. Construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent; S2. Input the source Python code repository into the repository functional understanding agent to perform functional analysis and establish the correspondence between the source Python code components and the target Java language code files; S3. Based on the correspondence, determine the translation order of the target Java language code files using a code translation agent; translate each target Java language code file sequentially based on the translation order to obtain translation results; construct a target Java language repository based on the translation results. S4. Input the target Java language repository into the code inspection and repair agent, repair the target Java language repository using pre-configured processing tools, and output a complete and runnable target Java language repository.
[0008] On the other hand, a repository-level code translation intelligent agent device based on a code graph structure is provided. This device is applied to a repository-level code translation intelligent agent method based on a code graph structure. The device includes: A building unit is used to construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent; Establish a unit to input the source Python code repository into the repository functional understanding agent for functional analysis, and establish the correspondence between the source Python code components and the target Java language code files; The translation unit is configured to determine the translation order of the target Java language code files based on the correspondence using a code translation agent; translate each target Java language code file sequentially based on the translation order to obtain translation results; and construct a target Java language repository based on the translation results. The repair unit is used to input the target Java language repository into the code inspection and repair agent, repair the target Java language repository through pre-configured processing tools, and output a complete and runnable target Java language repository.
[0009] On the other hand, a repository-level code translation agent device based on a code graph structure is provided. The repository-level code translation agent device based on a code graph structure includes: a processor; a memory, wherein computer-readable instructions are stored in the memory, and when the computer-readable instructions are executed by the processor, any one of the methods in the above-described repository-level code translation agent method based on a code graph structure is implemented.
[0010] On the other hand, a computer-readable storage medium is provided, wherein at least one instruction is stored in the storage medium, the at least one instruction being loaded and executed by a processor to implement any of the above-described warehouse-level code translation agent methods based on code graph structure.
[0011] The beneficial effects of the technical solutions provided in the embodiments of the present invention include at least the following: This invention, through systematic modeling of the overall functionality and code component roles of the source Python language code repository, can establish a reasonable correspondence between source language code components and target language engineering structures at the repository level. This breaks through the limitations of fixed mapping or simple one-to-one mapping in the prior art, enabling different code components within the source Python language file to be mapped to different target language files according to their functional positioning, thereby significantly improving the structural rationality, readability, and maintainability of the target code repository.
[0012] The embodiments of the present invention can comprehensively acquire and utilize cross-file and cross-module context dependency information during the translation process, and construct translation order and context constraints based on the dependency relationships between code components. This effectively avoids interface inconsistencies, type errors, or build failures caused by missing dependency information, and improves the completeness and consistency of repository-level code translation results.
[0013] The embodiments of the present invention can automatically check the target language code repository after translation is completed, and perform fine-grained analysis and repair for specific error locations and causes, forming a closed-loop automatic repair mechanism. Compared with the existing overall and coarse-grained repair methods, it can reduce manual intervention and improve the engineering usability and stability of the translation results. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0015] Figure 1 This is a flowchart of a repository-level code translation intelligent agent method based on a code graph structure provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of a warehouse-level code translation intelligent agent framework provided in an embodiment of the present invention; Figure 3 This is a flowchart of a warehouse function understanding intelligent agent provided in an embodiment of the present invention; Figure 4 This is a block diagram of a warehouse-level code translation intelligent agent device based on a code graph structure provided in an embodiment of the present invention; Figure 5 This is a schematic diagram of a warehouse-level code translation intelligent agent device based on a code graph structure provided in an embodiment of the present invention. Detailed Implementation
[0016] The technical solution of the present invention will now be described with reference to the accompanying drawings.
[0017] In embodiments of the present invention, words such as "exemplarily," "for example," etc., are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" in the present invention should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the word "exemplary" is intended to present the concept in a concrete manner. Furthermore, in embodiments of the present invention, the meaning expressed by "and / or" can be both, or either one.
[0018] In the embodiments of this invention, the terms "image" and "picture" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning. Similarly, the terms "of," "corresponding (relevant)," and "corresponding" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning.
[0019] In this embodiment of the invention, sometimes a subscript such as W1 may be written in a non-subscript form such as W1. When the difference is not emphasized, the meaning they express is the same.
[0020] To make the technical problems, technical solutions and advantages of the present invention clearer, a detailed description will be given below in conjunction with the accompanying drawings and specific embodiments.
[0021] This invention provides a repository-level code translation agent method based on a code graph structure. This method can be implemented by a repository-level code translation agent device based on a code graph structure, which can be a terminal or a server. Figure 1 The flowchart shown is for a repository-level code translation agent method based on a code graph structure. The processing flow of this method may include the following steps: S1. Construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent.
[0022] in, Figure 2 This is a schematic diagram of a repository-level code translation intelligent agent framework provided by an embodiment of the present invention. In one feasible implementation, this embodiment of the present invention constructs a repository-level code translation intelligent agent framework to achieve overall understanding of source code repositories, code structure modeling, cross-language mapping and translation, and automated inspection and repair of translation results. The repository-level code translation intelligent agent framework includes: a repository function understanding intelligent agent, a code translation intelligent agent, and a code inspection and repair intelligent agent. The aforementioned intelligent agents are based on a large language model, which can be one of ChatGPT, Qwen3, or DeepSeek-R1.
[0023] S2. Input the source Python code repository into the repository functional understanding agent to perform functional analysis and establish the correspondence between the source Python code components and the target Java language code files.
[0024] Optionally, the specific implementation process of S2 includes S21-S25: S21. Obtain the relative file paths and corresponding Python code content of each code file in the source Python code repository, and input them into the large language model to conduct a comprehensive analysis of the functions implemented by the source Python code repository and its application scenarios, and output a summary of the functions of the source Python code repository. S22. The source Python code is parsed using a code syntax parser called Tree-sitter, which divides the source Python code into multiple code components, including: class components, function components, and continuous code block components. Python is a scripting language, and a single file can contain consecutive executable code blocks, function definitions, and class definitions.
[0025] Each code component records the relative file path to which it belongs and the corresponding source code content.
[0026] Within a continuous code block component, adjacent functions or classes can be called, or previously defined variables can be called within a function or class.
[0027] S23. Based on the multiple source Python code components that have been divided, merge the adjacent source Python code components that have a calling relationship to obtain the merged source Python code components. By merging the source Python code components, redundant analysis information caused by irrelevant components can be reduced.
[0028] S24. Based on the merged source Python code components, construct a component dependency graph; according to the reverse order of the topological sort of the component dependency graph and the functional summary of the source Python code repository, generate a corresponding component functional summary for each source Python code component through the large language model; In the process of generating a functional summary of the source Python code repository, the large language model comprehensively considers the functional summary of the repository, the functional information of other components that the component depends on, and the source code content contained in the component itself, so as to accurately describe the functional positioning of the component in the entire code repository.
[0029] S25. Obtain the pre-defined target Java code repository directory structure information; use a large language model to analyze the main functional positioning of each Java file in the target Java code repository, and generate a component function summary corresponding to each Java file based on each source Python code component, determine the Java file mapped to each source Python code component, and establish the correspondence between source Python code components and target Java language code files.
[0030] Among them, such as Figure 3 The diagram shown is a flowchart of a warehouse function understanding intelligent agent provided by an embodiment of the present invention; in one feasible implementation, Figure 3 This paper demonstrates the main processing steps of a repository functionality understanding agent in analyzing a source language code repository. These steps include analyzing the overall functionality of the code repository, dividing the source language code into components, constructing dependencies between components, and generating component functional information. This process enables systematic modeling of the overall functionality of the source language code repository and the functional roles of each code component, providing foundational information for subsequent mapping and translation between code components and the target language engineering structure.
[0031] S3. Based on the correspondence, determine the translation order of the target Java language code files through the code translation agent; translate each target Java language code file sequentially according to the translation order to obtain the translation results; construct the target Java language repository based on the translation results.
[0032] Optionally, the specific implementation process of S3 includes S31-S36: S31. Based on the correspondence between the source Python code components and the target Java language code file, using a large language model, and combining the functional information of each source Python code component with the functional positioning of the target Java file, determine whether each source Python code component is a core component; among them, the source Python code components that can implement the core functions of the Java file are defined as core components. S32. For each core component, select at least two components that depend on that core component as related components; select the two components that are depended on the most among all core components as dependent components. S33. Establish a static dependency relationship between the selected dependent component's mapped Java file and the current Java file; S34. Use the large language model to determine the potential dependencies between Java files. If any one of the conditions for the existence of a potential dependency is met, then it is determined that there is a potential dependency between the Java files. Optionally, the conditions for the existence of a potential dependency include: The parameters, return values, or instantiated objects of functions or classes in the core component code exist in classes defined in corresponding candidate Java files; or, The code of the core components directly or indirectly calls methods defined in the candidate Java files.
[0033] S35. Construct a Java file dependency graph based on static and potential dependencies; determine the translation order of Java files according to the reverse topological sort of the Java file dependency graph. By determining the translation order of Java files, the translation of dependent Java files is ensured to be completed first, thus avoiding issues such as missing interfaces or inconsistent types.
[0034] S36. Based on the translation order of Java files, a large language model is used to translate each Java file, generating translated Java files and a pom.xml file matching each translated Java file; based on the pom.xml file and the translated Java files, the target Java language repository is constructed.
[0035] During the translation process, the large language model comprehensively utilizes the core component code, related component code, and the translated dependent Java file code to generate the corresponding Java file code.
[0036] S4. Input the target Java language repository into the code inspection and repair agent, repair the target Java language repository using pre-configured processing tools, and output a complete and runnable target Java language repository.
[0037] Optionally, the pre-configured processing tools include: file reading tools, file writing tools, and compilation tools.
[0038] Optionally, the specific implementation process of S4 includes S41-S44: S41. Input the target Java language repository into the code inspection and repair agent, and use the large language model to call the compilation tool to compile the target Java language repository and obtain formatted compilation error information. Optionally, formatted compilation error messages include: the code location where the error occurred and the reason for the error.
[0039] S42. Locate the target error file based on formatted compilation error information, and use a file reading tool to read the contents of the target error file to obtain context code information; When the context information is insufficient to support error analysis, the file reading tool can be called to obtain more code information.
[0040] S43. Based on formatted compilation error information and context code information, generate the repaired code content through a large language model, and modify it through a file writing tool to generate the modified code; S44. Based on the modified code, perform a compilation check using a compilation tool. If compilation errors are found, proceed to steps S42-S43 until the preset maximum number of repair rounds is reached, at which point the repair process stops and a complete, runnable target Java language repository is output.
[0041] This invention, through systematic modeling of the overall functionality and code component roles of the source Python language code repository, can establish a reasonable correspondence between source language code components and target language engineering structures at the repository level. This breaks through the limitations of fixed mapping or simple one-to-one mapping in the prior art, enabling different code components within the source Python language file to be mapped to different target language files according to their functional positioning, thereby significantly improving the structural rationality, readability, and maintainability of the target code repository.
[0042] The embodiments of the present invention can comprehensively acquire and utilize cross-file and cross-module context dependency information during the translation process, and construct translation order and context constraints based on the dependency relationships between code components. This effectively avoids interface inconsistencies, type errors, or build failures caused by missing dependency information, and improves the completeness and consistency of repository-level code translation results.
[0043] The embodiments of the present invention can automatically check the target language code repository after translation is completed, and perform fine-grained analysis and repair for specific error locations and causes, forming a closed-loop automatic repair mechanism. Compared with the existing overall and coarse-grained repair methods, it can reduce manual intervention and improve the engineering usability and stability of the translation results.
[0044] Figure 4 This is a block diagram of a repository-level code translation intelligent agent device based on a code graph structure, provided in an embodiment of the present invention. This device is used in a repository-level code translation intelligent agent method based on a code graph structure. (Refer to...) Figure 4 The device includes a construction unit 410, a building unit 420, a translation unit 430, and a repair unit 440. Wherein: Construction unit 410 is used to construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent; Unit 420 is established to input the source Python code repository into the repository functional understanding agent for functional analysis and to establish the correspondence between the source Python code components and the target Java language code files; Translation unit 430 is used to determine the translation order of target Java language code files through a code translation agent based on the correspondence; translate each target Java language code file sequentially based on the translation order to obtain translation results; and construct a target Java language repository based on the translation results. Repair unit 440 is used to input the target Java language repository into the code inspection and repair agent, repair the target Java language repository through pre-configured processing tools, and output a complete and runnable target Java language repository.
[0045] Optionally, the establishing unit 420 is used for: Obtain the relative file paths and corresponding Python code content of each code file in the source Python code repository, and input them into the large language model to conduct a comprehensive analysis of the functions implemented by the source Python code repository and its application scenarios, and output a summary of the functions of the source Python code repository. The source Python code is parsed using a code syntax parser called Tree-sitter, which divides the source Python code into multiple code components, including: class components, function components, and continuous code block components. Based on the multiple source Python code components that are divided, adjacent source Python code components that have a calling relationship are merged to obtain merged source Python code components; Based on the merged source Python code components, a component dependency graph is constructed; according to the reverse order of the topological sort of the component dependency graph and the functional summary of the source Python code repository, a corresponding component functional summary is generated for each source Python code component through a large language model; Obtain the pre-defined target Java code repository directory structure information; use a large language model to analyze the main functional positioning of each Java file in the target Java code repository, and generate a component function summary corresponding to each Java file based on each source Python code component, determine the Java file mapped to each source Python code component, and establish the correspondence between source Python code components and target Java language code files.
[0046] Optionally, the translation unit 430 is used for: Based on the correspondence between source Python code components and target Java language code files, a large language model is used, combined with the functional information of each source Python code component and the functional positioning of the target Java file, to determine whether each source Python code component is a core component; among them, source Python code components that can implement the core functions of the Java file are defined as core components; For each core component, select at least two components that depend on that core component as related components; select the two components that are depended on the most among all core components as dependent components. Establish a static dependency relationship between the selected dependent component's mapped Java file and the current Java file; A large language model is used to determine potential dependencies between Java files. If any one of the conditions for the existence of a potential dependency is met, it is determined that there is a potential dependency between Java files. Based on static and potential dependencies, construct a Java file dependency graph; determine the translation order of Java files according to the reverse topological sort of the Java file dependency graph. Based on the translation order of Java files, a large language model is used to translate each Java file, generating translated Java files and a matching pom.xml file for each translated Java file; based on the pom.xml file and the translated Java files, a target Java language repository is constructed.
[0047] Optionally, the conditions for the existence of a potential dependency include: The parameters, return values, or instantiated objects of functions or classes in the core component code exist in classes defined in corresponding candidate Java files; or, The code of the core components directly or indirectly calls methods defined in the candidate Java files.
[0048] Optionally, the pre-configured processing tools include: a file reading tool, a file writing tool, and a compilation tool.
[0049] Optionally, the repair unit 440 is used for: (1) Input the target Java language repository into the code inspection and repair agent, and use the large language model to call the compilation tool to compile the target Java language repository and obtain formatted compilation error information; (2) Locate the target error file based on the formatted compilation error information, and use a file reading tool to read the contents of the target error file to obtain the context code information; (3) Based on the formatted compilation error information and context code information, the repaired code content is generated through the large language model, and modified through the file writing tool to generate the modified code; (4) Based on the modified code, perform compilation checks using a compilation tool. If there are compilation errors, proceed to steps (2)-(3) until the preset maximum number of repair rounds is reached and the repair is stopped, outputting a complete and runnable target Java language repository.
[0050] Optionally, the formatted compilation error information includes: the code location where the error occurred and the reason for the error.
[0051] This invention, through systematic modeling of the overall functionality and code component roles of the source Python language code repository, can establish a reasonable correspondence between source language code components and target language engineering structures at the repository level. This breaks through the limitations of fixed mapping or simple one-to-one mapping in the prior art, enabling different code components within the source Python language file to be mapped to different target language files according to their functional positioning, thereby significantly improving the structural rationality, readability, and maintainability of the target code repository.
[0052] The embodiments of the present invention can comprehensively acquire and utilize cross-file and cross-module context dependency information during the translation process, and construct translation order and context constraints based on the dependency relationships between code components. This effectively avoids interface inconsistencies, type errors, or build failures caused by missing dependency information, and improves the completeness and consistency of repository-level code translation results.
[0053] The embodiments of the present invention can automatically check the target language code repository after translation is completed, and perform fine-grained analysis and repair for specific error locations and causes, forming a closed-loop automatic repair mechanism. Compared with the existing overall and coarse-grained repair methods, it can reduce manual intervention and improve the engineering usability and stability of the translation results.
[0054] Figure 5 This is a schematic diagram of a repository-level code translation intelligent agent device based on a code graph structure provided in an embodiment of the present invention, such as... Figure 5 As shown, a repository-level code translation intelligent agent device based on a code graph structure can include the above-mentioned... Figure 4 The illustrated repository-level code translation agent device is based on a code graph structure. Optionally, the repository-level code translation agent device 510 based on a code graph structure may include a first processor 2001.
[0055] Optionally, the repository-level code translation agent device 510 based on the code graph structure may also include a memory 2002 and a transceiver 2003.
[0056] The first processor 2001, memory 2002, and transceiver 2003 can be connected via a communication bus.
[0057] The following is combined Figure 5 A detailed introduction to each component of the 510 repository-level code translation intelligent agent device based on a code graph structure is provided below: The first processor 2001 is the control center of the repository-level code translation intelligent agent device 510 based on a code graph structure. It can be a single processor or a collective term for multiple processing elements. For example, the first processor 2001 can be one or more central processing units (CPUs), application-specific integrated circuits (ASICs), or one or more integrated circuits configured to implement embodiments of the present invention, such as one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs).
[0058] Optionally, the first processor 2001 can perform various functions of the warehouse-level code translation intelligent agent device 510 based on the code graph structure by running or executing software programs stored in the memory 2002 and calling data stored in the memory 2002.
[0059] In a specific implementation, as one example, the first processor 2001 may include one or more CPUs, for example... Figure 5 CPU0 and CPU1 are shown in the diagram.
[0060] In a specific implementation, as one example, the repository-level code translation intelligent agent device 510 based on a code graph structure may also include multiple processors, for example... Figure 5 The first processor 2001 and the second processor 2004 are shown in the diagram. Each of these processors can be a single-core processor or a multi-core processor. Here, a processor can refer to one or more devices, circuits, and / or processing cores used to process data (such as computer program instructions).
[0061] The memory 2002 is used to store the software program that executes the present invention, and is controlled by the first processor 2001 to execute it. The specific implementation method can be referred to the above method embodiment, and will not be repeated here.
[0062] Optionally, the memory 2002 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but not limited thereto. The memory 2002 may be integrated with the first processor 2001 or may exist independently, and may be connected via the interface circuit of the code graph-based warehouse-level code translation intelligent agent device 510. Figure 5 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0063] The transceiver 2003 is used to communicate with network devices or with terminal devices.
[0064] Alternatively, transceiver 2003 may include a receiver and a transmitter. Figure 5 (Not shown separately). The receiver is used to implement the receiving function, and the transmitter is used to implement the transmitting function.
[0065] Optionally, the transceiver 2003 can be integrated with the first processor 2001 or exist independently, and can be connected to the interface circuit of the repository-level code translation intelligent agent device 510 based on a code graph structure. Figure 5 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0066] It should be noted that, Figure 5 The structure of the repository-level code translation agent device 510 based on the code graph structure shown in the diagram does not constitute a limitation on the router. Actual repository-level code translation agent devices based on the code graph structure may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0067] Furthermore, the technical effects of the repository-level code translation agent device 510 based on the code graph structure can be referred to the technical effects of the repository-level code translation agent method based on the code graph structure described in the above method embodiments, and will not be repeated here.
[0068] It should be understood that the first processor 2001 in the embodiments of the present invention may be a central processing unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor, or it may be any conventional processor, etc.
[0069] It should also be understood that the memory in the embodiments of the present invention can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous DRAM (DDR SDRAM), enhanced synchronous DRAM (ESDRAM), synchronous linked DRAM (SLDRAM), and direct rambus RAM (DR RAM).
[0070] The above embodiments can be implemented, in whole or in part, by software, hardware (such as circuits), firmware, or any other combination thereof. When implemented using software, the above embodiments can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more sets of available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium. A semiconductor medium can be a solid-state drive.
[0071] It should be understood that the term "and / or" in this article is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. A and B can be singular or plural. Additionally, the character " / " in this article generally indicates an "or" relationship between the preceding and following related objects, but it can also represent an "and / or" relationship. Please refer to the context for a more accurate understanding.
[0072] In this invention, "at least one" means one or more, and "more than one" means two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of a single item or a plurality of items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be a single item or multiple items.
[0073] It should be understood that, in various embodiments of the present invention, the order of the above-mentioned process numbers does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0074] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0075] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0076] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0077] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0078] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0079] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0080] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention 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 the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A warehouse-level code translation agent method based on code graph structure, characterized in that, The method includes: S1. Construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent; S2. Input the source Python code repository into the repository functional understanding agent to perform functional analysis and establish the correspondence between the source Python code components and the target Java language code files; Specifically, S2 involves inputting the source Python code repository into the repository functional understanding agent for functional analysis, establishing the correspondence between the source Python code components and the target Java language code files, including: S21. Obtain the relative file paths and corresponding Python code content of each code file in the source Python code repository, and input them into the large language model to conduct a comprehensive analysis of the functions implemented by the source Python code repository and its application scenarios, and output a summary of the functions of the source Python code repository. S22. The source Python code is parsed using a code syntax parser called Tree-sitter, which divides the source Python code into multiple code components, including: class components, function components, and continuous code block components. S23. Based on the multiple source Python code components that have been divided, merge the adjacent source Python code components that have a calling relationship to obtain the merged source Python code components. S24. Based on the merged source Python code components, construct a component dependency graph; according to the reverse order of the topological sort of the component dependency graph and the functional summary of the source Python code repository, generate a corresponding component functional summary for each source Python code component through the large language model; S25. Obtain the pre-defined target Java code repository directory structure information; use a large language model to analyze the main functional positioning of each Java file in the target Java code repository, and generate a component function summary corresponding to each Java file based on each source Python code component, determine the Java file mapped to each source Python code component, and establish the correspondence between source Python code components and target Java language code files; S3. Based on the correspondence, determine the translation order of the target Java language code files using a code translation agent; translate each target Java language code file sequentially based on the translation order to obtain translation results; construct a target Java language repository based on the translation results. Specifically, S3 determines the translation order of the target Java language code files through a code translation agent based on the correspondence; translates each target Java language code file sequentially based on the translation order to obtain translation results; and constructs a target Java language repository based on the translation results, including: S31. Based on the correspondence between the source Python code components and the target Java language code file, using a large language model, and combining the functional information of each source Python code component with the functional positioning of the target Java file, determine whether each source Python code component is a core component; among them, the source Python code components that can implement the core functions of the Java file are defined as core components. S32. For each core component, select at least two components that depend on that core component as related components; select the two components that are depended on the most among all core components as dependent components. S33. Establish a static dependency relationship between the selected dependent component's mapped Java file and the current Java file; S34. Use the large language model to determine the potential dependencies between Java files. If any one of the conditions for the existence of a potential dependency is met, then it is determined that there is a potential dependency between the Java files. S35. Construct a Java file dependency graph based on static and potential dependencies; determine the translation order of Java files according to the reverse topological sort of the Java file dependency graph. S36. Based on the translation order of Java files, a large language model is used to translate each Java file, generating translated Java files and a pom.xml file matching each translated Java file; based on the pom.xml file and the translated Java files, the target Java language repository is constructed. S4. Input the target Java language repository into the code inspection and repair agent, repair the target Java language repository using pre-configured processing tools, and output a complete and runnable target Java language repository; wherein, the pre-configured processing tools include: file reading tools, file writing tools, and compilation tools; Specifically, in step S4, the target Java language repository is input into the code inspection and repair agent, where a pre-configured processing tool repairs the target Java language repository, outputting a complete and runnable target Java language repository, including: S41. Input the target Java language repository into the code inspection and repair agent, and use the large language model to call the compilation tool to compile the target Java language repository and obtain formatted compilation error information. S42. Locate the target error file based on formatted compilation error information, and use a file reading tool to read the contents of the target error file to obtain context code information; S43. Based on formatted compilation error information and context code information, generate the repaired code content through a large language model, and modify it through a file writing tool to generate the modified code; S44. Based on the modified code, perform a compilation check using a compilation tool. If compilation errors are found, proceed to steps S42-S43 until the preset maximum number of repair rounds is reached, at which point the repair process stops and a complete, runnable target Java language repository is output.
2. The code graph structure based warehouse level code translation agent method of claim 1, wherein, The conditions for the existence of a potential dependency include: The parameters, return values, or instantiated objects of functions or classes in the core component code exist in classes defined in corresponding candidate Java files; or, The code of the core components directly or indirectly calls methods defined in the candidate Java files.
3. The code map structure based warehouse level code translation agent method of claim 1, wherein, The formatted compilation error message includes: the code location where the error occurred and the reason for the error.
4. A repository-level code translation agent device based on a code graph structure, wherein the repository-level code translation agent device based on a code graph structure is used to implement the repository-level code translation agent method based on a code graph structure as described in any one of claims 1-3, characterized in that, The device includes: A building unit is used to construct a repository-level code translation agent framework; the framework includes: a repository function understanding agent, a code translation agent, and a code inspection and repair agent; Establish a unit to input the source Python code repository into the repository functional understanding agent for functional analysis, and establish the correspondence between the source Python code components and the target Java language code files; The translation unit is configured to determine the translation order of the target Java language code files based on the correspondence using a code translation agent; translate each target Java language code file sequentially based on the translation order to obtain translation results; and construct a target Java language repository based on the translation results. The repair unit is used to input the target Java language repository into the code inspection and repair agent, repair the target Java language repository through pre-configured processing tools, and output a complete and runnable target Java language repository.
5. The repository-level code translation intelligent agent device based on code graph structure according to claim 4, characterized in that, The establishment unit is used for: Obtain the relative file paths and corresponding Python code content of each code file in the source Python code repository, and input them into the large language model to conduct a comprehensive analysis of the functions implemented by the source Python code repository and its application scenarios, and output a summary of the functions of the source Python code repository. The source Python code is parsed using a code syntax parser called Tree-sitter, which divides the source Python code into multiple code components, including: class components, function components, and continuous code block components. Based on the multiple source Python code components that are divided, adjacent source Python code components that have a calling relationship are merged to obtain the merged source Python code components. Based on the merged source Python code components, a component dependency graph is constructed; according to the reverse order of the topological sort of the component dependency graph and the functional summary of the source Python code repository, a corresponding component functional summary is generated for each source Python code component through a large language model; Obtain the predefined target Java code repository directory structure information; use a large language model to analyze the main functional positioning of each Java file in the target Java code repository, and generate a component function summary corresponding to each Java file based on each source Python code component, determine the Java file mapped to each source Python code component, and establish the correspondence between source Python code components and target Java language code files.
6. The repository-level code translation intelligent agent device based on code graph structure according to claim 4, characterized in that, The translation unit is used for: Based on the correspondence between source Python code components and target Java language code files, a large language model is used, combined with the functional information of each source Python code component and the functional positioning of the target Java file, to determine whether each source Python code component is a core component; among them, source Python code components that can implement the core functions of the Java file are defined as core components. For each core component, select at least two components that depend on that core component as related components; select the two components that are depended on the most among all core components as dependent components; Establish a static dependency relationship between the selected dependent component's mapped Java file and the current Java file; A large language model is used to determine potential dependencies between Java files. If any one of the conditions for the existence of a potential dependency is met, it is determined that there is a potential dependency between Java files. Based on static and potential dependencies, construct a Java file dependency graph; determine the translation order of Java files according to the reverse topological sort of the Java file dependency graph. Based on the translation order of Java files, a large language model is used to translate each Java file, generating translated Java files and a matching pom.xml file for each translated Java file; based on the pom.xml file and the translated Java files, a target Java language repository is constructed.
7. A repository-level code translation intelligent agent device based on a code graph structure, characterized in that, The repository-level code translation intelligent agent device based on code graph structure includes: processor; A memory storing computer-readable instructions that, when executed by the processor, implement the method as described in any one of claims 1 to 3.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium contains program code that can be invoked by a processor to execute the method as described in any one of claims 1 to 3.
Citation Information
Patent Citations
Data processing method and device for converting C into Java and equipment
CN113608748A
Code translation method and device based on large language model
CN120371689A