npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prompt-context-builder

v1.1.7

Published

Based on project engineering files and other information, automatically generate context related to tasks to save the cost of writing prompt words.

Downloads

62

Readme

使用方式 (Usage Instructions)

安装 (Installation)

首先安装 prompt-context-builder 工具:

npm install -g prompt-context-builder

使用 (Usage)

项目结构设置 (Setting up the Project Structure)

在您的项目根目录中创建一个名为 prompt_builder 的文件夹。例如,您的项目结构应如下所示:

.
├── pom.xml
├── prompt_builder
│   ├── output
│   ├── prompt
│   │   ├── config.yml
│   │   └── 1.md 
│   └── README.md
└── src
    ├── main
    │   └── java
    └── test

prompt_builder 文件夹下,您需要准备配置文件(如 config.ymlrelated_files.yml)以及模板文件(如 1.md)。

生成文本 (Generating Text)

执行以下命令以使用 prompt_builder 生成编译后的文本,并将输出重定向到指定文件:

prompt_builder -t prompt/1.md -c prompt/config.yml -x prompt/related_files.yml > output/1.md

prompt_builder 命令说明 (Command Description)

  • -x: 此参数为可选项。
  • 若要了解更多参数含义,可直接执行 prompt_builder 命令。
Options:
      --help      Show help                                            [boolean]
      --version   Show version number                                  [boolean]
  -t, --template  Path to the template file                  [string] [required]
  -c, --config    Path to the YAML config file               [string] [required]
  -x, --context   Path to the YAML context file                         [string]

打包整个项目作为上下文

context_packer -c <config_file> -o <output_zip>
  • -c, --config <config_file>: YAML 配置文件路径
  • -o, --output <output_zip>: 输出的 zip 文件路径

context_packer 命令读取 config.yml 文件,根据配置文件中指定的过滤规则过滤文件,并将过滤后的文件按原始目录结构压缩到 zip 文件中,以便将整个项目打包作为上下文。该命令主要服务于Claude3等长上下文LLM。

IDE 插件支持

目前提供了vscode插件,项目地址为: https://github.com/jtong/prompt_builder_vscode_plugin

config 配置文件说明 (Config File Description)

config.yaml 文件的示例如下:

project:
  base_path: ../
  ignore:
    path:
      - prompt-builder
      - .git
    file:
      - .DS_Store
  filter_in:
    path:
      - "src/**/*.js"
      - "!src/ignored/**"
      - "*.md"

解释:

  1. project: 项目的顶层配置键。
  2. base_path: 指定项目文件夹的根路径。../ 表示当前文件夹的上级目录。
  3. ignore: 定义生成文件树时忽略的路径和文件。
    • path: 忽略的文件夹列表。基于 base_path 的相对路径。
    • file: 忽略的文件列表。适用于所有子文件夹。(可以在path用"**/xxx.xxx"替代,不再推荐使用。)
  4. filter_in: 定义生成文件树时需要包含的路径。
    • path: 需要包含的路径模式列表。支持通配符。

路径模式

filter_in.path 中的每一个元素都是一个路径模式字符串,支持使用以下通配符:

  • * 匹配任意字符序列,不包括路径分隔符 /
  • ** 匹配任意字符序列,包括路径分隔符 /
  • ? 匹配任意单个字符,不包括路径分隔符 /
  • [seq] 匹配序列 seq 中的任意一个字符
  • [!seq] 匹配任何不在序列 seq 中的字符

此外,还支持使用 ! 前缀来排除匹配的路径模式。

工作流程

  1. 首先,根据 ignore 规则过滤掉不需要的文件和文件夹,生成初始的文件树 JSON 对象。
  2. 然后,基于初始的文件树 JSON 对象,遍历每一个节点,判断其相对于项目根目录的路径是否匹配 filter_in.path 中的任何一个路径模式。
    • 对于文件夹节点,如果该文件夹本身或其子节点中有匹配 filter_in 规则的,则将该文件夹及其匹配的子节点保留在最终结果中。
    • 对于文件节点,如果匹配 filter_in 规则,则将该文件保留在最终结果中。
  3. 最终,根据过滤后的文件树 JSON 对象,生成文本形式的树状结构输出。

context 配置文件说明 (Context Config File Description)

related_files.yml 文件的示例如下:

- path: src/main/java/dev/jtong/training/demo/smart/domain/controllers/UsersController.java
  reader: controller
  methods:
    - changePassword
- path: src/main/java/dev/jtong/training/demo/smart/domain/controllers/representation/UserVO.java
  reader: model

