Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: generalize transformations and scripts of runner and preloads into TemplateTransformer #4487

Merged
merged 3 commits into from
May 20, 2024

Conversation

bowenliang123
Copy link
Contributor

@bowenliang123 bowenliang123 commented May 17, 2024

Description

  • generalize scripts of runner and preload into TemplateTransformer
  • properly indent scripts with dedent inside methods

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • existed tests for code executor

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes

@bowenliang123 bowenliang123 marked this pull request as ready for review May 17, 2024 12:50
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 💪 enhancement New feature or request labels May 17, 2024
@bowenliang123 bowenliang123 marked this pull request as draft May 17, 2024 12:59
@bowenliang123 bowenliang123 marked this pull request as ready for review May 17, 2024 13:12
@bowenliang123 bowenliang123 changed the title improve: generalize scripts of runner and preloads into TemplateTransformer improve: generalize response transformation and scripts of runner and preloads into TemplateTransformer May 17, 2024
@bowenliang123
Copy link
Contributor Author

cc @takatost @Yeuoly

@bowenliang123
Copy link
Contributor Author

BTW, during the refactoring, it's found that the approaches for the input objects serialization vary between Python and Javascript. The one for Python is wrapped in Base64 encoding, while the Javascript uses the raw inputs json object. Not the best way for further extensions to other languages. Is that as designed on purpose?

@Yeuoly
Copy link
Collaborator

Yeuoly commented May 18, 2024

BTW, during the refactoring, it's found that the approaches for the input objects serialization vary between Python and Javascript. The one for Python is wrapped in Base64 encoding, while the Javascript uses the raw inputs json object. Not the best way for further extensions to other languages. Is that as designed on purpose?

In Python, the boolean value True is capitalized, which is not standard in JSON encoding. As a result, simple JSON encoding might not work as unexpected. However, this issue does not occur in JavaScript.

@bowenliang123
Copy link
Contributor Author

Yes, the differences were noticed. So maybe we could unify the json+base64 as the standard serialization for input objects in the future.

@bowenliang123
Copy link
Contributor Author

And there's another point I may want to have your suggestions. We are still using replace with to fill the code and inputs, as the existed way before refactoring. Do you want jinja2 to do this for possible injection attacks (but I'm not sure how much MarkupSafe inside jinja2 does help) ?

@bowenliang123 bowenliang123 changed the title improve: generalize response transformation and scripts of runner and preloads into TemplateTransformer improve: generalize transformations and scripts of runner and preloads into TemplateTransformer May 18, 2024
@Yeuoly
Copy link
Collaborator

Yeuoly commented May 19, 2024

Yes, the differences were noticed. So maybe we could unify the json+base64 as the standard serialization for input objects in the future.

feel free to do it~

@Yeuoly
Copy link
Collaborator

Yeuoly commented May 19, 2024

And there's another point I may want to have your suggestions. We are still using replace with to fill the code and inputs, as the existed way before refactoring. Do you want jinja2 to do this for possible injection attacks (but I'm not sure how much MarkupSafe inside jinja2 does help) ?

It's unnecessary I think, the replace method doesn't significantly impact performance, maybe format could be a good alternative. As for Jinja2, it's too heavy for this task. It supports many features like loops, which we don't need. Additionally, it introduces some security risks. Even if it's currently safe, security experts can always find new ways to exploit it as I faced before.

@bowenliang123
Copy link
Contributor Author

And there's another point I may want to have your suggestions. We are still using replace with to fill the code and inputs, as the existed way before refactoring. Do you want jinja2 to do this for possible injection attacks (but I'm not sure how much MarkupSafe inside jinja2 does help) ?

It's unnecessary I think, the replace method doesn't significantly impact performance, maybe format could be a good alternative. As for Jinja2, it's too heavy for this task. It supports many features like loops, which we don't need. Additionally, it introduces some security risks. Even if it's currently safe, security experts can always find new ways to exploit it as I faced before.

Yes, I agree with you. Let's keep it with "replace".

@bowenliang123
Copy link
Contributor Author

bowenliang123 commented May 19, 2024

Yes, the differences were noticed. So maybe we could unify the json+base64 as the standard serialization for input objects in the future.

feel free to do it~

Hi, I have get this done. json + base64 serialization is now a shared mechanism for both Python and Javascript.
No blocker issues or discussions remain in this PR.

@loganclark360
Copy link

loganclark360 commented May 19, 2024 via email

Copy link
Collaborator

@Yeuoly Yeuoly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 20, 2024
@Yeuoly Yeuoly merged commit 5f4df34 into langgenius:main May 20, 2024
7 checks passed
@bowenliang123 bowenliang123 deleted the runner-temp branch May 20, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants