Files
codex/codex-cli/src/utils/singlepass/context.ts
T
Yonatan Lavy 6d4c4b1bdd refactor: improve performance of renderFilesToXml using Array.join (#127)
### Summary

Refactored the `renderFilesToXml` function to improve performance and
readability by replacing iterative string concatenation with
`Array.map().join()`.

### Changes

- Replaced the `for...of` loop with `files.map(...).join('')`
- Reduced number of string mutation operations
- Preserved the existing XML structure and CDATA safety

### Why

Using `join` avoids repeated string concatenation in loops, which can
improve performance, especially when rendering a large number of files.
It also results in more concise and idiomatic code.


I have read the CLA Document and I hereby sign the CLA
---

Let me know if this needs any adjustments!

Signed-off-by: yonatanlavy <yehonatanmind@gmail.com>
2025-04-16 16:14:57 -07:00

66 lines
2.1 KiB
TypeScript

/** Represents file contents with a path and its full text. */
export interface FileContent {
path: string;
content: string;
}
/**
* Represents the context for a task, including:
* - A prompt (the user's request)
* - A list of input paths being considered editable
* - A directory structure overview
* - A collection of file contents
*/
export interface TaskContext {
prompt: string;
input_paths: Array<string>;
input_paths_structure: string;
files: Array<FileContent>;
}
/**
* Renders a string version of the TaskContext, including a note about important output requirements,
* summary of the directory structure (unless omitted), and an XML-like listing of the file contents.
*
* The user is instructed to produce only changes for files strictly under the specified paths
* and provide full file contents in any modifications.
*/
export function renderTaskContext(taskContext: TaskContext): string {
const inputPathsJoined = taskContext.input_paths.join(", ");
return `
Complete the following task: ${taskContext.prompt}
# IMPORTANT OUTPUT REQUIREMENTS
- UNDER NO CIRCUMSTANCES PRODUCE PARTIAL OR TRUNCATED FILE CONTENT. You MUST provide the FULL AND FINAL content for every file modified.
- ALWAYS INCLUDE THE COMPLETE UPDATED VERSION OF THE FILE, do not omit or only partially include lines.
- ONLY produce changes for files located strictly under ${inputPathsJoined}.
- ALWAYS produce absolute paths in the output.
- Do not delete or change code UNRELATED to the task.
# **Directory structure**
${taskContext.input_paths_structure}
# Files
${renderFilesToXml(taskContext.files)}
`;
}
/**
* Converts the provided list of FileContent objects into a custom XML-like format.
*
* For each file, we embed the content in a CDATA section.
*/
function renderFilesToXml(files: Array<FileContent>): string {
const fileContents = files
.map(
(fc) => `
<file>
<path>${fc.path}</path>
<content><![CDATA[${fc.content}]]></content>
</file>`,
)
.join("");
return `<files>\n${fileContents}\n</files>`;
}