这个 YAML 文件的结构是列表形式的,每个条目都定义了一个文件的路径、类型和(可选的)特定方法。列表的每一项包含了以下几个部分:

  1. path: 这个键表示一个文件的相对路径。在这里,path 的值指的是相对于config 配置文件中 base_path 的相对路径。例如:

    • src/main/java/dev/jtong/training/demo/smart/domain/controllers/UsersController.java
  2. reader: 这个键指明了对应文件的类型或用途。在这里,它可能表示用于读取或解析该文件的工具或方法。例如:

    • controller: 这表明第一个文件被视为控制器类。
    • model: 这表明第二个文件被视为模型类。
  3. methods: 这个键是一个列表,指定了文件中特定的方法或函数。这个键是可选的,只在需要时出现。例如:

    • changePassword: 这是在 UsersController.java 文件中的一个特定方法。表示只读取这一个方法。

模版文件说明 (Template File Description)

模版文件使用 handlebars 模版语法,允许在文本中插入相关函数。

folder_tree 函数 (folder_tree Function)

{{folder_tree}}

使用 folder_tree 函数可以显示 configbase_path 指定路径的文件树。

related_files 函数 (related_files Function)

{{related_files}}

使用 related_files 函数时,需要提供 context 配置文件。此函数会读取相关文件并生成内容。

related_files_from 函数 (related_files_from Function)

{{#related_files_from}}
\```yaml
- path: src/main/java/dev/jtong/training/demo/smart/domain/controllers/UsersController.java
  reader: controller
  methods:
    - changePassword
- path: src/main/java/dev/jtong/training/demo/smart/domain/controllers/representation/UserVO.java
  reader: model
\```
{{/related_files_from}}

使用 related_files_from 函数可以作为类似标签的使用方式,通过将开闭标签之间的内容作为related_files.yml中的内容使用,从而将相关文件内容内联到模版中使用。 注:上述演示中为了转义,在开闭标签中的 code block 标记之前加了\,因为不是所有的网页markdown渲染工具都支持转义语法,可能会显示出来,如果想看正确的用法可以直接查看 test/cases/3.input.md 。

partial 函数 (partial Function)

{{#partial }}
\```yaml
path: /path/to/file
render: false
\```
{{/partial }}

partial 函数允许你直接在模板中嵌入指定文件的全部内容。你需要在 {{#partial}}...{{/partial}} 标签中提供一个 YAML 配置,指定想要读取的文件路径 (path) 和是否需要渲染 (render)。

  • path: 指定想要读取内容的文件相对路径或绝对路径。路径是相对于在配置文件中定义的 base_path 的路径。
  • render: 指定是否需要将文件内容作为 prompt-context-builder 的模版进行渲染。默认为 true ,表示需要渲染。如果设置为 false ,则直接返回文件内容而不进行渲染。

注:上述演示中为了转义,在开闭标签中的 code block 标记之前加了\,因为不是所有的网页markdown渲染工具都支持转义语法,可能会显示出来,如果想看正确的用法可以直接查看 test/cases/5.input.md 。

all_files_markdown 函数 (all_files_markdown Function)

all_files_markdown用于遍历项目中的所有文件,并以Markdown格式输出它们的内容。这个助手根据config.yml的过滤规则来选择要输出的文件,保证输出的文件与folder_tree的文件相同。

用法

在模板文件中,使用以下语法来调用all_files_markdown助手:

{{ all_files_markdown }}

all_files_xml 函数(all_files_xml Function)

all_files_xml用于遍历项目中的所有文件,并以XML格式输出它们的相对路径和内容。这个助手根据config.yml的过滤规则来选择要输出的文件,保证输出的文件与folder_tree的文件相同。

用法

在模板文件中,使用以下语法来调用all_files_xml助手:

{{ all_files_xml }}

输出格式

all_files_xml助手将为每个文件生成以下XML格式的内容:

<file path="{文件的相对路径}">
<![CDATA[
{文件的内容}
]]>
</file>

其中:

  • {文件的相对路径} 表示文件相对于工程根目录的路径。
  • {文件的内容} 表示文件的实际内容,并被包裹在 <![CDATA[...]]> 部分中,以确保内容中的任何XML特殊字符不会被解析为XML标记。

这样,all_files_xml助手函数的文档就被添加到了README.md文件中。您可以根据需要对文档进行进一步调整和完善。

支持的reader

controller reader

用于读取Java代码中的controller,会保留所有的field,可以针对性的要求读取某个函数,会保留函数的所有注解

model reader

用于读取Java代码中的 model,会保留所有的field,删除掉所有的setter和getter代码。

all reader

读取文件所有内容,不做任何静态分析和预处理。