Skip to content

Conversation

undertaker86001
Copy link

@undertaker86001 undertaker86001 commented Aug 26, 2025

What type of PR is this?

现状分析

当前 coze-loop 已经有了基础的模板系统:

前端实现:

  • 支持 Jinja2 语法高亮
  • 具备基本的表达式验证功能
  • 编辑器集成了 Jinja 支持

后端实现:

  • 使用 fasttemplate 进行简单的变量替换
  • 支持 {{ variable }} 格式的模板语法
  • 完整的模板格式化和执行流程

功能增强方案

第一阶段:后端 Jinja2 引擎升级

1. 引入专业 Jinja2 引擎

  • 替换现有的 fasttemplate,使用 Go 生态中的 Jinja2 实现(如 pongo2 或 raymond)
  • backend/modules/prompt/domain/entity/prompt_detail.go 中新增 Jinja2 模板类型支持
  • 扩展现有的 TemplateType 枚举,添加 TemplateTypeJinja2

2. 增强模板处理能力

  • 支持条件语句:{% if condition %}...{% endif %}
  • 支持循环语句:{% for item in items %}...{% endfor %}
  • 支持过滤器:{{ variable|filter_name }}
  • 支持函数调用:{{ function_name(args) }}

3. 安全性增强

  • 实现沙箱机制,限制可执行的函数和访问的变量
  • 添加模板执行超时机制
  • 实现变量类型检查和转换

第二阶段:前端编辑器增强

1. 语法高亮升级

  • 扩展现有的语法高亮规则,支持更多 Jinja2 语法元素
  • jinja.tsx 中添加对控制结构、过滤器、函数的高亮支持
  • 添加语法错误的实时提示

2. 智能补全功能

  • 实现变量名自动补全
  • 添加过滤器和函数的智能提示
  • 支持语法片段快速插入

3. 实时预览

  • 添加模板渲染预览功能
  • 支持测试数据输入和结果预览
  • 实现语法错误的实时检测和提示

第三阶段:高级功能实现

1. 模板函数库

  • 实现常用的字符串处理函数
  • 添加日期时间处理函数
  • 支持数学计算和逻辑运算函数
  • 提供 HTTP 请求和数据处理函数

2. 模板调试工具

  • 添加模板执行步进调试功能
  • 实现变量值实时监控
  • 提供模板性能分析工具

3. 模板复用机制

  • 支持模板片段的定义和引用
  • 实现模板继承机制
  • 添加模板库管理功能

实施技术方案

后端技术栈选择:

  • 模板引擎:pongo2(Django/Jinja2 兼容的 Go 实现)
  • 安全控制:自定义 Context 和 Filter 白名单
  • 性能优化:模板编译缓存机制

前端技术栈增强:

  • 语法解析:基于现有的 CodeMirror 扩展机制
  • AST 解析:实现 Jinja2 语法的抽象语法树解析
  • 实时验证:WebAssembly 版本的模板引擎用于客户端验证

API 设计:

  • 新增模板验证接口:POST /api/prompts/validate-template
  • 扩展现有的模板执行接口,支持 Jinja2 类型
  • 添加模板预览接口:POST /api/prompts/preview-template

Check the PR title.

  • This PR title match the format: [<type>][<scope>]: <description>. For example: [fix][backend] flaky fix
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Add documentation if the current PR requires user awareness at the usage level.
  • This PR is written in English. PRs not in English will not be reviewed.

(Optional) Translate the PR title into Chinese.

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:
zh(optional):

(Optional) Which issue(s) this PR fixes:

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@lsy357
Copy link
Collaborator

lsy357 commented Aug 26, 2025

Hi, thank you very much for your contribution. We are also supporting the prompt engineering jinjia2 template internally, which may cause some code conflicts. We will review and evaluate it as soon as possible and get in touch with you for code merging.
Thanks for your contribution again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants